# Authentication

All Claret API calls are authenticated with tokens. Before making a call, you must authenticate first. You do this by first creating an authentication token and then use that token in the header of the subsequent API calls.

**NOTE** that all references to `{tenant}` should be replaced with the name of the Claret tenant (i.e., `demo`, `zymoeno`, etc.)

<mark style="color:green;">`POST`</mark> `https://plan.claret.app/{tenant}/api/v1/tokens/create`

Retrieve an authentication token from the Claret API endpoint

#### Query Parameters

| Name                                       | Type   | Description                                                            |
| ------------------------------------------ | ------ | ---------------------------------------------------------------------- |
| email<mark style="color:red;">\*</mark>    | String | The email address associated with the user account being authenticated |
| password<mark style="color:red;">\*</mark> | String | The password associated with the user account being authenticated      |

{% tabs %}
{% tab title="200: OK " %}
A successful response will return a simple JSON payload containing the token value that should be used in each additional request.

**Example:**

```json
{
    "token": "1|bfDoNPffjU6k541RYSLB68oEfHySweWJ2HSx34sZ"
}
```

{% endtab %}
{% endtabs %}

Once the authentication token has been successfully retrieved, it can be used as a Bearer token within the header of each subsequent API call.

**Example:**

```
curl --location --request GET 'https://plan.claret.app/{tenant}/api/v1/sales?sale_type_name=History' \
--header 'X-Requested-With: XMLHttpRequest' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer 1|bfDoNPffjU6k541RYSLB68oEfHySweWJ2HSx34sZ' \
```

{% hint style="info" %}
The `X-Requested-With: XMLHttpRequest` header should be included on all authenticated API requests. This header tells the server to return JSON-formatted error responses. Without it, authentication failures (expired token, missing token, etc.) will return an HTML redirect to the login page instead of a JSON `401` response.
{% endhint %}


---

# 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.claret.app/apis/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.
