> ## Documentation Index
> Fetch the complete documentation index at: https://www.recraft.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Recraft API Errors: Status Codes and Retries

> How Recraft API errors look, which ones are known, and how to handle and retry them in your code.

A failed request returns a non-2xx HTTP status. The body explains the error, but its format varies: some errors return plain text, others JSON. Read the body as text first, and parse it as JSON only when `Content-Type` is `application/json`.

## Known errors

| Status | When | Body | Retry? |
| - | - | - | - |
| `400` | A parameter or size is invalid | `application/json`: `{"code": "invalid_request_parameter", "message": "..."}`. A request that can't be parsed returns `text/plain` | No: fix the request |
| `400` | Not enough API units | `application/json`: `{"code": "not_enough_credits", "message": ""}` | No: [buy units](https://www.recraft.ai/docs/api-reference/pricing.md) |
| `401` | The `Authorization` header is missing, or the token is invalid | `text/plain`: `request unauthorized` | No |
| `404` | The path doesn't exist, for example a typo in the endpoint | `text/plain`: `404 page not found` | No |
| `429` | Rate limit exceeded | `application/json`: `{"code": "rate_limit_exceeded", "message": "too many requests, try again later"}` | Yes, with backoff |
| `500` | Temporary server error | `text/plain`: `internal error, error_id: <id>` | Yes, with backoff |

JSON errors always have a `code` and a `message`. Use `code` to tell errors apart: two different errors can share the `400` status, and `message` can be empty. Other `400` codes exist, for example `prompt_is_improper` when the content filter blocks a prompt.

## Handle errors

```python Python theme={null}
import os

import requests

response = requests.post(
    'https://external.api.recraft.ai/v1/images/generations',
    headers={'Authorization': f"Bearer {os.environ['RECRAFT_API_TOKEN']}"},
    json={'prompt': 'two race cars on a track', 'model': 'recraftv4_1'},
)
if not response.ok:
    # The body is plain text or JSON: print it as text
    raise SystemExit(f'{response.status_code}: {response.text}')
print(response.json()['data'][0]['url'])
```

```javascript JavaScript theme={null}
// Node.js 18+, run as an ES module (file.mjs)
const response = await fetch('https://external.api.recraft.ai/v1/images/generations', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.RECRAFT_API_TOKEN}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ prompt: 'two race cars on a track', model: 'recraftv4_1' }),
});
if (!response.ok) {
  // The body is plain text or JSON: read it as text
  throw new Error(`${response.status}: ${await response.text()}`);
}
console.log((await response.json()).data[0].url);
```

## Retries

* **`400`, `401`, and `404`** mean the request needs a change: a token, a parameter, a size, or more API units. Fix it before sending again; retrying the same request gives the same result.
* **`429`** means you went over a rate limit: 100 images per minute or 5 requests per second per user. Wait and retry with exponential backoff, for example after 1, 2, and 4 seconds. The response has no `Retry-After` header. See [Limits and sizes](https://www.recraft.ai/docs/api-reference/appendix.md#rate-limits).
* **`500` and network timeouts** are usually temporary. Retry with the same backoff. If the error stays, send the `error_id` from the body to [support](mailto:help@recraft.ai).

## Related

> [card] [Limits and sizes](https://www.recraft.ai/docs/api-reference/appendix.md)
>
> Rate limits, sizes, prompt length, and input limits.

> [card] [Quickstart](https://www.recraft.ai/docs/api-reference/getting-started.md)
>
> Get a token and make a request that works.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.