# How to protect APIs from bots

> APIs need controls that do not depend on a browser. Authenticate every client, set quotas per key, account and endpoint, attest mobile apps with Apple App Attest or Google Play Integrity, and watch TLS fingerprints and behaviour per token. Validate request schemas and the order of calls, so a client that skips the steps your real app takes stands out.

Category: Blocking bots in practice. Level: Advanced. Updated: 2026-11-24.
Canonical page: https://stopbots.io/learn/implementation/how-to-protect-apis-from-bots/

Bots often go straight to the API behind a website or mobile app. An API returns structured data, skips page rendering and often has weaker checks than the pages that call it. Mobile APIs are public by design: anyone can install the app on a phone they control and read every request it makes.

## What is OWASP API6:2023?

The OWASP API Security Top 10 calls this risk [Unrestricted Access to Sensitive Business Flows](https://api-security.owasp.org/editions/2023/en/0xa6-unrestricted-access-to-sensitive-business-flows/). Each request is valid and authorised, and the harm comes from doing it at machine scale: buying all the stock of a limited product, creating thousands of accounts, sending OTP messages to premium numbers, redeeming every referral bonus, or pulling a whole price catalogue. OWASP's advice is to identify those flows and add protections suited to each.

For each endpoint, ask what happens if a script calls it a million times with valid inputs. Purchase, signup, login, password reset, OTP send, coupon redemption, booking, review posting, search and pricing endpoints usually make the list. The OWASP automated threats catalogue, explained in [OWASP automated threats in plain English](https://stopbots.io/learn/bots/owasp-automated-threats.md), helps you name each attack. Volume and cost limits belong to a separate item, API4:2023 Unrestricted Resource Consumption.

## Why don't browser challenges work for APIs?

JavaScript challenges and CAPTCHAs need a browser that renders a page and runs a script. A mobile app's network layer, a partner's server or a command-line client gets an HTML challenge page where it expected JSON, and the call fails. That is why CDN bot settings that apply to a whole domain often break mobile apps.

Split your API traffic by client type:

- Calls from your own web pages can carry a token from a check that ran on the page, such as a Turnstile pre-clearance cookie or a bot vendor's session token, and the API checks that token.
- Calls from your mobile apps need app attestation and an SDK, covered below.
- Server-to-server calls from partners need strong authentication and per-key quotas, with no challenge at all.

## Authentication and per-key quotas

Give every client an identity: API keys for partners, OAuth or session tokens for users. Keep anonymous clients off expensive or sensitive endpoints.

Set quotas on several keys at once: per API key, per user account, per endpoint, and a global cap per endpoint that protects you when every other key rotates. Separate short burst limits from daily totals. Count failures as well as requests: 401 responses, declined payments and invalid coupon codes. The [rate limiting guide](https://stopbots.io/learn/implementation/are-rate-limits-enough-to-stop-bots.md) explains why IP-only limits fail against proxy pools.

Return a clear response when a limit trips:

```http
HTTP/1.1 429 Too Many Requests
Retry-After: 60
Content-Type: application/json

{"error": "rate_limited", "retry_after": 60}
```

Treat a key shipped inside a mobile app or a web bundle as public. Anyone can extract it, so it identifies an app version, not a trusted caller.

## How does mobile app attestation help?

Both mobile platforms let your server check that a request comes from your unmodified app on a genuine device.

[Apple App Attest](https://developer.apple.com/documentation/devicecheck), part of the DeviceCheck framework, has the app create a key pair in the device's Secure Enclave and ask Apple to attest it. Your server verifies the attestation once, stores the public key, and then checks a signed assertion on sensitive requests. The private key never leaves the hardware, so it cannot be copied into a script.

[Google Play Integrity](https://developer.android.com/google/play/integrity) returns a signed verdict about the app (is it the build you published), the device (does it pass Android integrity checks) and the Play licence. Your server requests the verdict with a nonce or request hash tied to the call, then verifies it with Google, which stops an attacker replaying an old verdict.

Rules for both:

- Attest at session start or on sensitive actions instead of on every call, since both services limit request volume and add latency.
- Bind the attestation to the request through a server nonce or a hash of the request body.
- Roll out in log-only mode first. Rooted phones, custom ROMs and old devices fail checks for reasons unrelated to fraud, and a hard block on day one will hit real users.
- Attestation shows that the device and app are genuine. A farm of real phones running your real app passes it, so keep behaviour checks.

## What can request signing do?

Many apps sign requests with an HMAC (a keyed hash) using a secret stored in the app. Signing with a timestamp and nonce stops simple replay and blocks clients built from captured traffic alone. It does not hold up against someone who reverse engineers the app, because the secret is in the binary. Rotate signing secrets with each app release, reject old app versions after a grace period, and where you can, sign with an attested hardware key instead of a shared secret.

## TLS fingerprinting and behaviour per token

Every HTTP client library opens a TLS connection in its own way: the cipher suites, extensions and their order. A TLS fingerprint such as [JA4](https://github.com/FoxIO-LLC/ja4) summarises that handshake. Your iOS app built on the system networking stack produces a small, stable set of fingerprints. A Python script that copies your app's headers produces a different one. List the fingerprints your real app versions produce and alert on traffic that claims to be the app but does not match. OS updates change fingerprints, so refresh the list with each release. [TLS fingerprinting with JA3 and JA4](https://stopbots.io/learn/detection/tls-fingerprinting-ja3-ja4.md) explains how these fingerprints are built.

Then track behaviour per token and per device:

- Requests per session and per minute, compared with what your app's screens can produce.
- How many accounts log in from one device, and how many devices use one account.
- How many IP addresses and countries one token appears from. A session token seen from dozens of networks in an hour has been shared or stolen.
- The time between calls. Real users pause on screens; scripts call at fixed or minimal intervals.

For login endpoints, [how to detect credential stuffing](https://stopbots.io/learn/account-attacks/how-to-detect-credential-stuffing.md) lists the signals.

## Validate schemas and call sequences

Validate each request against a schema, for example an [OpenAPI](https://spec.openapis.org/oas/latest.html) definition. Reject unknown fields, wrong types and missing headers your app always sends. Bots built from captured traffic often send fields from an older app version or forget a header the current app adds.

Then check the order of calls. Your real app follows a path: it fetches configuration, loads a product, creates a cart, then checks out. A bot often jumps straight to the last call. Making every client walk the full sequence forces an attacker to run your real app end to end, which costs far more than replaying one request. Enforce the steps on the server:

- Issue a token at each step that the next step requires, such as a cart ID created in the same session before checkout accepts it.
- Reject OTP send requests that do not follow the call that opens the login or signup screen in the same session; [SMS pumping fraud](https://stopbots.io/learn/account-attacks/sms-pumping-fraud.md) shows why this endpoint needs it.
- Flag sessions that call search or price endpoints thousands of times without ever viewing a product.

## Tools that help

_StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking._
- [Apple App Attest](https://developer.apple.com/documentation/devicecheck) lets your server check that requests come from your genuine iOS app on a real Apple device, and it is free to use.
- [Google Play Integrity](https://developer.android.com/google/play/integrity) gives your server a signed verdict on the Android app and device, free to use within a daily request quota.
- [Cloudflare API Shield](https://developers.cloudflare.com/api-shield/) adds schema validation and API discovery at the edge for APIs already behind Cloudflare.
- [Kairal](https://www.kairal.io/industries/saas-ai-apis/) evaluates API traffic through an API integration or at the edge, linking device, network and behavioural signals per endpoint for SaaS and AI APIs.

## Common questions

### Can you use a CAPTCHA on an API?

Not directly, because an API client has no page to show it on. Your own app can handle a challenge-required response by showing a challenge in its interface and sending the token back, but solving services can still produce tokens, so treat it as added cost for the attacker.

### What is OWASP API6:2023?

It is Unrestricted Access to Sensitive Business Flows in the OWASP API Security Top 10. It covers APIs that let automation buy stock, create accounts, post content or redeem offers at a scale the business never intended, even when every single call is valid.

### How do you detect bots in a mobile app?

Use platform attestation to check that requests come from your genuine app on a real device, then add rate and behaviour signals per device and per account. Attestation shows the app and device are genuine; it does not show a person is using them, so farms of real phones still need behaviour checks.

## Related

- [TLS fingerprinting for bot detection: JA3 and JA4 explained](https://stopbots.io/learn/detection/tls-fingerprinting-ja3-ja4.md)
- [Rate limiting for bots: what it stops and what it misses](https://stopbots.io/learn/implementation/are-rate-limits-enough-to-stop-bots.md)
- [SMS pumping fraud: when bots attack your OTP endpoint](https://stopbots.io/learn/account-attacks/sms-pumping-fraud.md)
- [OWASP automated threats: the 21 bot attacks in plain English](https://stopbots.io/learn/bots/owasp-automated-threats.md)
