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

# Authentication

> Create API keys, authenticate requests, restrict keys, and stay within rate limits.

The Chatling API uses API keys to authenticate requests. Each key belongs to one project and can use its AI agents and chatbots, within the permissions, agent access, and expiry you set.

## Create an API key

**1. Open the API keys**

In the dashboard, go to **Project settings > API**. Only owners and admins can access it, and the plan must include API access.

**2. Name the key**

Click **+ New API key** and name the key after where you'll use it, such as "Helpdesk sync".

**3. Choose what it can do**

Set its expiry, agent access, and permissions. See [Key restrictions](#key-restrictions).

**4. Copy the key**

Click **Generate key** and copy the key. It's only shown once. If you lose it, delete it and generate a new one.

> [!WARNING]
>
> Your API keys can do everything their permissions allow, so keep them secure. Only use them on your server, never in a browser, mobile app, or public repository. If a key is exposed, delete it in the dashboard. Requests with it fail immediately.

## Authenticate requests

Authenticate by passing the key in the `Authorization` header as a bearer token. All requests must be made over HTTPS.

```
Authorization: Bearer <API_KEY>
```

Requests without a valid key fail with one of these errors:

| Status | Code | Description |
| - | - | - |
| 401 | `missing_api_key` | The `Authorization` header is missing or isn't `Bearer <key>`. |
| 401 | `invalid_api_key` | The key doesn't exist or was deleted. |
| 401 | `api_key_expired` | The key has expired. |
| 403 | `plan_feature_unavailable` | The plan doesn't include API access. |
| 403 | `project_suspended` | The project is suspended or no longer exists. |
| 429 | `rate_limit_exceeded` | Too many requests with invalid or expired keys came from your IP address. The limit is 60 a minute. |

## Sample request

```bash cURL theme={null}
curl https://api.chatling.ai/v3/me \
  -H "Authorization: Bearer $CHATLING_API_KEY"
```

```javascript JavaScript theme={null}
const response = await fetch("https://api.chatling.ai/v3/me", {
  headers: { Authorization: `Bearer ${process.env.CHATLING_API_KEY}` },
});
const apiKey = await response.json();
```

```python Python theme={null}
import os
import requests

response = requests.get(
    "https://api.chatling.ai/v3/me",
    headers={"Authorization": f"Bearer {os.environ['CHATLING_API_KEY']}"},
)
api_key = response.json()
```

```json Response theme={null}
{
  "object": "api_key",
  "id": "8d0c6f52-3b7e-4c1a-9e2f-5a4b3c2d1e0f",
  "name": "Helpdesk sync",
  "token_preview": "abc...wxyz",
  "created_by": { "account_id": "2f6e1c9a-8b4d-4a3e-b7c5-1d0e9f8a7b6c", "name": "Ada Lovelace" },
  "created_at": "2026-11-02T14:05:31Z",
  "expires_at": null,
  "permissions": { "mode": "read_write", "groups": null },
  "agent_access": { "mode": "all", "agent_ids": null },
  "project": { "object": "project", "id": "5c3b2a19-0f8e-4d7c-a6b5-e4d3c2b1a0f9", "name": "Acme" }
}
```

## Key restrictions

You can restrict a key in three ways when you create or edit it in the dashboard.

### Permissions

| Option | Description |
| - | - |
| Read and write | All requests. The default. |
| Read only | `GET` requests only. |
| Custom | Only the access you select for each permission group. |

With **Custom**, you choose create, read, update, and delete access for each permission group. Workflows also have publish access, which publishing a workflow and turning one on or off need, because both make it run on your users' conversations. Read-only keys don't include it.

### Agent access

A key can use all of the project's AI agents and chatbots, or only the ones you select. A key limited to selected agents:

* Can't see other agents or their conversations, contacts, and other resources. They return 404, as if they don't exist.
* Can't use project-wide endpoints: creating or duplicating an agent, listing members, and retrieving or updating the project and its usage.

### Expiry

A key can expire after 1 day, 7 days, 30 days, 1 year, or a custom number of days up to 365, or never. Requests with an expired key return 401 `api_key_expired`. Changing a key's expiry in the dashboard starts the new period from that day.

## Rate limits

Each API key can make **500 requests a minute**, across all endpoints. Each key has its own limit.

Every response includes headers with the key's current usage:

| Header | Description |
| - | - |
| `X-RateLimit-Limit` | The number of requests the key can make per minute. |
| `X-RateLimit-Remaining` | The number of requests left in the current minute. |
| `X-RateLimit-Reset` | The Unix timestamp when the limit resets. |

If the key exceeds the limit, the API returns 429 with a `Retry-After` header, the number of seconds to wait:

```http theme={null}
HTTP/1.1 429 Too Many Requests
Retry-After: 23
X-RateLimit-Limit: 500
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1795536000
```

```json theme={null}
{
  "error": {
    "type": "rate_limit_error",
    "code": "rate_limit_exceeded",
    "message": "This API key is limited to 500 requests a minute. Retry after 23 seconds.",
    "param": null,
    "request_id": "req_6Tn2Xc8LqZ4vR1mKp9Wb3Hs7"
  }
}
```

Other limits return the same error:

* **Messages per user:** [Send a user message](https://docs.chatling.ai/api-reference/v3/conversations/send-user-message.md) also enforces the AI agent's per-user message limits, set in the agent's settings in the dashboard.
* **Invalid keys:** 60 requests a minute with invalid or expired keys from one IP address.
* **Attachment downloads:** 120 requests a minute for [message attachment links](https://docs.chatling.ai/api-reference/v3/conversations/download-message-attachment.md).

### Handling rate limits

* **Wait for `Retry-After`** after a 429, then retry the same request. For a `POST`, reuse its `Idempotency-Key` so a request that already ran isn't repeated.
* **Slow down before you reach the limit.** If `X-RateLimit-Remaining` gets low, wait until `X-RateLimit-Reset`.
* **Spread out bulk work,** such as importing contacts, instead of sending it all at once. Use bulk endpoints where available, such as [Add knowledge base sources in bulk](https://docs.chatling.ai/api-reference/v3/knowledge-base/create-knowledge-base-sources.md).

```javascript theme={null}
async function chatling(path, options = {}, attempt = 0) {
  const response = await fetch(`https://api.chatling.ai/v3${path}`, {
    ...options,
    headers: { Authorization: `Bearer ${process.env.CHATLING_API_KEY}`, ...options.headers },
  });

  if (response.status === 429 && attempt < 3) {
    const seconds = Number(response.headers.get("Retry-After") ?? 1);
    await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
    return chatling(path, options, attempt + 1);
  }

  return response;
}
```


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