> ## 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/quickstart.md)生成第一张图像。


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