# Authentication

> Keys, headers and rate limits.

## The key [#the-key]

A key looks like `tt_live_` followed by 32 characters. It is shown once when created. Only a hash is stored on our side.
Send it in the `x-api-key` header, or as `Authorization: Bearer tt_live_...`.

A key is tied to one wallet. Several keys can share a wallet, for example one per environment.

The key is for this API only. You need no TikTok account, and you never give us a TikTok login or cookies.

## Rate limit [#rate-limit]

The limit is per account, shared by all its keys. It follows the total you have paid over the life of the
account, and it only goes up.

| Total paid | Requests per second |
|---|---|
| $0 | 1 |
| $10 or more | 20 |
| $50 or more | 50 |
| $200 or more | 100 |
| $600 or more | 200 |

Five starter packs count the same as one builder pack. A small pack after a large one changes nothing.
Above the limit the API answers `429` with a `retry-after` header. Nothing is charged. Need more?
Write to hello@tikdataapi.com.

### Read the headers instead of guessing [#read-the-headers-instead-of-guessing]

Every response carries the limit and what is left of it, so a client never has to
discover its ceiling by hitting it. The header names come from the IETF draft
[RateLimit header fields for HTTP](https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/):

```http
RateLimit: limit=20, remaining=19, reset=1
RateLimit-Policy: "per-second"; q=20; w=1
```

These are on refusals too, and on a `401`, where `RateLimit-Policy` quotes the rate
an account starts on. A `429` adds `Retry-After` in whole seconds.

The keyless endpoints — `GET /`, `/status`, `/healthz` and `/openapi.yaml` on
`api.tikdataapi.com` — have their own ceiling of 10 requests per second per address,
reported under the policy name `public`. No key is needed for them and none is
counted against your account.

The limit counts requests. Cached and batch requests run at the full rate. A read that is not in cache goes
to TikTok, so a burst of many different reads that are not in cache takes longer than the rate limit
alone suggests. A list call with a `count` above one TikTok page reads TikTok more than once, and takes
longer too: see [Pagination](/docs/pagination).

### Calls in progress [#calls-in-progress]

One account can have 16 reads of TikTok in progress at the same time. A further call waits for a place for up
to 5 seconds. If no place comes free, the API answers `503` with the code `busy` and `retry-after: 2`, and
nothing is charged. A response served from cache does not count. If you get `busy`, send fewer calls at
the same time.

## Check a key [#check-a-key]

```bash
curl https://api.tikdataapi.com/v1/me -H "x-api-key: tt_live_..."
```

```json
{ "customer_id": "...", "key_prefix": "tt_live_GPE4", "balance": 4895.5, "rate_limit_qps": 20 }
```

## Keep it secret [#keep-it-secret]

Never ship the key in a browser or a mobile app. Call the API from your server and put your own auth in front.
Revoke a leaked key in the [dashboard](https://tikdataapi.com/dashboard) and create a new one.
