September 5, 2025

API Security: The Complete Guide 2025

Photo of the author of the blog post
Buchi Reddy B

CEO & Founder at LEVO

Photo of the author of the blog post
Levo API Security Research Panel

Research Team

API Security: The Complete Guide

TL;DR

APIs are the backbone of products, partners, and automation. The biggest risks come from missing object-level authorization, shadow or zombie endpoints, weak token handling, and data leakage. Win by treating API security as a lifecycle: design with least privilege, test like an attacker, validate request and response schemas at runtime, enforce policy at edge and service, monitor drift and MTTR, and prove outcomes with evidence. Use the templates, code, policies, and tests below.

Why APIs, and how we got here

Software moved from single apps to networks of services, mobile clients, partner ecosystems, and automation. Teams needed:

  • A stable contract between producers and consumers
  • Loose coupling so teams can ship independently
  • Internet scale and polyglot stacks
  • A path to expose capability without sharing internals

New context to include in 2025

  • Multi-tenant isolation must be an explicit design choice, not an afterthought.
  • Mobile and IoT clients cache credentials and work on lossy networks.
  • Third-party SaaS calls add another trust boundary; their outages and policies become your risk.

APIs solved this by making capabilities addressable resources with predictable verbs and formats, versioned contracts, and independent lifecycles.

What APIs replaced, and why that matters for security

Earlier approach Why replaced Old posture New API risk
Direct DB access Tight coupling Few chokepoints Many internet endpoints → object-level auth mandatory
Proprietary SDKs Lock-in, slow Security hidden Public contracts → easy fuzzing
ESB/SOAP hubs Slow governance Central choke Edge sprawl, faster shipping
Screen scraping Fragile WAFs/sessions Stable APIs easier to probe
Batch/FTP Not real time Narrow windows Always-on tokens → continuous risk

Translation: we moved from “few chokepoints inside a trusted LAN” to “many internet-facing, fast-changing machine interfaces.” Authorization, schema validation, discovery, and evidence now matter most.

What is API security

API security is the set of design choices, controls, tests, and runtime protections that keep API capabilities and data safe without breaking delivery speed. It spans identity, authorization, input and output validation, data protection, abuse prevention, discovery, observability, and governance across the full lifecycle.

Why API security is needed

APIs are the product interface, the partner interface, and the automation interface. They sit on the internet, change quickly, and expose high-value data and actions. Traditional perimeter controls were built for web pages and humans, not machine traffic.

What breaks without it

  • Account and data exposure, missing object-level checks on reads and writes
  • Silent leakage, verbose errors and logs reveal secrets and PII
  • Fraud and abuse, webhook replays, idempotency gaps, business-logic bypass
  • Outages and blast radius, no rate limits or backpressure
  • Compliance findings, no evidence that sensitive fields are protected or access is justified

Business and technical drivers

Business

  • Revenue protection on checkout, billing, and account flows
  • Customer trust and faster questionnaires
  • Contractual and regulatory duty with provable controls
  • Efficiency with clean contracts and reusable policy

Technical

  • Zero trust on every hop
  • API sprawl and spec drift
  • Polyglot styles, REST, GraphQL, gRPC, webhooks
  • Multi-cloud and third-party integrations
  • Residency and sovereignty for data and telemetry
  • Software supply chain for SDKs, auth libraries, base images

AI and agent reality

  • Agents call APIs at speed; mistakes scale quickly
  • Prompt injection becomes API misuse
  • Vector stores and RAG introduce sensitive embeddings and metadata
  • Constrain tool use, scrub prompts from logs, and watch vector-store access for PII

Stakeholder map

Stakeholder Currency KPI
CEO/CFO Trust, predictability Incident count, churn after incidents
CISO Coverage, evidence % endpoints owned, drift-to-detect, audit pass rate
Platform/SRE SLOs, budgets 4xx/5xx by route, rate-limit hits, MTTR
Product Safe velocity Breaking changes avoided, secure defaults usage
Engineering Simplicity/speed Time to add secure endpoint, test coverage
Privacy/Legal Lawful, limited processing PII in logs ↓, deletion SLA

Minimum viable control set (MVCS)

  • Design, classify data, contract-first specs, ownership and version policy
  • Build, secrets out of code, strict types, securitySchemes and scopes in the spec
  • Test, negative tests for overposting and IDOR, fuzz encoders, GraphQL depth and cost checks
  • Ship, canary with schema shadow validation, non-breaking changes first
  • Runtime, token validation, object-level checks, request and response schema validation, rate limits, replay guards
  • Govern, policy as code, evidence capture, quarterly control reviews with SLOs
  • Input canonicalization, normalize Unicode and parameters to avoid signature mismatches
  • Secure defaults, templates and scaffolds that bake in the above

30-day proof checklist

  • Discover active endpoints and owners, build an API-BOM
  • Enforce JWT audience and issuer checks, shorten token lifetime
  • Add object-ownership checks on high-risk reads and write
  • Turn on rate limits and request normalization at the edge
  • Mask PII in logs, verify webhook signatures with short replay window
  • Disable GraphQL introspection in production and move to persisted queries
  • Add a “top five risky routes” dashboard with owners and violation budgets
  • Track drift time to detect and MTTR for API incidents

Success metrics to report

  • Coverage, % endpoints with owner, % routes with schema and auth
  • Protection, BOLA incidents per quarter, schema violation rate, replay blocks
  • Speed, drift time to detect, time to fix broken auth, MTTR
  • Privacy, PII exposure in logs and responses trending down
  • Cost predictability, stable unit cost across services and environments

How APIs fail in 2025 and attacker flow

  • Broken object-level authorization
  • Missing token checks, long-lived keys, confused deputy
  • Mass assignment and oversize payloads
  • Spec drift and shadow APIs
  • Data leakage through verbose errors and logs
  • Automation and bots that bypass business logic
  • OAuth pitfalls, insecure redirects and callback URLs

Attacker flow: recon → enumerate endpoints and verbs → probe auth and ownership → abuse pagination and rate → extract data or pivot.

Risk frameworks you must know

  • OWASP API Security Top 10 (2023) as baseline
  • NIST SP 800-204A/B microservices patterns
  • FAPI for high-risk OAuth and OIDC use cases

API types and specific risks

  • REST, watch BOLA, mass assignment, permissive filters
  • GraphQL, depth and cost, introspection, per-field auth, n+1
  • gRPC, mTLS, method allowlists, deadlines, message size
  • Webhooks and events, signatures, replay windows, idempotency
  • WebSockets/SSE, auth revalidation, framing, backpressure
  • SOAP/legacy bridges, XML parser limits, XXE prevention

Security architecture patterns that work

Internet-facing

  • Gateway or WAAP for TLS, rate limits, schema checks, JWT validation
  • Central IdP for OAuth 2.1 and OIDC
  • Token exchange for service calls
  • Sensitive routes with step-up checks

Service-to-service

  • mTLS between workloads, SPIFFE identities
  • Policy engines for ABAC or ReBAC near data
  • Sidecar or node agent for telemetry and deny-by-default

Cross-cutting

  • Decision logging with correlation IDs at edge and service
  • Data lineage for sensitive fields across hops

Lifecycle guide

Design

  • Data classification per field and route
  • Contract-first spec with security schemes and scopes
  • Owners per service, route, version
  • Versioning and deprecation policy
  • Multi-tenant isolation model and threat table

Outputs: data map, threat model table

Build

  • Secrets out of code with rotation
  • Standard token libraries
  • Strong types, block unknown fields by default
  • Policy bundles for gateway and mesh checked in

Outputs: service template, policy bundle

Test

  • Contract tests and negative tests for cross-tenant access, overposting, expired or wrong-aud tokens
  • Fuzz parsers and encoders; file upload limits; GraphQL depth and cost
  • Pagination denial tests

Outputs: negative test list, fuzz corpus

Ship

  • Canary with shadow contract validation
  • Promotion gates tied to violation budgets
  • Deprecation headers and timetable

Outputs: canary plan, rollback rule

Runtime

  • Validate iss/aud/exp on every call
  • Ownership checks at read and write
  • Rate limits on writes and canonicalization
  • Webhook HMAC with timestamp and short window
  • Response schema validation for PII leaks
  • Dashboards for 4xx/5xx by route and principal

Outputs: route dashboards, replay histograms

Govern

  • Policy and evidence in version control
  • Weekly evidence export with timestamps
  • Public security page updated quarterly

Outputs: evidence pack, change log

Identity, authentication, and authorization

OAuth 2.1 and OIDC in practice

  • Prefer short-lived access tokens; rotate refresh tokens
  • Enforce audience and issuer checks; consider jti for reuse detection
  • Use token exchange for narrow audience between services

Node Express JWT validation

JS
import express from "express";
import jwt from "jsonwebtoken";
import jwksClient from "jwks-rsa";
const app = express();
const client = jwksClient({ jwksUri: "https://idp.example.com/.well-known/jwks.json" });
const ISS = "https://idp.example.com/";

function getKey(h, cb){ client.getSigningKey(h.kid, (e,k)=>cb(e, k.getPublicKey())); }
function verifyJwt(requiredAud){
  return (req,res,next)=>{
    const t=(req.headers.authorization||"").replace("Bearer ","");
    if(!t) return res.status(401).json({error:"missing_token"});
    jwt.verify(t, getKey, {algorithms:["RS256"], issuer:ISS}, (err, dec)=>{
      if(err || dec.aud!==requiredAud) return res.status(401).json({error:"invalid_token"});
      req.user=dec; next();
    });
  };
}

Object-level authorization

JS
app.get("/v1/accounts/:id", verifyJwt("api://payments"), async (req,res)=>{
  const acct = await db.accounts.findById(req.params.id);
  if(!acct || acct.tenant_id!==req.user.tid || acct.owner!==req.user.sub)
    return res.status(403).json({error:"forbidden"});
  res.json(acct);
});

Extra patterns to know

  • DPoP/PoP tokens bind tokens to a client key to reduce replay
  • ReBAC for relationship-based access; evaluate near data
  • Service-to-service, avoid confused deputy by using new aud per downstream and optional act claim

Data protection and privacy

  • TLS 1.2+ with HSTS
  • Mask PII in logs and responses
  • Tokenize high-risk fields; encrypt at rest with key separation
  • Telemetry guard, reject logs containing risky fields
  • Retention defaults, start with 7 days for debug logs
  • Residency, keep payload inspection in region; export derived signals only

Redacted log example

JSON
{"event":"api.request","path":"/v1/payments","method":"POST","principal":"a1f3","body":{"card_number":"****-****-****-1234","cvv":"***","amount":12000}}

Abuse prevention and reliability

  • Request schema validation and normalization
  • Per-principal rate limits and quotas
  • Circuit breakers and deadlines
  • Replay protection using HMAC and short windows
  • Safe pagination with opaque cursors
  • Caching rules for private data

Envoy rate limit fragment

YAML
typed_per_filter_config:
  envoy.filters.http.ratelimit:
    "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
    domain: "public-api"
    rate_limited_as_resource_exhausted: true

Normalization rules to consider
Trim whitespace, lower case case-insensitive fields, sort keys, deduplicate params, bound arrays and strings, normalize Unicode to NFC.

API discovery and inventory

Combine specs with traffic discovery to build an API-BOM and flag zombie versions and ownerless endpoints.

API-BOM fields

PGSQL
service,path,method,version,owner,data_class,lawful_basis,
auth_type,
pii_fields,region,deps,last_seen,contract_ref,risk,deprecation_date

Testing playbook

Contract and negative tests

YAML
openapi: 3.1.0
info: { title: Payments API, version: "1.2.0" }
components:
  securitySchemes:
    oauth2:
      type: oauth2
      flows:
        clientCredentials:
          tokenUrl: https://idp.example.com/oauth2/token
          scopes: { payments.read: Read payments, payments.write: Create payments }
security:
  - oauth2: [payments.read]
paths:
  /v1/payments:
    post:
      security: [{ oauth2: [payments.write] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PaymentCreate'

Overposting test

BASH
curl -i -X POST https://api.example.com/v1/payments \
 -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
 -d '{"amount":12000,"currency":"USD","status":"paid","owner_id":"someone-else"}'
# Expect server to ignore or reject status and owner_id

Pagination denial tests
Try sequential IDs and unbounded limit, expect 400 or capped results.

File upload tests
Wrong MIME, oversize payload, double encoding, expect 4xx.

Observability for security and detections

Log decisions with correlation IDs, principal, route, decision, and reason. Emit high-cardinality metrics for 4xx/5xx by route and principal.

Detection ideas

  • 403 spikes by route and principal
  • Sequential object ID access
  • Tokens reused across services (join on jti)
  • Schema-violation bursts by route or version
  • Webhook timestamp skew and repeated event IDs
  • GraphQL introspection attempts in production

Compliance mappings you can reuse

Requirement theme What to implement Evidence to keep
PCI DSS 4.x scope and encryption TLS, key management, field protection Configs, rotation logs, cipher suites
SOC 2 Security and Availability Authentication, authorization, rate limits, SLOs Policy as code, CI logs, SLO dashboards
ISO 27001 Annex A (2022) Secure dev lifecycle, logging and monitoring Policies, pipeline evidence, log configs
GDPR minimization and rights Masking, tokenization, retention, deletion Data maps, retention jobs, DSR proofs
DPIA and ROPA Risk assessment for high-risk processing DPIA template, Article 30 records

Tooling landscape and buyer’s checklist

Landscape

  • Gateways and WAAP
  • Service mesh
  • API security platforms for discovery, drift, sensitive-data tracing
  • Test and fuzz tools

Buyer checklist

  • Discover shadow endpoints by traffic and specs
  • Validate requests and responses in real time without exporting payloads
  • Trace sensitive fields across services and logs
  • Produce auditor-ready evidence on demand
  • Predictable pricing across services, environments, regions
  • Cover REST, GraphQL, gRPC, webhooks, AI endpoints

Two buyer tests

  • One-flow proof converting findings into policy and CI tests within a week
  • Residency story that avoids raw payload export or documents safeguards

Secure by example

REST, response schema validation (FastAPI)

PYTHON
from fastapi import FastAPI
from pydantic import BaseModel, Field, StrictStr

app = FastAPI()

class PaymentOut(BaseModel):
    id: StrictStr
    status: StrictStr
    amount: int = Field(ge=1)
    currency: StrictStr

@app.get("/v1/payments/{id}", response_model=PaymentOut)
def get_payment(id: str): ...

GraphQL hardening

Schema fragment:

GRAPHQL
type Query {
  me: User!
  account(id: ID!): Account!
}

Server controls: persisted queries only, disable raw POSTs, depth and cost limits, per-field auth, disable introspection in production.

gRPC with mTLS

proto:

PROTO
syntax = "proto3";
package ledger.v1;

service Ledger {
  rpc GetBalance(GetBalanceRequest) returns (GetBalanceResponse) {}
}
message GetBalanceRequest { string account_id = 1; }
message GetBalanceResponse { int64 cents = 1; string currency = 2; }

Server note: SPIFFE IDs, method allowlists, deadlines, max message size, method-level RBAC.

Webhook verification (Python)

PYTHON
import hmac, hashlib, base64, time
def verify(sig_header, payload, secret):
    ts, sig = sig_header.split(",")
    if abs(time.time() - int(ts)) > 300: return False
    mac = hmac.new(secret.encode(), f"{ts}.{payload}".encode(), hashlib.sha256).digest()
    return hmac.compare_digest(base64.b16encode(mac).decode().lower(), sig.lower())

Opaque cursor pagination

BASH
GET /v1/orders?cursor=eyJvZmZzZXQiOjEwMCwic29ydCI6ImRlc2MifQ

Deep dives and edge cases

  • CORS, avoid * with credentials; handle preflight correctly
  • SSRF, deny internal ranges; outbound allowlists
  • Content negotiation, pin Content-Type, reject unknown types
  • Caching for private data, Cache-Control: private, no-store; vary on Authorization when necessary
  • Unicode normalization, normalize to NFC before comparing or signing
  • Enumeration resistance, opaque IDs, hidden counts, cursor pagination

Incident response for API breaches

Hot patch checklist

  • Shorten token TTL; rotate keys
  • Raise write thresholds; enable stricter normalization
  • Flip risky routes to soft-block; temporary allowlists if needed
  • Publish a customer notice plan

Forensics checklist

  • Freeze logs, traces, configs
  • Export point-in-time API-BOM
  • Snapshot policy bundles and mesh configs with signatures
  • Identify subjects and data classes impacted; notify per jurisdiction

Metrics, SLOs, and maturity model

Lead, spec coverage, endpoints with owners, drift time to detect
Lag, BOLA incidents, MTTR, false positive rate
SLOs, auth decision success rate, schema violation budget, time to rotate keys

Maturity model

  • Level 0, unknown endpoints, ad hoc fixes
  • Level 1, inventory and token checks on critical routes
  • Level 2, ownership checks on money and identity, rate limits, normalization, basic tests
  • Level 3, runtime contract validation, negative tests in CI, automated evidence
  • Level 4, policy as product, violation budgets, red-team findings converted to tests in a week

Case studies you can adapt

Case 1, BOLA on order detail
Request: GET /v1/orders/12345 with valid token
Fix: enforce tenant and subject ownership, opaque IDs, deny tests

Case 2, Mass assignment
Request includes role=admin in user update
Fix: strict schemas, ignore or reject unexpected fields, negative tests

Case 3, Webhook replay
Attacker replays signed event
Fix: HMAC with timestamp, short window, idempotency and last-seen store

Case 4, Caching leak
Shared cache returned previous user’s response
Fix: Cache-Control: private, no-store, vary on Authorization, or disable caching for sensitive routes

AI era APIs and agent traffic

  • Validate content type and size on LLM endpoints; scrub prompts from logs
  • Constrain tool use with allowlists and verb bounds
  • Output guard with size caps and schemas
  • Vector store access from approved services only; redact PII before embedding

Appendices, templates, and checklists

PR security checklist

  • Contract updated and linted
  • Token checks present (iss, aud, exp)
  • Ownership check on each sensitive read/write
  • Strict request and response schemas
  • Limits and normalization on writes
  • Webhook signature and replay window
  • Logs mask PII and include correlation IDs
  • Negative tests added or updated
  • Evidence artifacts updated

API-BOM CSV headers

PGSQL
service,path,method,version,owner,data_class,lawful_basis,auth,
pii_fields,
region,deps,last_seen,contract_ref,risk,deprecation

Runbook snippets

  • Rotate keys, who approves, how to test, rollback
  • Customer notice templates with timelines and scope

Page UX and SEO tips

  • Sticky table of contents and code tabs for Node, Python, Go
  • Copy buttons on code blocks
  • FAQ schema (JSON-LD) for rich results
  • Anchor links on headings

Introduction to Levo, how we operationalize this guide

Why teams pick Levo

  • Privacy-first by design. Sensitive payloads stay inside your perimeter and region. We observe behavior and enforce contracts without exporting raw data.
  • Fix-first workflow. Findings turn into policies, CI tests, and pull requests so issues are resolved and stay resolved.
  • Runtime truth, zero code change. An OS-level sensor gives protocol-agnostic visibility (REST, GraphQL, gRPC, webhooks, AI/tooling endpoints) and contract validation in real time.
  • Scale-agnostic, predictable cost. Pricing remains stable across environments, regions, and traffic growth, so success isn’t penalized.

Where Levo fits (Design → Build → Test → Ship → Runtime → Govern)

  • Design: Auto-discover endpoints from traffic + specs to build an API-BOM (owner, data class, auth type, last seen).
  • Build: Golden templates and policy bundles (gateway/mesh) so the secure path is the easy path.
  • Test: Generate negative tests (IDOR/BOLA, overposting, wrong/expired audience) and wire them into CI as non-flaky gates.
  • Ship: Shadow contract validation on canaries; promotion tied to violation budgets.
  • Runtime: Enforce token and object-level checks, schema validation (request and response), replay guards, limits, normalization, without payload export.
  • Govern: Continuous evidence packs (configs, test results, dashboards) that auditors accept; publish-ready progress for your security page.

90-day adoption path

  • Days 1–30: API-BOM from traffic, shorten token TTLs, enable write-route limits, mask PII in logs.
  • Days 31–60: Object checks on money/identity flows; CI negatives turned on; GraphQL to persisted queries with depth/cost caps.
  • Days 61–90: Retire zombie versions, automate evidence for PCI/SOC2/GDPR, and publish a deprecation timetable.

Proof you can show

  • Fewer BOLA incidents and blocked replays on high-risk routes.
  • Drift-to-detect and MTTR trending down.
  • Questionnaire cycles shorter with automated evidence.
  • Stable spend as customers, partners, and regions grow.

See it in practiceBook a demo to walk through discovery → policy → tests → runtime enforcement on your most critical flows, end-to-end.

Glossary

  • Access token – Credential a client presents to an API to act on behalf of a subject.
  • ABAC – Authorization using attributes of subject/resource/action/context.
  • ACME – Protocol for automated certificate issuance (e.g., Let’s Encrypt).
  • Actor (act claim) – Delegation indicator inside tokens for token exchange.
  • API-BOM – Inventory of APIs with path, method, owner, data classes, auth type, last seen.
  • Backpressure – Slowing/rejecting requests when downstream is constrained.
  • Bearer token – Token usable by whoever holds it; protect with TLS and short TTLs.
  • BFLA – Missing function-level authorization on privileged actions.
  • BOLA / IDOR – Object access without ownership checks.
  • BOPLA – Missing property-level checks within permitted objects.
  • CORS – Browser policy controlling which origins can read API responses.
  • CSP / SRI – Browser defenses to restrict sources and verify integrity.
  • DPoP / PoP – Bind a token to a key the client proves it holds; reduces replay.
  • DPIA / ROPA – Privacy artifacts: impact assessment and processing records.
  • FAPI – OAuth/OIDC security profile for high-risk financial APIs.
  • gRPC – High-performance RPC over HTTP/2 using Protocol Buffers.
  • GraphQL – Typed query language/runtime; watch depth/cost and per-field auth.
  • HMAC – Hash-based message authentication, common for webhook signatures.
  • HSTS – Policy header that forces HTTPS in browsers.
  • HTTP Message Signatures – Standard to sign headers/body for integrity.
  • Idempotency key – Client-supplied unique value to avoid duplicate processing.
  • JAR / JARM / PAR – OAuth hardening for signed requests/responses and pushed requests.
  • JOSE / JWS / JWE / JWK / JWKS – JSON standards for signing/encryption and keys.
  • JTI – Unique token identifier to detect replay.
  • JWT – Compact token format carrying signed claims.
  • Least privilege – Only the minimal permissions required are granted.
  • mTLS – Mutual TLS; both client and server authenticate with certificates.
  • NFC (Unicode) – Canonical text normalization to avoid look-alike bypasses.
  • OAuth 2.1 – Consolidated OAuth profile (PKCE, no implicit flow).
  • OIDC – Identity layer on OAuth; adds ID Tokens and standard claims.
  • OPA / Rego – Policy engine & language for authorization decisions as code.
  • Opaque token – Non-parseable token that requires introspection at issuer.
  • OpenAPI – Machine-readable spec for HTTP APIs; v3.1 aligns with JSON Schema.
  • OpenTelemetry – Vendor-neutral traces, metrics, and logs.
  • Overposting (mass assignment) – Client sends fields not intended; block in schema.
  • PCI DSS – Cardholder data security standard for systems handling payments.
  • PII – Data that identifies a person, alone or in combination.
  • Quota – Longer-window allocation of requests or resource units per identity.
  • RBAC – Role-based access control.
  • ReBAC – Relationship-based access (e.g., org → team → resource).
  • Replay attack – Resending a valid request/token; defend with nonces & timestamps.
  • Request normalization – Canonicalize input before validation and signing.
  • Response schema – Enforce response shape to avoid leaking PII/internal fields.
  • Schema enforcement – Block or normalize requests/responses that don’t match the contract.
  • Scope – Named permission limiting token use.
  • Service mesh – Layer providing mTLS, traffic policy, and telemetry across services.
  • SPIFFE / SPIRE – Workload identity standard and its production implementation.
  • Spec drift – Behavior diverges from documented contract.
  • SSRF / XXE / ReDoS – Common classes of server-side and parser attacks.
  • TLS – Transport encryption; use modern ciphers and disable weak ones.
  • Token exchange – Swap one token for another with narrower audience/scope.
  • UMA 2.0 – Delegated user-managed access profile on top of OAuth.
  • WAAP – API-aware edge protection (schema checks, bot/DDoS, anomaly detection).
  • Webhook signatures – HMAC or HTTP Message Signatures to verify origin & content.

ON THIS PAGE

We didn’t join the API Security Bandwagon. We pioneered it!