# Authentication > Authenticate with an API key or an OAuth token, choose what each key may do, and stay inside the rate limits Every request to the QwryAI API carries a credential in the `Authorization` header. Most integrations use an **API key** you create in the dashboard. Apps that act for other QwryAI users, such as an AI assistant your customers connect, use an **OAuth access token** instead. Either way, the credential acts as the member who created it and can never do more than that member's role allows. ## Bearer Token Authentication All requests to the Chat API and the Tools API (except the health check) need a Bearer credential: ``` Authorization: Bearer ``` The Tools API also accepts the key in an `x-api-key` header, for clients that can't set `Authorization`: ``` x-api-key: ``` | Credential | Looks like | Chat API (`/api/v2`) | Tools API (`/public/v1`) | | - | - | - | - | | API key | starts with `cb_live_` | Yes | Yes | | OAuth access token | starts with `qwa_` | No | Yes | ### Getting an API Key API keys belong to the workspace. Members with the **Admin** or **Manager** role create and delete them; other members see the list without **Create API key**. **1. Open API keys** In the sidebar, open **Workspace settings › API keys**. The page lists every key in the workspace, masked, with the access each one has underneath. Until the first key exists, it shows **No API keys found**. ![The API keys page under Workspace settings, with the Manage keys heading, a link to the documentation, the Create API key button, and No API keys found while the workspace has none.](annotated/01-api-keys-page.webp "Workspace settings › API keys") **2. Select Create API key** ![The Create API key button at the top right of the Manage keys section.](annotated/02-create-button.webp "Select Create API key.") **3. Choose the key's permissions and confirm** **Create New API Key** lists the permissions you can give the key, all switched on. Switch off anything the integration doesn't need, then select **Confirm**. The key never gets more than your own role allows, now or later. ![The Create New API Key dialog with a switch for each permission: Chat with agents, Manage conversations and helpdesk, Read agent settings, Change agent settings, Change sources and training, Read actions and procedures, Change actions and procedures, and View plan and credits, above Cancel and Confirm.](annotated/03-create-dialog.webp "Create New API Key") **4. Copy the key** The new key joins the list, masked, with its access listed underneath. Open its **⋯** menu and select **Copy key**. The eye button, **Show key**, reveals the whole key on screen if you need to read it. > [!WARNING] > > A key acts in your workspace as you. Keep it on your server: never put it in a web page, a mobile app, a public repository or a browser request. If a key leaks, select **Delete key** in its menu; every service using it loses access at once. > [!NOTE] > > API keys work on the **Starter**, **Growth** and **Enterprise** plans. On **Free**, the page shows **Unlock API access by subscribing to one of these plans** with a **View plans** button, and requests with an existing key are refused. ### Permissions Each key holds the permissions you switched on when you created it. Each tool needs one permission, and a key sees only the tools its permissions allow. The Chat API needs **Chat with agents**. | Permission | Code | What it allows | | - | - | - | | Chat with agents | `USE_CHAT` | List agents, start customer conversations, send customer messages, and search knowledge and product documentation. | | Manage conversations and helpdesk | `MANAGE_CHAT` | Read conversations, tickets and analytics; reply as a human agent, add notes, resolve, and pause the AI. | | Read agent settings | `READ_AGENT_CONFIGURATION` | Instructions, model, chat interface, sources, knowledge gaps, integrations, webhooks and deploy status. | | Change agent settings | `MANAGE_AGENT_CONFIGURATION` | Instructions, model, chat interface, styles, spam and country rules, and deploy channels. | | Change sources and training | `MANAGE_AGENT_SOURCES` | Add, edit, delete and restore sources, answer knowledge gaps, and start training. | | Read actions and procedures | `READ_AUTOMATION` | Actions, procedures, drafts, publication state, widgets and templates. | | Change actions and procedures | `MANAGE_AUTOMATION` | Create, edit, enable, disable and delete actions, procedures and webhooks. | | View plan and credits | `VIEW_BILLING` | Plan, credit balance and credit usage. | The permissions are checked against the creator's **current** role on every request. If the creator's role is lowered, the key loses whatever the new role can't do; if the creator leaves the workspace, the key stops working. To see what a key holds right now, call `GET /public/v1/me`: ```bash curl 'https://api.qwryai.com/public/v1/me' \ -H 'Authorization: Bearer YOUR_API_KEY' ``` ```json { "success": true, "data": { "plan": "growth", "authType": "api_key", "creatorRole": "Admin", "granted": ["USE_CHAT", "MANAGE_CHAT", "READ_AGENT_CONFIGURATION"], "permissions": [ { "permission": "USE_CHAT", "label": "Chat with agents" }, { "permission": "MANAGE_CHAT", "label": "Manage conversations and helpdesk" }, { "permission": "READ_AGENT_CONFIGURATION", "label": "Read agent settings" } ], "tools": ["get_account_context", "list_agents", "search_knowledge", "query_conversations", "list_sources"] } } ``` `granted` is what the key was given; `permissions` is what it holds now, after the creator's role is applied. The response also carries your workspace's `tenantId`. ### Example Request ```bash curl -X POST 'https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat' \ -H 'Authorization: Bearer YOUR_API_KEY' \ -H 'Content-Type: application/json' \ -d '{"message": "Hello!"}' ``` ### OAuth access tokens Apps that connect to other people's QwryAI workspaces use OAuth 2.0 with PKCE instead of asking for an API key. The person approving the connection picks the workspace and the permissions, just as they would for a key. | Detail | Value | | - | - | | Discovery | `https://api.qwryai.com/.well-known/oauth-authorization-server` | | Grant types | `authorization_code` (with PKCE `S256`), `refresh_token`, and the device code grant | | Client registration | Dynamic, at `/oauth/register`; public clients, no client secret | | Scopes | The permission codes in the table above | | Access token | `qwa_…`, valid for 1 hour | | Approval | Lasts 90 days; refreshing a token doesn't extend it | Send the access token exactly like an API key, `Authorization: Bearer qwa_…`, to the Tools API. The Chat API accepts API keys only. The [MCP server](/docs/developer-guides/mcp) and the [CLI](/docs/cli/auth) use this flow for you. ## Rate Limiting The two APIs have separate limits: | API | Limit | Counted per | | - | - | - | | Chat API | **1,000 requests per 10-second sliding window** | API key and caller IP address | | Tools API | **2,000 requests per 15 minutes** | Credential (API key or OAuth token) | The Chat API also caps how many streamed replies can be open at once. When it's full, a new streaming request gets the same `429` until a stream finishes. ### Rate Limit Headers | Header | Description | | - | - | | `X-RateLimit-Limit` | Requests allowed in the window. | | `X-RateLimit-Remaining` | Requests left in the current window. | | `X-RateLimit-Reset` | Unix time, in seconds, when the window resets. | | `Retry-After` | Seconds to wait before retrying. Only on `429` responses. | ### Handling Rate Limits Past the Chat API limit, you get a `429`: ```json { "error": { "code": "RATE_LIMIT_TOO_MANY_REQUESTS", "message": "Too many requests, please try again later" } } ``` Past the Tools API limit, you get a `429` with its own code: ```json { "error": { "code": "INTEGRATION_RATE_LIMIT_EXCEEDED", "message": "Too many integration requests, please try again later." } } ``` Wait for the number of seconds in `Retry-After`, then try again: ```javascript async function fetchWithRetry(url, options) { const response = await fetch(url, options); if (response.status === 429) { const retryAfter = parseInt(response.headers.get("Retry-After") ?? "1", 10); await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000)); return fetchWithRetry(url, options); } return response; } ``` ## Request ID Chat API responses carry an `x-request-id` header. Send your own `x-request-id` and QwryAI echoes it back; otherwise it generates one. Include it when you contact support about an error. ``` x-request-id: 3f6c2a9e-1b7d-4e58-9c0a-5d2e8f7b4a61 ``` ## Authentication errors | HTTP status | Chat API code | Tools API code | When | | - | - | - | - | | 401 | `AUTH_MISSING_API_KEY` | `API_KEY_REQUIRED` | No credential was sent. | | 401 | `AUTH_INVALID_API_KEY` | `INVALID_API_KEY` / `INVALID_OAUTH_TOKEN` | The credential is malformed, deleted or unknown. | | 401 | `AUTH_EXPIRED_API_KEY` | `AUTH_EXPIRED_API_KEY` | The key has expired. | | 403 | `SUBSCRIPTION_API_RESTRICTED_PLAN` | `API_ACCESS_PLAN_REQUIRED` | The workspace's plan doesn't include API access. | | 403 | `AUTH_INSUFFICIENT_PERMISSIONS` | `CREATOR_NOT_ACTIVE` | The key lacks **Chat with agents** (Chat API), or its creator is no longer in the workspace. | | 403 | — | `INSUFFICIENT_PERMISSION` | The key doesn't hold the permission this tool needs; `required` names it. | See [Error Handling](/docs/api-reference/v2/error-handling) for every code.