# Errors and rate limits


## Errors

> Incorrect input (an account ID that doesn't exist)

```shell
curl -XPOST https://api.soundtrack.io/v2 \
-H 'Authorization: Basic your_token' \
-H 'Content-Type: application/json' \
-d '{
  "query": "query Account($id: ID!) { account(id: $id) { businessName } }",
  "variables": {"id": "QWNjb3VudCwsWNheXg3dTc1Nm8v"},
  "operationName": "Account"
}'
```

> Response (HTTP `200 OK`)

```json
{
  "errors": [
    {
      "path": ["account"],
      "message": "Forbidden",
      "locations": [
        {
          "line": 2,
          "column": 0
        }
      ]
    }
  ],
  "data": {
    "account": null
  }
}
```

With GraphQL, an HTTP `200 OK` doesn't always mean you got the data you wanted. If the query runs but hits an error, you get a `200` with an `errors` array in the response body. The API returns `Forbidden` both for an ID that doesn't exist, as in this example, and for one you can't access.

The API answers with one of three HTTP status codes:

| Status                    | Meaning                                                             |
| ------------------------- | ------------------------------------------------------------------- |
| `200 OK`                  | The request was handled. Any errors are in the `errors` array       |
| `429 Too Many Requests`   | You're [rate limited](#rate-limit)                                  |
| `503 Service Unavailable` | A service the API depends on has problems. Back off and retry later |

:::note
Did you get an error message that you think could be improved? Please [reach out on Slack](https://join.slack.com/t/soundtrack-api/shared_invite/enQtNjM5MzAwMzExNTI3LWIyZjQ3NjQ4MjU2NWJlM2YxZjk2YjZmNWNhMzdkMjNkNWI2ZTliYTg1Nzc2MGUwODQyODc3MzQ4MmY5NmNkZWE) and let us know.
:::

## Rate limit

Each API token can send a burst of up to 200 requests, and after that 30 requests a second. Think of it as a bucket of 200 that refills by 30 every second: a burst empties it, and a steady 30 a second never runs it dry.

Every query and mutation counts as one request, however much it asks for. Subscriptions don't count.

> We watch how the API is used and may change the rate limit.

### When you're rate limited

```text
HTTP/1.1 429 Too Many Requests
Content-Type: application/json

{"errors":[{"message":"Rate limit exceeded","extensions":{"code":"RATE_LIMIT_EXCEEDED"}}],"data":null}
```

Once you go over the limit, the API answers with HTTP `429 Too Many Requests` and an error with the code `RATE_LIMIT_EXCEEDED`, with `data` set to `null`. The limit refills 30 requests a second, so wait a second before you send the request again, and longer if the next one is rejected too.

## Retries

Retry only what a second try can fix, and wait longer before each one.

| What you got                                  | Retry? | How                                                                                                                             |
| --------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------- |
| HTTP `429`                                    | Yes    | Wait a second, since the limit refills 30 requests a second, then send the request again                                        |
| HTTP `503`, a timeout or a dropped connection | Yes    | Back off: wait about 1 second, then 2, then 4, up to a minute, and stop after a few tries                                       |
| An unexpected WebSocket close                 | Yes    | Reconnect with the same backoff, then [catch up](/api/subscriptions#handling-the-first-value-and-reconnects) on what you missed |
| HTTP `200` with an `errors` array             | No     | The same input gets the same error, such as `Forbidden`. Fix the request first                                                  |

Add a random amount to each wait. If many clients lose the API at the same moment, the same fixed waits bring them all back at the same moment too.

A timed-out mutation may still have run. Retrying `setVolume` or `play` is safe, since running them twice ends the same way, but a retried `skipTrack` can skip two songs. Before you retry one like that, query the zone to see whether it already happened.
