# Errors and rate limits

> API error codes (400, 401, 402, 403, 404, 413, 415, 429), the JSON error format, the rate limit of 120 requests per minute per organization, and how to retry.

A failed request gets a 4xx or 5xx status and a JSON body with a stable `code`, the HTTP `status` and a readable `message`:

```json
{
	"defined": false,
	"code": "UNSUPPORTED_MEDIA_TYPE",
	"status": 415,
	"message": "Unsupported file type. Send a JPEG, PNG, WebP, GIF, TIFF, BMP or AVIF image."
}
```

Branch on `status` or `code`. The `message` text may change.

## Error codes

| Status | `code` | When it happens | What to do |
| --- | --- | --- | --- |
| 400 | `BAD_REQUEST` | No image; both `file` and `url`; an empty file; an invalid URL or one that is not `http(s)`; invalid parameters. | Fix the request. Validation errors include details in `data.issues`. |
| 401 | `UNAUTHORIZED` | Missing, invalid or revoked API key. | Check the `Authorization: Bearer` header and the key. See [below](#missing-or-invalid-key). |
| 402 | `PAYMENT_REQUIRED` | The free image checks are used up (they do not renew), the last payment failed, or a spending cap was reached. | Read `data.reason` and `data.next`. See [below](#payment-required). |
| 403 | `INSUFFICIENT_SCOPE` | The API key lacks the permission this endpoint needs. | Ask an owner or admin to change the key's permissions. See [below](#missing-permission). |
| 404 | `NOT_FOUND` | No detection with that id for your organization. | Check the id. |
| 413 | `PAYLOAD_TOO_LARGE` | An upload over 25 MB. | Send a smaller image. |
| 415 | `UNSUPPORTED_MEDIA_TYPE` | The file is not a JPEG, PNG, WebP, GIF, TIFF, BMP, AVIF or MPO image. HEIC is not supported. | Convert the image to one of these formats. |
| 429 | `TOO_MANY_REQUESTS` | More than 120 requests in a minute from your organization, across all of its API keys. | Wait for the `Retry-After` time (60 seconds), then retry. |
| 5xx | `INTERNAL_SERVER_ERROR` and others | A failure on our side. | Retry with backoff. |

A validation error looks like this:

```json
{
	"defined": false,
	"code": "BAD_REQUEST",
	"status": 400,
	"message": "Input validation failed",
	"data": {
		"issues": [
			{
				"code": "too_big",
				"maximum": 100,
				"path": ["limit"],
				"message": "Too big: expected number to be <=100"
			}
		]
	}
}
```

## Missing or invalid key

A 401 carries a `WWW-Authenticate` header that points to the API's metadata ([RFC 9728](https://www.rfc-editor.org/rfc/rfc9728)), and `data.next` says how to get a key:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://api.ringingbytes.com/.well-known/oauth-protected-resource"
```

```json
{
	"defined": false,
	"code": "UNAUTHORIZED",
	"status": 401,
	"message": "Missing API key. Send it as `Authorization: Bearer <key>`. No key yet? See the agent guide at /auth.md.",
	"data": {
		"next": {
			"action": "get_api_key",
			"docs_url": "https://api.ringingbytes.com/auth.md",
			"resource_metadata_url": "https://api.ringingbytes.com/.well-known/oauth-protected-resource",
			"device_authorization_url": "https://api.ringingbytes.com/v1/agent/device/code"
		}
	}
}
```

An agent can follow `device_authorization_url` to ask a person for a key. See [Agents](https://ringingbytes.com/docs/agents#get-a-key).

## Payment required

A 402 always has `code: "PAYMENT_REQUIRED"`. `data.reason` says why:

| `data.reason` | When it happens | Who can fix it |
| --- | --- | --- |
| `free_checks_exhausted` | On Free, the free image checks are used. | Anyone: choose a plan, or an agent asks for a checkout link. |
| `payment_failed` | The last payment for the plan didn't go through. | A person, by updating the payment method. |
| `key_cap_reached` | The check would take the key over its monthly spending cap. | An owner or admin, by raising the cap. Otherwise it resets with the next billing period. |
| `org_cap_reached` | The check would take the organization over its monthly spending cap. | An owner or admin, by raising the cap. Otherwise it resets with the next billing period. |

`data.free_checks_remaining` is the number of free image checks left, `data.cap` describes the cap that was reached (`scope`, `monthly_cap_usd`, `spend_usd`, `resets_at`; otherwise null), and `data.next` has the next step and the links for it. [Agents](https://ringingbytes.com/docs/agents#when-a-check-returns-402) has a full example.

## Missing permission

Keys an agent got through the [device flow](https://ringingbytes.com/docs/agents#permissions) carry only the permissions a person granted. Calling an endpoint without its permission gets 403, with `error="insufficient_scope"` in the `WWW-Authenticate` header:

```http
HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer resource_metadata="https://api.ringingbytes.com/.well-known/oauth-protected-resource", error="insufficient_scope", scope="billing:checkout"
```

```json
{
	"defined": false,
	"code": "INSUFFICIENT_SCOPE",
	"status": 403,
	"message": "This API key does not have the \"billing:checkout\" permission. An owner or admin can change the key's permissions in the dashboard.",
	"data": {
		"required_scopes": ["billing:checkout"],
		"granted_scopes": ["checks:create", "account:read"],
		"next": {
			"action": "ask_human",
			"dashboard_url": "http://localhost:4101/api-keys",
			"docs_url": "https://api.ringingbytes.com/auth.md"
		}
	}
}
```

## Failed detections

If the API accepted a check but could not analyse the image, the response is still `200`, with `status: "failed"`, an `error` object and `credits: 0`. You are not charged.

```json
{
	"status": "failed",
	"error": { "code": "download_failed", "message": "..." },
	"credits": 0
}
```

`error.code` is one of:

| `error.code` | Meaning |
| --- | --- |
| `download_failed` | The URL could not be fetched. |
| `too_large` | The image is over 25 MB or 120 megapixels. |
| `unsupported_media` | The file is not a decodable image. |
| `bad_request` | The URL is not allowed (a private address, or a port other than 80 or 443). |
| `inference_unavailable` | The detector is not available right now. Retry later. |
| `inference_timeout` | The detector took too long. Retry later. |
| `interrupted` | The check was interrupted. Retry. |
| `internal` | A failure on our side. Retry later. |

## Rate limits

The API allows 120 requests per minute per organization, shared by all of its API keys and counted across every `/v1` endpoint. Creating more keys does not raise the limit. Over the limit, you get HTTP `429` with `Retry-After: 60` and `{ "code": "TOO_MANY_REQUESTS", … }`.

To stay under it:

- On `429`, wait for the `Retry-After` time before you retry. Retry `5xx` responses with exponential backoff and jitter, for example after 1, 2, 4 and 8 seconds.
- Spread batch jobs over time.
