# Conventions and errors

All Unspar API endpoints answer in the same JSON format and report errors the same way. One part of your code can handle all of them.

Web version: https://www.unspar.com/docs/api/conventions/

This page covers the rules that apply across every endpoint. Read it once before you build a client. The pages for individual areas, like [Articles](https://www.unspar.com/docs/api/articles/), only cover what is specific to them.

## What do requests and responses look like?

Every endpoint lives under `https://app.unspar.com/api/v1`. Pass your API token as a bearer token. You can create one in [Settings](https://app.unspar.com/settings) under **API tokens**, as shown in the [API overview](https://www.unspar.com/docs/api/).

| Part           | Rule                                                                                                                                                         |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Authentication | `Authorization: Bearer <token>` on every request.                                                                                                            |
| Request body   | JSON, with the `Content-Type: application/json` header.                                                                                                      |
| Response       | JSON, with the result inside `data`. There are two exceptions: the OpenAPI document returns YAML, and downloading an article's file returns the file itself. |
| Timestamps     | ISO 8601 with a time zone offset, such as `2026-12-01T08:00:00+00:00`. Always include the offset.                                                            |
| Request ID     | Every response includes an `X-Request-Id` header. Share it with [Support](https://app.unspar.com/support) if you report a problem.                           |

Here is what a successful response for a single project looks like:

```json
{
  "data": {
    "id": "01JX9Q3M4N5P6R7S8T9V0W1X2Y",
    "name": "My blog",
    "language": "en-GB"
  }
}
```

## What do IDs look like?

Most resources in Unspar use a 26-character public ID made of letters and digits, which you pass in URL paths and request bodies. A few IDs follow different formats:

| What                                                  | ID                                                       |
| ----------------------------------------------------- | -------------------------------------------------------- |
| Projects, articles, outlets, and most other resources | 26 letters and digits, like `01JX9Q3M4N5P6R7S8T9V0W1X2Y` |
| Teams                                                 | `team_...`                                               |
| API tokens                                            | `ak_...`                                                 |
| Built-in templates                                    | `template:builtin:general_guide`                         |
| Built-in voices                                       | `voice:builtin:friendly_explainer`                       |

IDs don't use type prefixes like `prj_` or `art_`. If you have seen those in older examples, they don't exist.

## How do I get the next page of a list?

List endpoints return one page at a time. You pick the page with two query parameters:

| Parameter  | What it does                    |
| ---------- | ------------------------------- |
| `page`     | The page number, starting at 1. |
| `per_page` | Items per page, from 1 to 100.  |

Alongside `data`, the response includes a `meta.pagination` object. In this example, there are three projects in total, and the first page returns two:

```json
{
  "data": [
    { "id": "01JX9Q3M4N5P6R7S8T9V0W1X2Y", "name": "My blog" },
    { "id": "01JXA2B3C4D5E6F7G8H9J0K1M2", "name": "My blog (German)" }
  ],
  "meta": {
    "pagination": {
      "current_page": 1,
      "page": 1,
      "per_page": 2,
      "total": 3,
      "last_page": 2,
      "from": 1,
      "to": 2,
      "has_more": true
    }
  }
}
```

Keep requesting the next page as long as `has_more` is `true`. Some lists also accept filters. The article list, for example, filters by `project` and `status` and sorts with `sort=-created_at`. Check each endpoint's reference page for its available filters.

## Repeat a request safely

If your connection drops while creating an article, you can't tell whether Unspar received the request. Sending it again could create a duplicate article and spend credits twice. An idempotency key prevents that.

An idempotency key is a unique label you pick for a single piece of work and send in the `Idempotency-Key` header. If the same API token sends the same request with the same key again within 24 hours, Unspar skips the duplicate work and returns the original response.

```sh
curl -X POST https://app.unspar.com/api/v1/articles \
  -H "Authorization: Bearer $UNSPAR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: flat-tyre-article-2026-12-01" \
  -d '{"project":"<project-id>","brief":"How to fix a flat bike tyre"}'
```

Keep these rules in mind, especially the last two:

| Rule                         | Detail                                                                                                                                |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Which requests               | Write requests: `POST`, `PUT`, `PATCH`, and `DELETE`.                                                                                 |
| Format                       | 1 to 255 visible ASCII characters, with no spaces.                                                                                    |
| How long                     | 24 hours, per API token.                                                                                                              |
| Same key, different body     | Returns `409 idempotency_conflict`. Use a fresh key for each new piece of work, and only reuse a key to retry the exact same request. |
| Creating or rotating a token | Omit the header. The response contains a new secret that Unspar never stores, so sending an idempotency key returns `422`.            |

The [CLI](https://www.unspar.com/docs/cli/) adds idempotency keys to its write commands automatically.

## What do the error codes mean?

Every error follows the same format. Your code can safely match on `code`, which stays stable, while `message` is written for humans. Some errors also include a `details` object. For `validation_failed`, `details` lists the specific fields that failed.

```json
{
  "error": {
    "code": "insufficient_credits",
    "message": "The team does not have enough credits for this operation."
  },
  "request_id": "..."
}
```

Most endpoints can return these codes:

| Code                                                    | Status   | What it means                                                                                                         |
| ------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `unauthenticated`                                       | 401      | No token was sent.                                                                                                    |
| `invalid_token`                                         | 401      | Unspar doesn't recognize the token.                                                                                   |
| `token_expired`, `token_revoked`                        | 401      | The token expired or was revoked. Create a new one.                                                                   |
| `legacy_key_unsupported`                                | 401      | An old token that can't call API v1. Create a new one.                                                                |
| `forbidden_ability`                                     | 403      | The token lacks permission for this endpoint.                                                                         |
| `forbidden_project_scope`                               | 403      | The token is restricted to other projects.                                                                            |
| `api_access_unavailable`, `capability_unavailable`      | 403      | The plan doesn't include this feature. Every plan includes the API today.                                             |
| `failed_authorization`                                  | 403      | Permissions changed while the request was starting.                                                                   |
| `resource_not_found`                                    | 404      | No matching resource exists in the projects this token can reach.                                                     |
| `route_not_found`, `method_not_allowed`                 | 404, 405 | The path or HTTP method is wrong.                                                                                     |
| `malformed_json`                                        | 400      | The request body isn't valid JSON.                                                                                    |
| `validation_failed`                                     | 422      | A field is missing or invalid. Check `details`.                                                                       |
| `plan_limit_reached`                                    | 422      | The request exceeds a limit on your plan.                                                                             |
| `insufficient_credits`                                  | 402      | Not enough credits. [Buy credits](https://app.unspar.com/credits/purchase) or wait for your monthly credits.          |
| `project_inactive`                                      | 409      | The project is paused or archived.                                                                                    |
| `conflict`                                              | 409      | The resource is in a state that doesn't allow this action right now.                                                  |
| `idempotency_conflict`                                  | 409      | The idempotency key was already used with a different body.                                                           |
| `idempotency_failed`                                    | 409      | The first request with this key failed after it may have cost credits. Check the resource, then retry with a new key. |
| `retryable_contention`, `billing_configuration_changed` | 409      | Something changed at the same moment. Retry with the same idempotency key.                                            |
| `billing_unavailable`                                   | 409      | Billing is briefly unavailable. Try again later.                                                                      |
| `acting_user_unavailable`                               | 409      | No team member can act for the token. Contact [Support](https://app.unspar.com/support) with the request ID.          |
| `rate_limited`                                          | 429      | Too many requests. See below.                                                                                         |
| `internal_error`                                        | 500      | Something broke on our side. Try again later. If it keeps happening, send us the request ID.                          |

Some endpoints also have their own error codes, like `idea_run_state_conflict` for idea runs. Their `message` explains what went wrong.

## How many requests can I send?

Unspar tracks rate limits per minute. They are high enough for normal polling. Checking an article's status every 10 seconds stays well under the cap.

| What is counted                             | Requests per minute                |
| ------------------------------------------- | ---------------------------------- |
| One API token                               | 60                                 |
| `GET /whoami` with one token                | 40, counted separately from the 60 |
| One IP address, before the token is checked | 120                                |
| Requests with a missing or invalid token    | 10                                 |

If you go over a limit, you get `429 rate_limited`. The `Retry-After` header tells you how many seconds to wait, and `X-RateLimit-Limit`, `X-RateLimit-Remaining`, and `X-RateLimit-Reset` show your current standing.
