# Errors, credits and rate limits

> The error format, what each status means, and how to handle 402 and 429.

## The error format

Every failure answers JSON with the HTTP status that fits:

```json
{ "error": "UPPER_SNAKE_CODE", "message": "..." }
```

Validation errors add an `issues` array. Branch on the HTTP status and the `error` code, never on `message`, which is for people.

| Status | Example `error` | What to do |
| --- | --- | --- |
| `400` | `VALIDATION_ERROR` | Fix the request; `issues` names the field (for example a `phone` outside 6 to 20 characters) |
| `401` | `AUTHENTICATION_FAILED` | The `x-api-key` was sent but isn't a working key: check it wasn't deleted, turned off or expired |
| `402` | `INSUFFICIENT_CREDITS` | The workspace has no credits left; add credits or a plan under **Billing** |
| `403` | `ACCESS_DENIED` | The key may not make this request, or no key was sent at all |
| `429` | | Too many requests; wait `Retry-After` seconds and retry |
| `500` | `INTERNAL_SERVER_ERROR` | Retry with backoff |
| `503` | | The request couldn't be served right now; it says nothing about your request, so send it again |

## Credits

Each lookup spends its tier's credits from the workspace of the key: 1 for `GET /phone/details`; 3 for `GET /phone/extra-details` and 6 for `GET /phone/full-details` on a NANP number in the NPA-NXX registry, 1 for any other number. A number that answers `valid: false` is still a lookup and spends its credits.

`402 INSUFFICIENT_CREDITS` is a billing signal, not a validation result: don't treat the number as invalid. Top up on the pricing page or turn on auto top-up; see [Plans, credit packs and auto top-up](https://support.phone-check.app/plans-and-billing/plans-credit-packs-and-auto-top-up).

## Rate limits

The API's [authentication guide](https://api.phone-check.app/developers/auth.md) gives each key's per-minute limit. Past it the API answers `429` with a `Retry-After` header in seconds. Wait that long, then retry:

```ts
async function lookup(phone: string) {
  const url = 'https://api.phone-check.app/phone/details?phone=' + encodeURIComponent(phone);
  const response = await fetch(url, { headers: { 'x-api-key': process.env.API_KEY ?? '' } });
  if (response.status === 429) {
    const seconds = Number(response.headers.get('retry-after') ?? '1');
    await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
    return lookup(phone);
  }
  return response.json();
}
```

For large lists, the [Bulk Checker](https://support.phone-check.app/bulk-validation/validate-a-csv-list) paces the checks for you.
