# Usage API

Track the billed requests of your account over a period, in total and per API key — without going through the Staan console. Combined with the [API Keys API](/docs/api-keys), it lets you bill your own customers for the searches they make with the keys you created for them.

```
GET https://api.staan.ai/v1/usage
```

Data is available **up to the previous day**: the current day is not included.

## Authentication

Call this endpoint with an API key **created in the Staan console**:

```bash
Authorization: Bearer YOUR_CONSOLE_API_KEY
```

Keys created through [`POST /v1/keys`](/docs/api-keys#create-a-key) can only be used for search: on `/v1/usage` they get `403 SCOPE_NOT_GRANTED`.

## Quick start

```bash title="Daily usage for October"
curl "https://api.staan.ai/v1/usage?from=2026-10-01&to=2026-10-31&granularity=daily" \
  -H "Authorization: Bearer YOUR_CONSOLE_API_KEY"
```

```bash title="Monthly usage of one key"
curl "https://api.staan.ai/v1/usage?from=2026-01-01&to=2026-12-31&granularity=monthly&key_id=6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b" \
  -H "Authorization: Bearer YOUR_CONSOLE_API_KEY"
```

## Parameters

| Parameter     | Required | Description                                    |
| ------------- | -------- | ---------------------------------------------- |
| `from`        | yes      | Start of the period, inclusive, `YYYY-MM-DD`   |
| `to`          | yes      | End of the period, inclusive, `YYYY-MM-DD`     |
| `granularity` | no       | `daily` (default) or `monthly`                 |
| `key_id`      | no       | Restrict the result to one key of your account |

The period cannot exceed **366 days**, and `from` must be before or equal to `to`.

## Response

```json
{
  "from": "2026-10-01",
  "to": "2026-10-31",
  "granularity": "daily",
  "total": { "web_search": 410, "web_search_for_ai": 0, "total": 410 },
  "keys": [
    {
      "key_id": "6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b",
      "name": "Customer A",
      "total": { "web_search": 410, "web_search_for_ai": 0, "total": 410 },
      "usage": [
        {
          "date": "2026-10-01",
          "web_search": 12,
          "web_search_for_ai": 0,
          "total": 12
        }
      ]
    }
  ]
}
```

| Field                           | Description                                                      |
| ------------------------------- | ---------------------------------------------------------------- |
| `total`                         | Usage of the whole account over the period (or of `key_id` only) |
| `keys[].key_id` / `keys[].name` | Identifier and name of the key                                   |
| `keys[].total`                  | Usage of this key over the period                                |
| `keys[].usage[]`                | Usage of this key per day or per month                           |
| `date`                          | `YYYY-MM-DD` in `daily` mode, `YYYY-MM` in `monthly` mode        |
| `web_search`                    | Billed [Web Search](/docs/web-search) requests                   |
| `web_search_for_ai`             | Billed [Web Search for AI](/docs/web-for-ai) requests            |
| `total`                         | Sum of `web_search` and `web_search_for_ai`                      |

- Days or months without usage are omitted from `usage`.
- Deleted keys still count in `total`, and are listed in `keys` only if they had usage in the period.

## Rate limits and errors

This endpoint accepts **1 request per second, per key**. Above that it returns `429`: wait a second and retry.

| HTTP  | `code` / message                                                                             | Cause                                          |
| ----- | -------------------------------------------------------------------------------------------- | ---------------------------------------------- |
| `400` | `from must be before or equal to to`, `The period cannot exceed 366 days`, invalid parameter | Invalid parameters                             |
| `401` | `INVALID_KEY_FORMAT`                                                                         | Missing or malformed `Authorization` header    |
| `401` | `INVALID_CREDENTIALS`                                                                        | Unknown key or wrong secret                    |
| `403` | `KEY_DISABLED`                                                                               | The key is disabled                            |
| `403` | `SCOPE_NOT_GRANTED`                                                                          | Key created through the API: use a console key |
| `404` | not found                                                                                    | `key_id` does not belong to your account       |
| `410` | `KEY_EXPIRED`                                                                                | The key has expired                            |
| `429` | too many requests                                                                            | Rate limit exceeded                            |
