Types of API Security and 9 Proven Ways to Protect Your APIs
API security covers several layers: encrypting traffic with TLS, authenticating callers with OAuth 2.0, API keys or mutual TLS, checking authorisation on every object and function, validating input, limiting request rates and payload sizes, routing traffic through an API gateway, logging and monitoring calls, keeping an inventory of every API version, and testing continuously.
Topic: Web & API Security in Cybersecurity & Ethical Hacking · Explainer
Quick answer: API security covers several layers: encrypting traffic with TLS, authenticating callers with OAuth 2.0, API keys or mutual TLS, checking authorisation on every object and function, validating input, limiting request rates and payload sizes, routing traffic through an API gateway, logging and monitoring calls, keeping an inventory of every API version, and testing continuously. The nine practices below map to the OWASP API Security Top 10 2023, where the leading risk is broken object level authorisation: an API returning another user's data because it never checked who owns the record.
Key takeaways
- Authorisation failures, not exotic exploits, cause most API data leaks.
- Check ownership on the server for every object ID a client sends.
- Follow RFC 9700 for OAuth: authorisation code with PKCE, no implicit flow.
- Old and undocumented API versions are a frequent blind spot.
- Rate limits protect against abuse and resource exhaustion.
Table of Contents
- What Are the Main API Risks?
- What Are the 9 Ways to Protect APIs?
- How Do You Implement Object-Level Authorisation?
- How Should Tokens Be Handled?
- What Does an API Gateway Add?
- How Do You Test API Security?
- References
What Are the Main API Risks?
| OWASP API 2023 | Risk |
|---|---|
| API1 | Broken Object Level Authorization |
| API2 | Broken Authentication |
| API3 | Broken Object Property Level Authorization |
| API4 | Unrestricted Resource Consumption |
| API5 | Broken Function Level Authorization |
| API6 | Unrestricted Access to Sensitive Business Flows |
| API7 | Server Side Request Forgery |
| API8 | Security Misconfiguration |
| API9 | Improper Inventory Management |
| API10 | Unsafe Consumption of APIs |
Source: OWASP API Security Top 10 2023.
What Are the 9 Ways to Protect APIs?
| # | Practice | Addresses |
|---|---|---|
| 1 | TLS 1.2 or 1.3 everywhere (RFC 8446) | Interception |
| 2 | Strong authentication: OAuth 2.0, mTLS for service-to-service | API2 |
| 3 | Object and property level authorisation on every request | API1, API3 |
| 4 | Function level authorisation (admin endpoints) | API5 |
| 5 | Schema validation of input and output | API3, injection |
| 6 | Rate limits, quotas and payload size limits | API4, API6 |
| 7 | Gateway with central policy | API8 |
| 8 | Inventory of all APIs and versions; retire old ones | API9 |
| 9 | Logging, monitoring and regular testing | All |
How Do You Implement Object-Level Authorisation?
Never trust an ID from the client. Look up the record and check it belongs to the caller:
# Python / Flask example
@app.get("/api/orders/<int:order_id>")
@login_required
def get_order(order_id):
order = Order.query.get_or_404(order_id)
if order.customer_id != current_user.id:
abort(404) # do not reveal that the order exists
return order.to_public_dict() # return only allowed fields
Returning only allowed fields also prevents excessive data exposure (API3). The OWASP REST Security Cheat Sheet lists more patterns.
How Should Tokens Be Handled?
- Use the authorisation code flow with PKCE and drop the implicit flow, as RFC 9700 recommends.
- Keep access tokens short-lived and restrict their scope and audience.
- Validate JWTs fully: signature, algorithm allowlist, issuer, audience and expiry, per RFC 8725.
- Consider sender-constrained tokens such as mutual-TLS bound tokens for high-value APIs.
- Never put API keys in mobile apps or front-end code.
What Does an API Gateway Add?
A gateway enforces authentication, rate limits, request size limits and schema validation in one place, and gives a single point for logging. It does not replace authorisation checks inside each service, because only the service knows who owns a record. NIST SP 800-204 describes gateway and service mesh patterns for microservices.
How Do You Test API Security?
- Build an inventory from gateway logs and OpenAPI files, including old versions.
- Test every endpoint with two accounts: can user A read or change user B's objects?
- Call admin functions as a normal user.
- Send oversized payloads and high request rates to check limits.
- Run automated scans in CI and periodic manual tests, with written authorisation.
Related: 20 API security tips and API testing with Postman. WebAsha offers API and web application security training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0