# Credits and API keys

With these endpoints, your code reads the team's credit balance and history. It can also check what a token is allowed to do and manage the team's tokens.

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

Credits are how you pay in Unspar. Every article and idea run draws from your team's balance. With the credit endpoints, your code can check the balance before starting a batch of articles or send an alert when credits run low.

The token endpoints tell you who is calling and what that caller is allowed to do. In the API, an API token is also called an API key, and its ID starts with `ak_`. You always create your first token in [Settings](https://app.unspar.com/settings) under **API tokens**, as shown in the [API overview](https://www.unspar.com/docs/api/#get-an-api-token). After that, a token with the right permissions can create more tokens from code.

## Read the balance

`GET /credits/balance` returns the current balance and the team's plan. It also tells you when the balance last changed.

```sh
curl https://app.unspar.com/api/v1/credits/balance \
  -H "Authorization: Bearer $UNSPAR_API_KEY"
```

The response looks like this:

```json
{
  "data": {
    "balance": 212,
    "subscription": { "plan": "pro", "status": "active" },
    "last_credit_update": "2026-11-24T08:02:08+00:00"
  }
}
```

Without a paid plan, `subscription` is `null`. The balance still includes credits that Unspar is holding for articles currently being written, so the number alone doesn't tell you whether you can afford one more article. Ask for an [estimate](https://www.unspar.com/docs/api/articles/#what-does-an-article-cost) instead. Its `credits.reason` will be `insufficient_credits` if your team can't cover it.

## Read the credit history

`GET /credits/transactions` returns the history, newest first, one page at a time. You can filter by `type` and by date with `created_from` and `created_to`. This request fetches everything spent in November:

```sh
curl "https://app.unspar.com/api/v1/credits/transactions?type=credit_spent&created_from=2026-11-01T00:00:00Z&created_to=2026-11-30T23:59:59Z&per_page=100" \
  -H "Authorization: Bearer $UNSPAR_API_KEY"
```

Each entry has an `amount` and a short `description`. The amount is negative for spent credits. To get a total, add up the amounts and ask for the next `page` while `meta.pagination.has_more` is `true`. Held credits show up here only once they are charged.

| `type`         | What it is                                                                                             |
| -------------- | ------------------------------------------------------------------------------------------------------ |
| `credit_added` | Credits added to your balance, such as your plan's monthly credits or the free credits you start with. |
| `credit_spent` | Credits spent, for example on a finished article or an idea run.                                       |
| `debt`         | The balance went below zero, or a negative balance was paid back.                                      |
| `adjustment`   | Credits that expired, or changes from switching or ending a plan.                                      |
| `other`        | Any other entry.                                                                                       |

The web app shows the same history with more detail on [Credits](https://app.unspar.com/credits), including the article and the person who started it. Only the team owner can open that page. [Credits and prices](https://www.unspar.com/docs/guides/credits-and-prices/) explains when credits are held and when they are charged.

## Check what a token can do

Two endpoints tell you about the token you're using. Call `GET /whoami` once when your program starts so a wrong or expired token fails early and clearly.

| Endpoint       | What it shows                                                                                                                                                       |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GET /whoami`  | The team the token belongs to and the projects it can reach. It also lists the permission groups, each with its `id` and whether the token can read or write there. |
| `GET /account` | The plan and its subscription, plus whether the plan includes API access. Every plan includes it today.                                                             |

`GET /whoami` has its own rate limit of 40 calls a minute, separate from your other calls.

## What can a token be allowed to do?

When you create a token in the web app and tick **Restrict access**, you choose three things. The same choices exist in the API.

| Setting           | Options                                                                                                                                                                                                                                                                                  |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Preset**        | **Full access** can write and publish articles, but doesn't include **Publish approval** or **API keys**. **Read-only** can only read. **Custom** lets you set each permission group to **Read** or **Read and write**. **Translations (SiteChorus)** is for a connected WordPress site. |
| **Project scope** | **All projects**, including ones created later, or **Selected projects**. A token limited to selected projects can't create projects or manage outlets, because those belong to the whole team.                                                                                          |
| **Expiration**    | **Never**, **30 days**, **90 days**, or **Custom** for a date you pick.                                                                                                                                                                                                                  |

You see a token's secret only once, right after you create it. Unspar stores only a fingerprint of it, so nobody can show it to you again. If you lose it, rotate the token. `unspar login` in the [CLI](https://www.unspar.com/docs/cli/) creates the same kind of token for your computer after you approve it in the browser.

## Create a token from code

A deploy script can give each new server its own limited token. The token the script uses needs these settings:

- **Preset** is **Custom**, with **API keys** set to **Read and write**.
- **Project scope** is **All projects**.

It can only pass on permissions it has itself, and a new token can't expire later than the token that creates it. Tokens that can publish can't be created this way at all. You can only create and rotate those on the **API tokens** page in the web app.

This request creates a token that writes articles for one project and can't publish them:

```sh
curl -X POST https://app.unspar.com/api/v1/api-keys \
  -H "Authorization: Bearer $UNSPAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Website server",
    "preset": "custom",
    "group_selections": [
      {"id": "projects", "mode": "read"},
      {"id": "articles", "mode": "read_write"},
      {"id": "generation", "mode": "read_write"}
    ],
    "project_scope": "selected_projects",
    "selected_project_public_ids": ["<project-id>"],
    "expires_at": "2027-03-01T00:00:00Z"
  }'
```

`preset` is `full_access`, `read_only`, or `custom`. For `custom`, use the group IDs listed by `GET /whoami`. The new secret is in `data.access_token`, and the token's ID is in `data.key.id`. This response is the only time you'll see the secret, so save it right away. Don't send an `Idempotency-Key` header here, or you'll get a `422`.

## Rotate or revoke a token

Rotating replaces a token's secret, for example after a leak. Revoking switches a token off for good.

| Request                          | What happens                                                                                                                                                                                                                                                                                                |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `POST /api-keys/{apiKey}/rotate` | A new token with the same settings replaces the old one, which stops working right away. The new token has a new secret and a new ID. The response returns the secret in `data.access_token`, the new token in `data.key`, and the old one in `data.previous_key`. Save both the new secret and the new ID. |
| `POST /api-keys/{apiKey}/revoke` | The token stops working for good. It stays in the list so you can see it existed, but you can't change or restore it.                                                                                                                                                                                       |
| `PATCH /api-keys/{apiKey}`       | Changes an active token, such as its name or permissions.                                                                                                                                                                                                                                                   |

A token can't rotate or revoke itself. It can only change its own name, so use a second token to manage the others. [API keys and connections](https://www.unspar.com/docs/agents/access/) covers the same options in the web app, along with the connections AI tools use instead of tokens.

## Endpoint reference

### GET /api/v1/whoami

`getWhoami`: Inspect the authenticated team API key or site connector.

Team API keys get the `key` block. Site connector OAuth tokens get a
`connection` block instead. A `401` for a connector token names the
connection or its access token in `error.message`, not an API key; the
codes are the same as for keys. Both see the team with its credit balance
and the connected sites the credential can see: its one site for a
connector, the team's sites for an API key.

`team.credits.balance` is the translation-usable balance: the credits
the team can spend on site translation now, net of pending holds, the
same balance a translation job reservation checks. It is the balance
read to poll while someone buys credits. `team.credits.expires_at` and
`expiring_credits` say when the next credits in it expire and how many.
`team.translation_allowance.state` says whether the free start is
`granted`, `under_review` (waiting for support) or `none`, and
`team.personal` marks a personal team.

Rate limit: `whoami` has its own bucket of 40 requests per minute per
API key or connector authorization, so polling it every three seconds
(20 per minute) never uses the budget of the other calls, which keep
their bucket of 60 per minute. Every request also counts against the
limit of 120 per minute per client IP.


Responses: 200 Token identity and grants.

### GET /api/v1/account

`getAccount`: Inspect compact account, plan, subscription, and API access state.

Responses: 200 Account summary.

### GET /api/v1/credits/balance

`getCreditBalance`: Show current credit balance.

Permission: `unspar:credits:read`

Responses: 200 Credit balance.

### GET /api/v1/credits/transactions

`listCreditTransactions`: List public credit transactions.

Permission: `unspar:credits:read`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `page` | query | integer | no | Minimum 1. |
| `per_page` | query | integer | no | Minimum 1. Maximum 100. |
| `type` | query | "credit_added", "credit_spent", "debt", "adjustment", "other" | no |  |
| `created_from` | query | string (date-time) | no |  |
| `created_to` | query | string (date-time) | no |  |
| `sort` | query | "created_at", "-created_at" | no |  |

Responses: 200 Credit transaction list.

### GET /api/v1/api-keys

`listApiKeys`: List team API keys.

Permission: `unspar:api_keys:read`

Responses: 200 Team API key list.

### POST /api/v1/api-keys

`createApiKey`: Create a team API key.

Permission: `unspar:api_keys:write`

| Body field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | yes | Up to 255 characters. |
| `preset` | "full_access", "read_only", "translations", "custom" | no |  |
| `group_selections` | array of object | no |  |
| `project_scope` | "all_projects", "selected_projects" | no |  |
| `selected_project_public_ids` | array of string | no |  |
| `expires_at` | string (date-time) | no |  |
| `permissions` | object | no | Reserved for compatibility; public requests reject direct permission arrays. |

Responses: 201 Created API key secret and metadata.

### GET /api/v1/api-keys/{apiKey}

`getApiKey`: Show one team API key.

Permission: `unspar:api_keys:read`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes |  |

Responses: 200 Team API key.

### PATCH /api/v1/api-keys/{apiKey}

`updateApiKey`: Update one team API key.

Permission: `unspar:api_keys:write`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes |  |

| Body field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | no | Up to 255 characters. |
| `preset` | "full_access", "read_only", "translations", "custom" | no |  |
| `group_selections` | array of object | no |  |
| `project_scope` | "all_projects", "selected_projects" | no |  |
| `selected_project_public_ids` | array of string | no |  |
| `expires_at` | string (date-time) | no |  |
| `permissions` | object | no | Reserved for compatibility; public requests reject direct permission arrays. |

Responses: 200 Team API key.

### POST /api/v1/api-keys/{apiKey}/rotate

`rotateApiKey`: Rotate a team API key secret.

Permission: `unspar:api_keys:write`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes |  |

| Body field | Type | Required | Description |
| --- | --- | --- | --- |
| `expires_at` | string (date-time) | no |  |

Responses: 200 Rotated API key secret and metadata.

### POST /api/v1/api-keys/{apiKey}/revoke

`revokeApiKey`: Revoke one team API key.

Permission: `unspar:api_keys:write`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes |  |

Responses: 200 Team API key.
