# Authentication

> Authenticate to the Hapti API with a bearer key. Test keys are free; live keys run real tasks and are billed only when a task succeeds.

Every request carries an API key in the `Authorization` header:

```bash
curl https://api.hapti.dev/v1/me -H "Authorization: Bearer hp_test_..."
```

Create and revoke keys in the [console](https://app.hapti.dev), under **Developers**. A key belongs to your organisation and to one mode.

## Test and live

| | Test keys `hp_test_` | Live keys `hp_live_` |
| --- | --- | --- |
| Price | Always free | Billed only when a task succeeds |
| What the agent does | Works on the real site, but holds back any request that would send the data you gave, so nothing is submitted for real | Does the task for real |
| Data | Tasks, people and webhooks of the test mode only | Tasks, people and webhooks of the live mode only |

Build and test with a test key, then switch the key. Your code does not change.

## OAuth for AI apps

AI apps such as Claude connect to the [MCP server](/docs/agents) with OAuth instead of a key: the person signs in to Hapti, chooses the organisation and the mode, and approves. The app then holds an access token that lasts one hour and renews itself, tied to that person. It stops working as soon as the connection is removed in **Developers**, or the person leaves the organisation. Hapti follows the MCP authorization specification: protected resource metadata at `/.well-known/oauth-protected-resource`, dynamic client registration, client metadata documents and PKCE.

## Keep keys secret

- Keep keys on your server, never in a browser, a mobile app or a public repository.
- Give each service its own key, so you can revoke one without stopping the others.
- Revoke a key at once if it leaks. Requests made with it are listed in **Developers, Requests**, with the address they came from.

A missing or wrong key gets a `401`:

```json
{
  "error": {
    "type": "authentication_error",
    "code": 401,
    "message": "a valid key is required: Authorization: Bearer hp_live_… or hp_test_…"
  }
}
```
