# Rate limits

> Per-credential request limits for the NorthechPay API, the headers that report them, and how to back off after a 429.

Limits are counted per credential: each API key, and each signed-in user, has its own budget. Each limit counts requests in a one-minute window.

| Limit             | Applies to                                                                    | Default        |
| ----------------- | ----------------------------------------------------------------------------- | -------------- |
| General API       | Every authenticated `/api/v1` request                                         | 120 per minute |
| Checkout creation | `POST /merchant/checkouts` and `POST /merchant/payment-links`, together       | 30 per minute  |
| Dispute actions   | `POST …/evidence`, `DELETE …/evidence/{id}`, `POST …/submit`, `POST …/accept` | 20 per minute  |
| Secure onboarding | `POST /merchant/onboarding/drop-session` and `drop-account`                   | 20 per minute  |
| Sign-in           | `POST /auth/token`, counted per IP address                                    | 10 per minute  |

The general limit and the checkout creation limit can be changed by the platform operator. The others are fixed. Requests on a stricter route count toward both that limit and the general one.

## Headers

Successful responses carry the remaining budget of the tightest limit that applies:

```http
HTTP/1.1 200 OK
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 117
X-Request-Id: 6f1d2c3b-4a5e-4f60-9a7b-8c9d0e1f2a3b
```

When you go over a limit you get `429`:

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

{"error":{"code":"rate_limited","message":"Too many requests. Retry after the indicated delay.","details":{},"request_id":"…"}}
```

`Retry-After` is in seconds.

## Back off correctly

- Wait at least `Retry-After` seconds before the next request with the same credential.
- Retry a create with the **same** `Idempotency-Key`. A `429` means the request was not processed, but reusing the key is always safe.
- Do not retry in a tight loop. Add jitter if several workers share one key.

```js
async function withRateLimitRetry(send, attempts = 3) {
    for (let attempt = 1; ; attempt++) {
        const response = await send();
        if (response.status !== 429 || attempt === attempts) return response;
        const waitSeconds = Number(response.headers.get('Retry-After')) || 60;
        await new Promise((resolve) =>
            setTimeout(resolve, (waitSeconds + Math.random()) * 1000),
        );
    }
}
```

```php
use Illuminate\Http\Client\Response;

function with_rate_limit_retry(callable $send, int $attempts = 3): Response
{
    for ($attempt = 1; ; $attempt++) {
        /** @var Response $response */
        $response = $send();

        if ($response->status() !== 429 || $attempt === $attempts) {
            return $response;
        }

        sleep(((int) $response->header('Retry-After') ?: 60) + random_int(0, 1));
    }
}
```

The `send` callback must reuse the same idempotency key on every call.

## Stay under the limits

- Use webhooks instead of polling.
- In scheduled jobs, use `GET` instead of `POST …/refresh`, and spread the requests over the run.
- Use `per_page=100` when you page through lists.
- Use one API key per system, so a busy back-office job cannot starve your checkout.
