> ## Documentation Index
> Fetch the complete documentation index at: https://docs.topsort.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Toppie Errors

> Understand the error responses of the Toppie API and how to resolve them

When a Toppie API request fails, the response carries an HTTP status code and a JSON body that explains the problem. This page describes the body format, what each status code means, and the most common causes.

## Error Response Format

Toppie errors return a JSON object with a single `detail` field:

| Field | Type | Description |
| :- | :- | :- |
| `detail` | string or array | A human-readable message. For validation errors (`422`) on parameters or body fields, a list of field errors. |

```json theme={null}
{
  "detail": "Agency campaign with id 3f1c9a52-7d4e-4b8a-9c61-2e5f0a8b7d13 not found."
}
```

When a parameter or body field fails validation, `detail` is a list with one entry per invalid field:

| Field | Type | Description |
| :- | :- | :- |
| `loc` | array | Where the invalid value is, for example `["body", "budget", "amount"]` or `["query", "limit"]`. |
| `msg` | string | What is wrong with the value. |
| `type` | string | A short code for the kind of error, for example `missing` or `int_parsing`. |

```json theme={null}
{
  "detail": [
    {
      "type": "missing",
      "loc": ["body", "name"],
      "msg": "Field required"
    }
  ]
}
```

Entries can include extra fields, such as the rejected `input`. Messages can change over time, so use the status code to decide how to handle an error, and log `detail` for troubleshooting.

## HTTP Status Codes

> [card] 400 - Bad Request
>
> The request is not supported, for example an update on a banner campaign, or a report request with an invalid page token.

> [card] 401 - Unauthorized
>
> The API key is missing, invalid or not a Toppie agency key, or the account is inactive. See [Toppie authentication](https://docs.topsort.com/en/api-reference/toppie/authentication.md).

> [card] 403 - Forbidden
>
> The API key is not allowed to perform the action. Keys created in the Toppie UI have full access, so contact your Toppie account manager if you receive this.

> [card] 404 - Not Found
>
> The campaign does not exist, or it does not belong to the account of the API key. Check the campaign id.

> [card] 409 - Conflict
>
> The account already has a campaign with the same name. Use a different name.

> [card] 422 - Unprocessable Entity
>
> A parameter or body field is missing or invalid, a product target is not in the catalog, or the campaign dates are not valid.

> [card] 429 - Too Many Requests
>
> The API key sent more than 10 requests per second. Wait and retry. See [Rate Limiting](#rate-limiting).

> [card] 5xx - Server Error
>
> Something went wrong on our side. Retry with backoff, and contact support if the error persists.

## Common Errors

> [accordion] Authentication Errors
>
> * **`401`** `Unauthorized`: the `Authorization` header is missing, the key is not valid, it is a Marketplace (`TSE_`) or Advanced (`TSC_`) key instead of a Toppie (`TAS_`) key, or the account is inactive.
> * **`403`** `You do not have the required permissions to perform this action.`: the key is not allowed to call the endpoint.

> [accordion] Campaign Errors
>
> * **`400`** `Campaign type '...' is not supported for this endpoint.`: banner campaigns cannot be updated through the API. Manage them in the Toppie UI.
> * **`404`** `Agency campaign with id ... not found.`: the campaign id does not exist in the account.
> * **`409`** `Agency Campaign with name ... already exists ...`: campaign names must be unique within the account.
> * **`422`** `Targets not found in the catalog: ...`: one or more product targets are not in the catalog. Use ids returned by the products endpoint.
> * **`422`** `The campaign end date (...) must be greater than its start date (...).`: an update would leave the campaign ending at or before its start.

> [accordion] Pagination Errors
>
> * **`400`** `Invalid next page token.`: the page token sent to a campaign report or to the campaigns report list is not valid.
> * **`422`** `Invalid next_page ...`: the `next_page` value sent to the campaign list, the campaign bids or the account activity is not valid.
>
> In both cases, send the value returned in the previous response without changes.

> [accordion] Validation Errors
>
> * **`422`** with a list in `detail`: a required field is missing, a value has the wrong type, or it is out of range. Each entry's `loc` points to the field to fix.

> [!NOTE]
>
> Fields that an endpoint does not define, including misspelled ones, are ignored without an error. If a change you sent does not take effect, check the field names against the endpoint reference.

## Rate Limiting

Each API key can send up to 10 requests per second. Every response includes these headers:

* `X-RateLimit-Limit`: total requests allowed in the window.
* `X-RateLimit-Remaining`: requests left in the current window.
* `X-RateLimit-Reset`: number of seconds until the window resets. It is a duration, not a timestamp.

When you receive `429 Too Many Requests`, wait the number of seconds in `X-RateLimit-Reset` before you retry. For `5xx` errors, retry with increasing delays:

```python theme={null}
import time
import requests

def get_with_retry(url, headers, max_retries=5):
    delay = 1
    for _ in range(max_retries):
        response = requests.get(url, headers=headers)
        if response.status_code == 429:
            # Seconds until the rate limit window resets
            time.sleep(max(int(response.headers.get("X-RateLimit-Reset", 1)), 1))
        elif response.status_code >= 500:
            time.sleep(delay)
            delay = min(delay * 2, 64)
        else:
            return response
    return response
```

## Need Help?

If an error persists after you check this page, contact your Toppie account manager with the endpoint, the request body without the API key, the status code and the `detail` returned.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.