> 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/ja/preparation/authentication.md).

# API 認証

本ページでは、MaiAgent API を使用する際の認証方法と API キーの取得方法について説明します

MaiAgent では、API キーを使用して認証を行います。

## 認証方式

すべての API リクエストでは、HTTP Header に API Key を以下の形式で含める必要があります。

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

## API キーの取得方法

MaiAgent の管理画面にログインし、以下の手順に従って操作してください。

1. 右上の <mark style="color:blue;">ユーザー名</mark>ドロップダウンメニューをクリックします。
2. <mark style="color:blue;">アカウント</mark>をクリックして、プロフィールページを開きます。
3. <mark style="color:blue;">API キー</mark>ページに切り替えます。
4. <mark style="color:blue;">キーを作成</mark>をクリックし、名前を入力して、必要に応じて有効期限を設定します。有効期限を設定していないキーは自動的に期限切れになりません。
5. 作成直後に完全なキーをコピーし、安全に保管してください。

{% hint style="success" %}

* **セキュリティに関する注意**：API キーは適切に保管し、公開の場で共有しないでください
* **キー形式の説明**：API キーは英数字の組み合わせです
  {% endhint %}

{% hint style="warning" %}
完全なキーは、作成に成功した時点で一度だけ表示されます。その後、一覧には一部の文字のみが表示され、完全なキーを再表示したり復元したりすることはできません。保存しなかった場合は、新しいキーを作成し、そのキーを使用するサービスを更新してから、古いキーを失効させてください。
{% endhint %}

## 以前にシステムが発行したキーが見つからない場合 <a href="#system-issued-api-key" id="system-issued-api-key"></a>

個人アカウントの <mark style="color:blue;">API キー</mark>ページには、ご自身で作成したキーのみが表示されます。組織に初めて参加したとき（AD を使用して初めてログインした場合を含む）、システムによって有効期限のないキーが自動的に発行されることがありますが、その完全な値は提供されないため、そのキーを取得して API を呼び出すことはできません。API を呼び出すには、プロフィールページで新しいキーをご自身で作成し、作成に成功した時点で完全な値を直ちに保存してください。

システムによって自動的に発行されたキー、または管理者によって代理発行されたキーについては、組織のキーを管理する権限を持つメンバーのみが、<mark style="color:blue;">組織キー管理</mark>ページで所有者、プレフィックス、発行元、ステータス、有効期限を確認できます。ただし、このページにも完全な値は表示されません。この権限がない場合は、組織の所有者または組織のキーを管理する権限を持つメンバーに、古いキーの確認または失効を依頼してください。

## エラー処理

認証エラーが発生した場合、API は 401 Unauthorized の HTTP エラーコードを返します。

* **API Key が無効または欠落している**

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

*修正方法：リクエストヘッダーに正しい Authorization フィールドが含まれているかを確認してください。形式は次のとおりです： `Authorization: Api-Key YOUR_API_KEY`*

* **API Key の形式が誤っている**

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

*修正方法：API Key の形式が正しいことを確認してください。`Bearer` ではなく正しいプレフィックス `Api-Key` を使用していること、および API Key 自体に余分なスペースや文字が含まれていないことを確認してください。*


---

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