# Authentication

Every Briqpay API request uses HTTP Basic authentication with your merchant ID and a secret. Use your API key from the Merchant Portal, or generate a short-lived secret limited to one session or certain operations.

Every request to the Briqpay API must be authenticated. You use HTTP Basic authentication, with your merchant ID as the username and a secret as the password.

## The header

Send the credentials in the `Authorization` header:

```http title="Authentication"
Authorization: Basic <base64(merchantId:secret)>
```

Base64-encode `merchantId:secret` as one string. Make these calls from your backend only. The secret must never reach the browser.

## Get your credentials

Your merchant ID and API keys are on the keys page in the [Merchant Portal](https://app.briqpay.com/dashboard/keys?test=1). If you don't have an account yet, register there to get credentials for the playground.

## Environments

| Environment | Base URL                             |
| ----------- | ------------------------------------ |
| Playground  | `https://playground-api.briqpay.com` |
| Production  | `https://api.briqpay.com`            |

Build and test against the playground first.

## Which secret to use

You can use one of two kinds of secret as the password.

### The API key

This is the recommended way. Use an API key from the Merchant Portal. It gives full access to the API.

### A generated secret

For more control, call [Generate a secret](https://developer.briqpay.com/api-reference/generate-a-secret.md) with your merchant ID and API key. You get back a `secret` and an `expiresAt` time. Use the `secret` as the password. Generated secrets start with `generated_secret_`.

In the request you can set:

- `lifetime`: a `unit` (`second`, `minute`, `hour` or `day`) and a whole-number `count`. A secret lasts one day if you leave `lifetime` out, and at most seven days: a longer lifetime is rejected with a `400`.
- `scopes.operations`: the operations the secret may be used for. If you leave it out, all operations are allowed.
- `scopes.sessionId`: limits the secret to one session. The session must be one of yours; otherwise the request answers `404`.

The operations you can list are `create_session`, `read_session`, `update_session`, `update_session_metadata`, `validate_session`, `update_cart`, `update_references`, `capture_order`, `refund_order`, `cancel_order`, `release_remaining_order`, `create_hpp`, `update_hpp`, `read_hpp`, `read_token`, `patch_token` and `delete_token`.

```json title="Create sessions for 48 hours"
{
  "lifetime": { "unit": "hour", "count": 48 },
  "scopes": { "operations": ["create_session"] }
}
```

```json title="Anything on one session for 24 hours"
{
  "lifetime": { "unit": "hour", "count": 24 },
  "scopes": { "sessionId": "dcea4c10-d67e-4ce3-8826-a16c7bbc4771" }
}
```

```json title="Read one session for one minute"
{
  "lifetime": { "unit": "minute", "count": 1 },
  "scopes": {
    "operations": ["read_session"],
    "sessionId": "dcea4c10-d67e-4ce3-8826-a16c7bbc4771"
  }
}
```

## When authentication fails

Errors come back as `{ "error": { "code": "...", "message": "..." } }`.

| Status | Code                  | Cause                                                                             |
| ------ | --------------------- | --------------------------------------------------------------------------------- |
| `401`  | `INVALID_CREDENTIALS` | The `Authorization` header is missing or not valid Basic.                         |
| `403`  | `INVALID_CREDENTIALS` | The API key is not valid, or the call is outside a generated secret's scopes.     |
| `403`  | `INVALID_API_KEY`     | The generated secret doesn't exist, or was removed seven days after its creation. |
| `403`  | `AUTH_SECRET_EXPIRED` | The generated secret has expired.                                                 |

> [!NOTE]
> The `401` message says "invalid Authentication header". The header you must send is `Authorization`.

If a generated secret expires, generate a new one. A request outside a secret's scopes is refused, so check the `scopes` you asked for.

[Generate a secret](https://developer.briqpay.com/api-reference/generate-a-secret.md) itself only accepts your API key, not a generated secret. With a wrong merchant ID or key it answers `400` with the code `INVALID_DATA`.
