API security is the set of checks that decide who can call an endpoint and what that call is allowed to see or change. For a small app, the API is often the real product: a mobile client, a single-page app, a webhook, and a handful of routes that talk to the database. The screens can hide a button. The route still answers anyone who can form the request. If you protect the endpoints and treat every response as public until proven otherwise, you keep one customer's data from becoming another customer's download.
This guide is for builders shipping a JSON API without a security team. It covers authentication, ownership, validation, rate limits, and data exposure, in the order a request actually hits your server. The OWASP REST Security Cheat Sheet is the longer reference. The steps below are the ones to implement before real users arrive.
What API security means for a small product
API security means five questions on every route that touches user data: who is calling, whether they own the object, whether the input is the shape you accept, whether the rate is reasonable, and whether the JSON includes only intended fields. Answer them in the handler, even when a gateway sits in front.
List public routes such as health, login, signup, and published pages. Every other route requires a verified caller. Write that list in the README so the next endpoint does not ship open because a sample did.
Five checks on every sensitive endpoint
Apply the five checks in order and stop at the first failure. A failed login, a failed ownership check, or a failed validation should return before any write runs. The table is the contract. The sections under it are how to implement each row.
| Check | Question | Failure response |
|---|---|---|
| Authentication | Is this a real session or token? | 401, no object data |
| Ownership | Does this caller own this id? | 404 (or 403), no object data |
| Validation | Is every field the type and length you accept? | 400 with field errors, no stack trace |
| Rate limit | Is this caller within the budget for this route? | 429 with a retry hint |
| Data exposure | Does the JSON include only intended fields? | Remove secrets, tokens, and other users' columns |
Authentication without ownership is the mistake that exposes user data even when login works. Read authentication versus authorization before you call the API "done" because the lock screen looks right.
Authenticate the caller
Accept a session cookie or a bearer token that your server issued or that a provider signed and you verified. Check the signature, the expiry, and the audience if you use JWTs. Do not decode a token and trust the payload without verification. Do not invent your own "user id" header that the client can set.
Login, refresh, and password reset are unauthenticated on purpose. Protect them with rate limits and, for reset, with a single-use token that expires. Webhooks are unauthenticated in the HTTP sense and authenticated by a signature over the raw body. Compare that signature with a constant-time function and the secret from server-side secret storage.
Check ownership
After you know the caller, load the row with both the object id and the caller id.
select id, title, body
from notes
where id = $1 and owner_id = $2
Take owner_id from the session. If the client sends an account id, ignore it for authorization. A missing row and a row owned by someone else should look the same to the caller, so guessing ids does not map your table. Admin routes need a role stored on the server, checked in the same place. "The front end is the admin app" is not a role.
Validate the input
Check types, lengths, ranges, and allowed enums on the server. An id should be the format you generate. A status field should be one of the states you support. An email should be a reasonable length, not a 2 MB string you will log forever.
Reject unexpected fields on write endpoints, or copy only the allowlist into the update. Mass assignment is how role=admin arrives in a profile form the UI never showed. For file uploads, limit size and content type, and store the object outside the web root with a generated name.
Rate limit the expensive and the sensitive
Limit the routes that send email, check passwords, create accounts, start payments, or call a paid API. Key the limit on the user id when you have one, and on IP for anonymous routes. Return 429 when the caller exceeds the budget. Put the same limit on search and on any route that fetches a user-supplied URL.
Return only the fields the client needs
Serialize a response type you defined. Password hashes, reset tokens, and other users' emails leak when the handler returns the whole model. If a list returns many rows, confirm each row passed the ownership check. Map unexpected errors to a generic 500 and a request id you can find in the logs.
Put the checks on a new endpoint
Use this sequence every time you add a route, including a route an assistant drops into an existing file. The point is to leave a test, an owner column, and a response type, not only a handler that returns 200 on the happy path.
- Decide if the route is public. Anything absent from that list requires a verified session.
- Name the object and the owner column. Add both to the query.
- Write the allowlist of fields the body may set.
- Add a rate limit if the route sends mail, touches auth, or calls a paid API.
- Define the response type and test it. Confirm hashes and tokens are absent.
- Call the route as user A with user B's id. Confirm a 404 and an empty body.
- Call it with a bad token, a huge string, and an extra
rolefield. - Read the log line. It should have a request id and the user id, and it should not have the token.
Document the route in one line: method, path, auth, owner column. The pre-launch checklist is the moment to run the two-user test across the whole table.
A notes API that leaked through the list route
A solo builder shipped a notes app. The detail route checked owner_id. The list route was written later, in a hurry, as select * from notes order by updated_at desc limit 50. The home screen only rendered the current user's notes because the client filtered. The network response contained everyone's titles.
The fix was a where owner_id = $1 on the list, a serializer that dropped body from the preview, and a test that counted rows for user A and asserted none of them belonged to user B. The detail route had been "secure" for a week. The list route was the product.
Run the same review on exports, search, and recent activity. A collection endpoint needs its own ownership check.
FAQ
Is a secret API key enough to protect user data?
A server-to-server key proves that the caller possesses that key. It does not prove which end user is asking, and it does not prove they own the id in the path. Mobile and browser apps cannot keep a privileged key secret. Use a user session for user data, and keep partner keys on your server. Then still check ownership.
Should I use 403 or 404 when a user requests someone else's object?
Prefer 404 when the existence of the object is itself sensitive, which is the usual case for private notes, invoices, and files. Use 403 when the caller is allowed to know the object exists and is simply not allowed to change it, such as a shared document they can see but not delete. Pick one rule per resource and apply it on every method.
Where should rate limits live?
Start in the application on the routes that hurt if abused: login, reset, signup, and paid third-party calls. A platform or reverse proxy can add a coarse limit in front. Limits in the UI, such as disabling a button, do not count. The caller can ignore your JavaScript.
Do GraphQL and RPC need a different model?
The same five checks apply to each field or procedure that reads user data. A single gateway authentication check is not ownership. Disable introspection and overly broad queries in production if you do not need them, and set a maximum query depth or cost so one request cannot fan out forever.
What to do next
Write down your public routes. For every other route, confirm a verified caller, an owner constraint, an allowlisted body, and a response type that omits secrets. Swap ids between two test users and watch the raw response, not the rendered screen.
If you want a recurring look at new endpoints, StaySecure scans the code and explains findings a builder can act on. Use that as a backstop for API security, and keep the two-user request test whenever a route starts accepting an id.