# Authentication (/docs/authentication)

> Pre-release: the public API is not live yet and has no base URL. Routes and fields are in the OpenAPI contract, at /openapi.yaml.

API keys, environments, and what happens when a key is rejected.

Every request to the API carries an API key in the `Authorization` header, with the `Bearer` scheme:

```http
Authorization: Bearer sk_test_…
```

The key identifies an **environment**, and the environment decides which data the request can see.

## Environments [#environments]

Every merchant has two environments, with separate data:

| Environment | Key prefix | What it moves |
|---|---|---|
| Test | `sk_test_` | Nothing: the bank is simulated. See [Test mode](/docs/test-mode) |
| Live | `sk_live_` | Real PIX payments |

A test key only reaches the test environment, and a live key only the live one. There is no parameter to switch environments: the key decides.

## The key [#the-key]

- It is shown **once**, when it is created. The platform stores only a hash of it, so a lost key cannot be recovered: create another.
- It can be revoked at any time. Revoking is permanent.
- Keep it on your server. Never send it to a browser or a mobile app, and never commit it to a repository.

## Getting a key [#getting-a-key]

A key is created in the dashboard, in **API keys**, under the mode it is for. Give it a name, to tell your keys apart later, choose what it can do with charges, with refunds, with offers and with the balance, and copy it then: the dashboard shows it once. A key cannot create another key.

Creating a key asks for your passkey again when you signed in more than 5 minutes ago. Revoking one never does.

## What a key can do [#what-a-key-can-do]

A key does only what its **scopes** say. Every operation asks the key for one, and a key has the scopes it was created with.

| Scope | What it allows |
|---|---|
| `charges:read` | Reading and listing charges, with `getCharge` and `listCharges` |
| `charges:write` | Creating a charge, with `createCharge`. With a test key, also the operations that simulate the bank. See [Test mode](/docs/test-mode) |
| `refunds:read` | Reading and listing refunds, with `getRefund` and `listRefunds` |
| `refunds:write` | Asking for a refund, with `createRefund`. It is the only scope that moves money out. See [Refunds](/docs/refunds) |
| `offers:read` | Reading and listing offers, with `getOffer` and `listOffers` |
| `offers:write` | Putting an offer on sale and archiving it, with `createOffer` and `archiveOffer`. See [Offers](/docs/offers) |
| `balance:read` | Reading the balance, with `getBalance`. See [Balance](/docs/balance) |

- **Give a key only what its job needs.** A key that only answers questions needs the `read` scopes and nothing else.
- **Writing does not include reading.** A key with `charges:write` alone creates a charge and cannot read it back: give it `charges:read` as well.
- **A key only has the scopes it was created with.** A scope is never added to a key that exists: to do something else through the API, create another key in the dashboard. The list of keys shows what each one can do.
- **A key is never created without a scope.** It would call no operation.
- **A key created before every operation asked for a scope** has the scopes of what it did then: `charges:read`, `charges:write` and `refunds:read`. It has `refunds:write` only if it was created with it.
- **An operation that arrives later asks for a scope that no earlier key has.** Offers and the balance came after the first keys: to use them, create a key with their scopes.
- In the [OpenAPI contract](/openapi.yaml), each operation names its scope in its `security`.
- Without the scope the answer is `403`, with the code `insufficient_scope`, and the `WWW-Authenticate` header names the scope that is missing. The key is valid: do not replace it for the other operations.
- The scope is checked right after the key, before the request is read: a key without it gets the same `403` whatever else the request has wrong.

## When a key is rejected [#when-a-key-is-rejected]

Without a valid key the answer is `401`, with the code `unauthorized`.

- A missing, malformed, unknown or revoked key gets exactly the same answer. The API does not say which case it was.
- The key is checked before anything else. Without a valid key the answer is `401` whether the route exists or not.
- Check first that the key belongs to the environment you expect.

## Rate limits [#rate-limits]

Requests are limited by API key and by IP address. Over the limit the answer is `429`, with the code `rate_limited`, and the `Retry-After` header says in how many seconds to try again.

When you retry the creation of a charge or of a refund, send the same `Idempotency-Key`.