> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/api/api-doc-en/preparation/authentication.md).

# API Authentication

MaiAgent uses API keys for authentication.

## Authentication Method

All API requests must include an API Key in the HTTP Header, in the following format:

```javascript
const headers = {
  "Authorization": "Api-Key YOUR_API_KEY_HERE",
  "Content-Type": "application/json"
}
```

## How to Obtain an API Key?

Please log in to the MaiAgent system backend and follow these steps:

1. Click the "Username" dropdown menu in the upper right corner.
2. Click "Account" to go to the profile page.

<div><figure><img src="/files/Tq5CJdFvIXrCB7pCEXam" alt=""><figcaption></figcaption></figure> <figure><img src="/files/CaHvrAIO9nkK5XKGVlhN" alt=""><figcaption></figcaption></figure></div>

2. Switch the page to the API Key page.

<figure><img src="/files/osuOAqFxZJDiT5LuI5Ca" alt=""><figcaption></figcaption></figure>

3. You can then copy and view the "API Key".

<figure><img src="/files/7gX1bZsUKzXIxIM2dkCV" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}

* **Security Reminder**: Please keep your API key secure and do not share it in public places.
* **Key Format Description**: The API key is a combination of alphanumeric characters.
  {% endhint %}

## Error Handling

When authentication fails, the API will return a 401 Unauthorized HTTP error code:

* **Invalid or Missing API Key**

```json
{
  "detail": "Authentication credentials were not provided."
}
```

*Fix suggestion: Check if the request header contains the correct Authorization field. The format should be: `Authorization: Api-Key YOUR_API_KEY`*

* **Incorrect API Key Format**

```json
{
  "detail": "Invalid API key format."
}
```

*Fix suggestion: Confirm that the API Key format is correct. Check if the correct prefix `Api-Key` is used instead of `Bearer`, and ensure the API Key itself does not have extra spaces or characters.*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.maiagent.ai/api/api-doc-en/preparation/authentication.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
