# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.senmo.io/segment-ledger/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
