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

# API 身份认证

Unitlab HTTP API 身份认证：在每个请求的 Authorization 请求头中发送工作区 API 密钥，并处理 401、403 等认证与权限错误。

使用 `Api-Key` 方案，在 `Authorization` 请求头中发送密钥：

```bash
curl -s https://api.unitlab.ai/api/sdk/projects/ \
  -H "Authorization: Api-Key $UNITLAB_API_KEY"
```

```python
import os, httpx

api = httpx.Client(
    base_url="https://api.unitlab.ai/api/sdk/",
    headers={"Authorization": f"Api-Key {os.environ['UNITLAB_API_KEY']}"},
    timeout=60,
)
print(api.get("projects/").json())
```

密钥决定了所属工作区，不存在工作区参数。

在 **Settings** > **API keys** 中创建和管理密钥。参见 [身份认证与 API 密钥](https://docs.unitlab.ai/api-sdk-cli/zh/get-started/authentication-and-api-keys.md)。

## 响应 <a href="#responses" id="responses"></a>

| 状态码 | 响应体                                                           | 含义                          |
| --- | ------------------------------------------------------------- | --------------------------- |
| 401 | `{"detail": "Authentication credentials were not provided."}` | 未提供密钥、密钥错误，或密钥已停用、已删除、已过期   |
| 403 | `{"detail": "...", "code": "permission_denied"}`              | 密钥缺少该路由所需的权限，或该操作需要特定创建者的密钥 |
| 403 | `{"detail": "...", "code": "workspace_read_only"}`            | 工作区当前无法修改，读操作仍然可用。          |
| 403 | 其他 `code`                                                     | 套餐或配额不允许该操作                 |

## 带签名的链接 <a href="#signed-links" id="signed-links"></a>

部分响应包含指向媒体或下载内容的签名链接，例如 `source_url`、`file` 和 `upload_url`。请求这些链接时**不要**带 `Authorization` 请求头。链接会过期；请向 API 重新获取，不要长期保存。

## 相关内容 <a href="#related" id="related"></a>

[API 概览](https://docs.unitlab.ai/api-sdk-cli/zh/apis/api-overview.md) · [错误、限流与分页](https://docs.unitlab.ai/api-sdk-cli/zh/apis/errors-rate-limits-and-pagination.md)
