> ## Documentation Index
> Fetch the complete documentation index at: https://apixo.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# API 身分驗證 · 建立並使用 APIXO API 金鑰

> 使用 APIXO API 金鑰驗證請求。在控制台建立金鑰，妥善保管並定期更換。

所有 APIXO API 請求均需使用 Bearer 令牌進行身分驗證。

## 取得 API 金鑰

**Step 1**

### 建立賬號

如果還沒有賬號，請在 [apixo.ai](https://apixo.ai) 註冊。

**Step 2**

### 開啟控制台

進入控制台（**Dashboard**），開啟 API 金鑰（**API Keys**）頁面。

**Step 3**

### 建立金鑰

點選 **Create New Key**（建立新金鑰），輸入名稱並立即複製金鑰。

> [!WARNING]
>
> API 金鑰只顯示一次，請妥善儲存。

## 使用 API 金鑰

在每次請求的 `Authorization` 請求標頭中傳入 API 金鑰。

```
Authorization: Bearer YOUR_API_KEY
```

> [tab] cURL
>
> ```bash theme={null}
> curl -X POST https://api.apixo.ai/api/v1/generateTask/nano-banana \
>   -H "Authorization: Bearer YOUR_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "request_type": "async",
>     "input": {
>       "mode": "text-to-image",
>       "prompt": "A sunset",
>       "aspect_ratio": "16:9"
>     }
>   }'
> ```

> [tab] JavaScript
>
> ```javascript theme={null}
> const response = await fetch('https://api.apixo.ai/api/v1/generateTask/nano-banana', {
>   method: 'POST',
>   headers: {
>     'Authorization': 'Bearer YOUR_API_KEY',
>     'Content-Type': 'application/json',
>   },
>   body: JSON.stringify({
>     request_type: 'async',
>     input: {
>       mode: 'text-to-image',
>       prompt: 'A sunset',
>       aspect_ratio: '16:9',
>     }
>   }),
> });
>
> const data = await response.json();
> console.log(data.data.taskId);
> ```

> [tab] Python
>
> ```python theme={null}
> import requests
>
> response = requests.post(
>     'https://api.apixo.ai/api/v1/generateTask/nano-banana',
>     headers={
>         'Authorization': 'Bearer YOUR_API_KEY',
>         'Content-Type': 'application/json',
>     },
>     json={
>         'request_type': 'async',
>         'input': {
>             'mode': 'text-to-image',
>             'prompt': 'A sunset',
>             'aspect_ratio': '16:9',
>         }
>     }
> )
>
> data = response.json()
> print(data['data']['taskId'])
> ```

## 安全最佳實踐

> [card] 避免在用戶端程式碼中暴露金鑰
>
> API 金鑰僅應用於伺服器端應用。

> [card] 使用環境變數
>
> 將金鑰儲存在 `.env` 檔案中，切勿提交至 Git。

> [card] 定期更換金鑰
>
> 定期建立新金鑰並撤銷舊金鑰

> [card] 設定 IP 允許列表
>
> 在控制台中限制金鑰的使用，僅允許指定的 IP 位址。

## 錯誤回應

身分驗證失敗時會收到以下回應。

```json theme={null}
{
  "code": 401,
  "message": "Invalid or missing API key"
}
```

常見原因包括以下幾種。

* 缺少 `Authorization` 請求標頭
* API 金鑰無效或已被撤銷
* 金鑰沒有呼叫請求中指定模型的權限

## 下一步

完成身分驗證後，按[快速入門](https://apixo.ai/docs/zh-hant/quickstart.md)生成第一張影像。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.