# Keys and authentication

> Test and live keys, how to send them, what a key may touch, and what to do if one leaks.

Every request carries a key. A key is two parts:

| Part | Looks like | Is it secret? |
| --- | --- | --- |
| Key ID | `ck_live_8Fq2LmZ0pR4tV6xY9aBcDe` | No. Safe to show and to log. |
| Secret | `cs_live_...` (43 more characters) | Yes. Shown once, when you make the key. |

Test keys start `ck_test_` and `cs_test_`.

## Sending the key

Use either of these. Both work on every endpoint.

**HTTP Basic**: the key ID as the username and the secret as the password. Most tools do this for you:

```bash
curl -u "$CALLVIEW_KEY_ID:$CALLVIEW_SECRET" 'https://v2-api.callview.ai/api/v1/external/campaigns'
```

**Bearer**: the secret alone:

```bash
curl -H "Authorization: Bearer $CALLVIEW_SECRET" 'https://v2-api.callview.ai/api/v1/external/campaigns'
```

Always use `https://`. Never send a key over plain `http://`: the request is not processed, but the key has already crossed the network unencrypted, so roll it. Today a plain `http://` request gets a `301` redirect to `https://` (don't follow it: many HTTP clients turn a redirected `POST` into a `GET`); behind CallView's edge it gets a `403` and never reaches the API.

A missing, wrong, revoked or expired key gets `401` with the code `authentication_failed`. The `reason` field says which one. After 20 failed tries from one address in 5 minutes, that address is blocked for 15 minutes (`429`).

Keys work only on the API described here (`/api/v1/external`). They can't sign in to the CallView app or reach anything else.

## Test keys and live keys

A **live key** works with your real leads and calls, and the leads you add get called. A **test key** doesn't dial anyone and costs nothing. It can't see or change real leads or calls: it gets sample data in the same shapes, so you can build your whole integration with it. See [Test mode](https://developers.callview.ai/guides/test-mode/).

Both see your real campaign IDs and names, so moving from test to live means changing the key and nothing else.

## What a key may touch

When you make a key in **Settings > API**, you choose:

- **Which campaigns.** A key limited to some campaigns can't see or add leads anywhere else. Asking about another campaign gets `403 permission_denied`.
- **What it can do.** Read or Write, area by area (the table below). A request outside that gets `403 permission_denied`, and `missing_permission` names what's missing, for example `dialing:write`.
- **A lead source** (optional): stamped on every lead the key adds, so you can tell where leads came from.
- **Allowed IP addresses** (optional): requests from anywhere else are refused.
- **When it stops working** (optional).
- **Limits**: requests and new leads a minute, see [Rate limits](https://developers.callview.ai/guides/rate-limits/).
- **Day limits** (optional): calls per day and a daily spend cap, for this key alone. See [Money guards](https://developers.callview.ai/guides/money-guards/#limits-for-one-key).

### The permissions

| Permission | In the app | What it allows |
| --- | --- | --- |
| `leads:read` | Leads: read | See leads and where they are |
| `leads:write` | Leads: write | Update a lead (`PATCH /leads/{id}`) and opt it out. Adding a lead needs Start calls too. |
| `calls:read` | Calls: read | See calls, outcomes and notes |
| `calls:write` | Calls: write | Call a lead now (`POST /calls`), together with Start calls |
| `dialing:write` | Start calls | Anything that puts a lead in a dial queue or starts dialing: `POST /leads`, `POST /leads/batch`, `POST /campaigns/{id}/leads` and its batch, `POST /calls`, and starting a campaign (`POST /campaigns/{id}/activate`). A `dry_run` needs it too. |
| `recordings:read` | Recordings & transcripts | `GET /calls/{id}/recording` and `GET /calls/{id}/transcript`, together with Calls: read |
| `campaigns:read` | Campaigns: read | See the campaigns |
| `campaigns:write` | Campaigns: write | Start and pause them. Starting one (`POST /campaigns/{id}/activate`) needs Start calls too, because a started campaign dials its waiting leads. |
| `callbacks:read` | Callbacks: read | See booked callbacks |
| `callbacks:write` | Callbacks: write | Book and cancel them |
| `dnc:read` | Do Not Call: read | Check numbers |
| `dnc:write` | Do Not Call: write | Add and remove numbers |
| `webhooks:read` | Webhooks: read | See endpoints and deliveries |
| `webhooks:write` | Webhooks: write | Add, change and re-send |
| `events:read` | Events: read | See events |
| `usage:read` | Usage: read | See usage and cost. Only the organization's owner can give it. |

Start calls is its own box because adding a lead to a running campaign means the AI phones that person. A key with Leads: write and no Start calls can keep your leads up to date but can't start a call.

Recordings & transcripts is its own box because a recording is what the lead actually said. A key can read every outcome and note without it.

### The ready-made sets

Three sets fill in the boxes for you. Change any box afterwards.

| Set | Campaigns | Permissions |
| --- | --- | --- |
| CRM speed to lead | You pick them | `leads:read`, `leads:write`, `calls:read`, `dialing:write`, `events:read` |
| Reporting only | All | `leads:read`, `calls:read`, `campaigns:read`, `callbacks:read`, `dnc:read`, `webhooks:read`, `events:read`, `usage:read` (each read permission except recordings and transcripts) |
| Full access | All | Every permission. Only the organization's owner can make one. |

Give each system its own key, with only what it needs. A CRM that sends leads and reads outcomes needs Leads (read and write), Start calls, Calls (read) and Events (read): the CRM speed to lead set. Tick Recordings & transcripts as well only if the CRM stores recordings or transcripts.

Keys made before Start calls and Recordings & transcripts existed kept what they could do: every key with Leads: write, Calls: write or Campaigns: write was given Start calls, and every key with Calls: read was given Recordings & transcripts. Untick them on keys that don't need them.

## Keeping keys safe

Keep the secret on your server. Don't put it in a web page, a mobile app or a public repository.

- **Roll** a key in Settings > API to get a new secret without making a new key. The old secret keeps working for a grace period you choose (24 hours unless you pick another, at most 7 days), so you can switch over without dropping a request.
- **Revoke** a key you no longer use. It stops working on the next request.
- If a secret leaks, revoke the key right away, or roll it with a grace period of 0 so the leaked secret dies at once.
