API security: who may call, what they may touch, how much
API security is the practice of protecting the interfaces that applications call directly: the endpoints behind mobile apps, single-page sites, partner integrations and services talking to each other. An API exposes data and actions in a form built for programs, which makes it easy to use at scale, for customers and for attackers alike. Most API failures are not exotic: an object anyone can read, a call nobody limited, an endpoint nobody remembered.
Quick facts
- What it protects
- Endpoints called by programs: mobile apps, front ends, partners, internal services
- Reference list
- OWASP API Security Top 10 2023, summarised on OWASP API Top 10
- First risk on that list
- Broken object level authorization: reading or changing another user's objects
- Where it is fixed
- Authorisation in the API code, limits on cost, and an inventory of endpoints
- What a filter adds
- Matching known attack shapes and limiting request rates; not object-level permissions
API security overlaps with web application security but is not the same: there is no page to hide a button on, so every check has to be on the server.
Why APIs need their own security thinking
A web page hides what a user cannot do. An API answers whatever is asked, in a format that is easy to automate. Identifiers in the path can be changed one by one; fields the interface never shows can be sent anyway; a call that is cheap for one user becomes expensive a million times over.
This is why the OWASP API list is dominated by authorisation and resource consumption rather than injection. It is also why API abuse often uses an endpoint exactly as designed.
Application programming interface security comes down to the three questions in this page's heading: who is calling, what may they touch, how much may they ask for. Authentication, authorisation and limits answer them in turn.
The main API security risks
- Broken object level authorization. Changing an identifier returns someone else's record; see BOLA.
- Broken authentication. Weak tokens, missing expiry, credentials that can be tried without limit.
- Broken property and function level authorization. Fields or admin functions reachable by ordinary users.
- Unrestricted resource consumption. No limits on rate, size or cost per call, which turns into outages and bills.
- Unrestricted access to sensitive business flows. Purchases, sign-ups or bookings automated at a scale the business never intended.
- Improper inventory management. Old versions and forgotten endpoints still answering.
Names follow the OWASP API Security Top 10 2023.
API security best practices
- Check object ownership on every call. Never trust an identifier because the client sent it.
- Authenticate every endpoint. Short-lived tokens, verified signatures, keys that can be rotated.
- Return only the fields the caller may see. And accept only the fields they may change.
- Limit rate, size and cost. Per key, per user and per endpoint; see limits by IP, user or key.
- Keep an inventory. Know every version and endpoint that answers, and retire the old ones.
- Validate input and log decisions. The OWASP REST Security Cheat Sheet covers the details.
How to secure an API, in order
To secure an API, take these steps in order.
- List every endpoint and version that answers. OWASP names the gap improper inventory management (API9:2023).
- Require authentication on all of them, by API key, bearer token or OAuth 2.0; name the few that are public on purpose.
- Add the authorisation checks that sit in code: object level (API1:2023), object property level (API3:2023) and function level (API5:2023).
- Set limits per key, per user and per endpoint.
- Put a request filter in front, in monitor mode first.
Authentication on API calls: bearer tokens and OAuth 2.0
An API call carries its credential in the Authorization request header. A key identifies the application that holds it. A bearer token stands for whoever holds it: any party in possession of the token can use it, and the header that carries it is defined in RFC 6750, which makes TLS mandatory for it.
OAuth 2.0 lets a third-party application obtain limited access to an HTTP service (RFC 6749). To secure an API with OAuth 2.0, give each client only the scopes it needs, and verify the token, its expiry and its scope on every call.
Broken authentication is what is left when these checks are skipped, as with tokens that never expire.
Keeping API keys secret
An API key is a password for a program, and it leaks the same ways: committed to a code repository, pasted into a ticket, written to a log, or shipped inside a mobile app or web page where anyone can read it. The OWASP Secrets Management Cheat Sheet covers storing keys in a secrets manager rather than in code or configuration files.
Give each key the narrowest scope that works, one key per client so a leak can be revoked without breaking everyone, and an expiry or rotation schedule. Scan repositories for keys before they are pushed, and treat any key that appeared in public, even briefly, as stolen.
APIs behind mobile and browser apps
Anything shipped to a phone or a browser can be read by the person holding it. A secret key embedded in an app identifies the app, not the user, and an attacker can extract it and call the API directly with whatever parameters the app never sends.
So the API has to treat every call as coming from an untrusted client: authenticate the user with short-lived tokens, check authorisation on the server for every object, and limit rates per user and per token rather than per app key. Calls made from browser pages also bring in CORS and CSRF. JSON bodies are a vector too: NoSQL injection as well.
An API security checklist
- Every endpoint requires authentication, or is listed as public on purpose.
- Every object access checks ownership, including updates and deletes.
- Every list has a page size limit, and every upload a size limit.
- Every key can be revoked without redeploying.
- Errors do not reveal internals, such as stack traces or query fragments.
- Old versions have an end date that is published and enforced.
A checklist only states intent. To see which items hold on a running API, compare the methods in api security testing.
API gateways, WAFs and what each one sees
An API gateway sits in front of services and handles routing, authentication and quotas per key. A web application firewall inspects the content of requests for attack shapes and limits rates. Both work on requests, so both are blind to whether user A should be allowed to read object 42.
That decision lives in the code. The gateway and the filter reduce volume and noise around it; web attacks covers the injection side that a filter does see.
Securing REST endpoints and the gateway
A secure RESTful API starts with these rules from the OWASP REST Security Cheat Sheet:
- Serve HTTPS endpoints only.
- Allowlist the permitted HTTP methods per endpoint.
- Keep passwords, security tokens and API keys out of the URL, where web server logs can capture them.
A secure API gateway adds one more: make it the only way in, so no caller skips its checks by reaching the backend.
Questions
What is API security?
API security is the practice of protecting the interfaces applications call directly: making sure every call is authenticated, every object access is authorised, and every endpoint has limits on rate and cost.
What are the biggest API security risks?
According to the OWASP API Security Top 10 2023, broken object level authorization comes first, followed by broken authentication, property-level authorization and unrestricted resource consumption.
Does a WAF protect APIs?
Partly. A WAF can match injection attempts and limit request rates, but it cannot tell whether a caller is allowed to read a particular object; authorisation has to be checked in the API.
What are API security best practices?
Check object ownership on every call, authenticate every endpoint, return and accept only allowed fields, limit rate and size, keep an inventory of endpoints, and validate input.