> For the complete documentation index, see [llms.txt](https://docs.senmo.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.senmo.io/segment-ledger/authentication.md).

# Authentication

Authentication can be handled in two layers, depending on the endpoint:

#### 1. User Authentication

Some endpoints are protected through a user authentication token, with the frontend passing the user token to a Next.js API handler.

#### 2. Ledger API Key Authentication

Relevant ledger endpoints also require an API key sent through the header:

```
x-api-key: YOUR_LEDGER_API_KEY
```

The API validates that the `x-api-key` matches the `api_key` stored on the target ledger.

If the header is missing or invalid, the API returns an authorization error instead of a generic server error.

#### Common Response Format

**Successful response example:**

```json
{
    "data": {
        "id": "ledger_123",
        "name": "Main Ledger"
    }
}
```

**Error response example:**

```json
{
  "error": "Invalid API key"
}
```

Depending on the layer configuration, responses may also include standardized metadata such as trace IDs, status codes, or internal error codes.
