# Errors

> Status codes, error codes and what to do.

Errors are JSON with a stable `code`, a human `message` and the `request_id`. No error is charged.

```json
{ "error": { "code": "no_credits", "message": "balance is zero; buy a credit pack at https://tikdataapi.com/dashboard/billing", "top_up_url": "https://tikdataapi.com/dashboard/billing" }, "request_id": "..." }
```

Match on `code`, never on `message`. The code is part of the contract and will
not change under you; the message is written for a person and may be reworded.

| Status | Code | Meaning | What to do |
|---|---|---|---|
| 400 | `bad_request` | a parameter is missing or malformed | fix the request |
| 401 | `missing_key`, `invalid_key` | no key, or a revoked key | check the header |
| 402 | `no_credits` | balance is zero | buy a pack at `error.top_up_url` |
| 403 | `email_not_allowed` | the account uses a throwaway email domain | sign up with a permanent address |
| 403 | `owner_only` | a team member's key tried to buy credits; only the account owner buys | ask the account owner to buy |
| 403 | `access_denied` | TikTok refused this object to our reader | try later, or a different object |
| 404 | `not_found` | no such account, video, comment or hashtag, or TikTok does not show it | read `error.reason` |
| 429 | `rate_limited` | over your requests-per-second limit | wait `retry-after` seconds |
| 503 | `upstream_rate_limited` | TikTok asked our reader for a captcha, or blocked its address | retry after `retry-after` seconds, with backoff |
| 503 | `busy` | 16 of your reads are in progress, or all our readers are busy | retry after `retry-after` seconds; send fewer calls at the same time |
| 502, 503 | `upstream_error` | TikTok answered with something we could not use | retry with backoff; if it persists we are already paged |
| 502 | `request_rejected` | TikTok rejected the request | retry once |
| 500 | `internal` | our bug | send us the `request_id` |

## RFC 9457 problem+json [#rfc-9457-problemjson]

Send `Accept: application/problem+json` and the same failure comes back in the
shape a generic HTTP client already understands. Nothing new is reported: `type`,
`title`, `status` and `detail` are the registered names for what `error.code` and
`error.message` already said, and the native `error` object is still there, so one
parser reads either shape.

```json
{
  "type": "https://tikdataapi.com/docs/errors#no_credits",
  "title": "Balance is zero",
  "status": 402,
  "detail": "balance is zero; buy a credit pack at https://tikdataapi.com/dashboard/billing",
  "code": "no_credits",
  "error": {
    "code": "no_credits",
    "message": "balance is zero; buy a credit pack at https://tikdataapi.com/dashboard/billing",
    "top_up_url": "https://tikdataapi.com/dashboard/billing"
  },
  "request_id": "..."
}
```

Without that header you get the native shape, which is what both SDKs read. Neither
is going away.

## Retry policy we recommend [#retry-policy-we-recommend]

Retry `503` and `502` up to three times with 2, 4 and 8 seconds between attempts. Retry a `429` after
`retry-after`. Do not retry other `4xx`. Because failed requests are free, retries never cost credits.

Read `retry-after` when it is present rather than guessing, and read `RateLimit`
on every response to throttle before a `429` happens at all. See
[Authentication](/docs/authentication) for the rate ladder.

## Every code [#every-code]

### missing_key [#missing_key]

`401`. No key was sent. Put it in `x-api-key`, or send `Authorization: Bearer tt_live_…`.
Free.

### invalid_key [#invalid_key]

`401`. The key is unknown or has been revoked. Check it has not been deleted in the
dashboard, and that you are not sending a key from another environment. Free.

### rate_limited [#rate_limited]

`429`. Over the account's requests-per-second limit, which is shared by every key
on the account. Wait `retry-after` seconds. The limit and what is left of it are on
every response in the `RateLimit` header, so a client that reads it need never hit
this. Free.

### no_credits [#no_credits]

`402`. The balance is zero. Buy a pack in the dashboard: the body carries the page as
`error.top_up_url`, so an agent can show it to a person. There is no overage and no
invoice: the API stops rather than spending money you did not agree to. Free. The account
email gets one notice when the balance reaches zero, and one when it falls under 20% of the
last pack.

### email_not_allowed [#email_not_allowed]

`403`. The account was made with a throwaway email domain. Such accounts get no credits
and no key works. Sign up again with a permanent address. Free.

### owner_only [#owner_only]

`403`. Only on `/v1/billing/checkout`. A team member made the key, and only the account
owner can buy credits. Ask the owner to buy. Free.

### bad_request [#bad_request]

`400`. Something about the request itself is wrong: a missing required parameter, a
malformed cursor, a `count` out of range, a batch list over its limit, an unparseable body. The message
names the problem. Retrying unchanged will fail again. Free.

### not_found [#not_found]

`404`. There is no such account, video, comment or hashtag, or TikTok does not show it to a
logged-out reader. Not a fault and not retryable. Free.

`error.reason` says why:

| `reason` | What it means |
|---|---|
| `nonexistent` | No such account, video, comment or hashtag, or it was deleted. TikTok does not tell those two apart. |
| `banned` | TikTok banned the account. |
| `private` | A video that only some accounts may see. |
| `removed` | TikTok removed the video for a rule of TikTok. |
| `unavailable` | Any other reason, for example a video that TikTok does not show in the country of our reader. |

A private account is not a `not_found`: its profile comes back with `private: true`, and its video list is
empty and free. The batch endpoints put the same `reason` on the item in `errors`.

```json
{ "error": { "code": "not_found", "message": "the account is banned", "reason": "banned" }, "request_id": "..." }
```

### access_denied [#access_denied]

`403`. TikTok refused to show this object to our reader. Often it is about the object and not the
account, so another read may well succeed. Free.

### upstream_rate_limited [#upstream_rate_limited]

`503`. TikTok asked our reader for a captcha, or blocked the address it reads from. Retry after
`retry-after` seconds with backoff. Free.

### busy [#busy]

`503`. Your account has 16 reads of TikTok in progress, or all our readers are in use at this moment. The
call waited up to 5 seconds for a place. Retry after `retry-after` seconds, and send fewer calls at the same
time. Free.

### upstream_error [#upstream_error]

`502` or `503`. TikTok answered with something we could not use. A `503` carries `retry-after` and is worth
a retry soon; a `502` is worth one retry. If it persists, it is already paging us. Free.

### request_rejected [#request_rejected]

`502`. TikTok rejected the shape of our request to it. Same handling as `upstream_error`. Free.

### billing_disabled [#billing_disabled]

`503`, on the checkout endpoint only. Card checkout is not configured on this deployment. Nothing you can fix
from the client. Free.

### internal [#internal]

`500`. Our bug. Send the `request_id` to hello@tikdataapi.com and we will find the exact
call in the logs. Free.
