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

# 认证

> 为 API 请求配置认证

## API Key 认证

所有 Nonce 私有 API 请求都需要认证：在 `Authorization` 请求头中携带 Workspace API Key。

### 请求头格式

使用标准的 Bearer Token 格式：

```bash theme={null}
curl -X GET "https://api.nonce.app/private-api/v1/{workspace_id}/farms" \
  -H "Authorization: Bearer your_api_key_here"
```

### 获取 API Key

API Key 在 Nonce 控制台生成：

1. 登录 [Nonce 控制台](https://nonce.app)
2. 切换到需要通过 API 访问的工作区
3. 进入 **Organization** → **Manage** → **API Keys**
4. 点击 **Create API Key**，填写便于识别的名称
5. 复制并妥善保存生成的密钥，密钥只显示一次

<Frame caption="从控制台创建 API Key">
  <img src="https://mintcdn.com/nonce/tY66cGlSTnuTA6xD/images/create-api-keys.png?fit=max&auto=format&n=tY66cGlSTnuTA6xD&q=85&s=99216ae1497214477b0ba8a01eeeeb29" alt="创建 API Key" width="1808" height="1448" data-path="images/create-api-keys.png" />
</Frame>

<Warning>
  API Key 只在生成它的工作区内有效。请确认生成密钥的工作区与请求路径中的 `workspace_id` 一致。
</Warning>

### 认证错误

| 状态码 | 错误           | 描述                         |
| --- | ------------ | -------------------------- |
| 401 | Unauthorized | 缺少或无效的 `Authorization` 请求头 |
| 401 | Unauthorized | 无效、已撤销或已过期的 API Key        |
| 403 | Forbidden    | API Key 没有此工作区的访问权限        |

### 错误响应示例

```json theme={null}
{
  "success": false,
  "data": null,
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid or missing API key",
    "traceId": "92c47a3f5c1e4b28a6d0f9e83b7c5d12"
  }
}
```

### 安全最佳实践

* 不要在客户端代码中暴露 API Key
* 把 API Key 存放在环境变量中
* 定期轮换 API Key
* 为 Development 和 Production 环境使用各自独立的 API Key
* 及时在控制台撤销不再使用或已泄露的密钥
