# 身份验证

使用 API 密钥或工作负载身份联合向 Claude API 进行身份验证。

---

Claude API 支持两种验证请求的方式：

| 方法 | 凭据 | 最适合 |
|------|------|--------|
| [API 密钥](#api-密钥) | 在 `x-api-key` 头中的长期 `sk-ant-api...` 密钥 | 本地开发、原型设计、脚本和您控制密钥存储的单租户服务器 |
| [工作负载身份联合](#工作负载身份联合) | 从您的身份提供商的身份 token 交换的短期承载 token | 云平台（AWS、Google Cloud、Azure）上的生产工作负载、CI/CD 管道和 Kubernetes，您希望消除静态密钥 |

两种方法授予对 Claude API 端点的相同访问权限。选择 API 密钥快速开始，当您的工作负载已经具有可以联合的平台颁发的身份时，迁移到工作负载身份联合。

## API 密钥

API 密钥是您在 Claude 控制台中生成并在每个请求上传递的静态密钥。

- **创建密钥：** 转到 Claude 控制台中的[设置 → API 密钥](https://platform.claude.com/settings/keys)。使用[工作区](https://platform.claude.com/settings/workspaces)按项目或环境限定密钥范围。
- **发送密钥：** 在直接 HTTP 请求上设置 `x-api-key` 头，或设置 `ANTHROPIC_API_KEY` 环境变量，[客户端 SDK](/docs/en/api/client-sdks) 会自动获取它。

```http
POST /v1/messages
x-api-key: YOUR_API_KEY
anthropic-version: 2023-06-01
content-type: application/json
```

API 密钥没有过期时间。将它们存储在密钥管理器中，定期轮换，并撤销您怀疑已泄露的任何密钥。

<CodeGroup>

```bash cURL
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-4-7",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello, Claude"}]
  }'
```

```python Python hidelines={1..2}
from anthropic import Anthropic

client = Anthropic(api_key="my-anthropic-api-key")
# 或者，在环境中设置 ANTHROPIC_API_KEY：
client = Anthropic()
```

```typescript TypeScript hidelines={1..2}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ apiKey: "my-anthropic-api-key" });
// 或者，在环境中设置 ANTHROPIC_API_KEY：
// const client = new Anthropic();
```

```go Go hidelines={1..8,12..13}
package main

import (
	"github.com/anthropics/anthropic-sdk-go"
	"github.com/anthropics/anthropic-sdk-go/option"
)

func main() {
	client := anthropic.NewClient(
		option.WithAPIKey("sk-ant-api03-..."), // 默认为 os.LookupEnv("ANTHROPIC_API_KEY")
	)
	_ = client
}
```

```java Java nocheck
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;

// 显式
AnthropicClient client = AnthropicOkHttpClient.builder()
  .apiKey("my-anthropic-api-key")
  .build();

// 从 ANTHROPIC_API_KEY（或 anthropic.apiKey 系统属性）
AnthropicClient clientFromEnv = AnthropicOkHttpClient.fromEnv();
```

```csharp C#
using Anthropic;

AnthropicClient client = new() { ApiKey = "my-anthropic-api-key" };
// 或者，在环境中设置 ANTHROPIC_API_KEY：
// AnthropicClient client = new();
```

```php PHP hidelines={1..4}
<?php

use Anthropic\Client;

// 从环境读取 ANTHROPIC_API_KEY
$client = new Client();
// 或者显式传递密钥：
$client = new Client(apiKey: 'my-anthropic-api-key');
```

```ruby Ruby hidelines={1..2}
require "anthropic"

anthropic = Anthropic::Client.new(api_key: "my-anthropic-api-key")
# 或者，在环境中设置 ANTHROPIC_API_KEY：
anthropic = Anthropic::Client.new
```

```bash CLI
# 有关 zsh、bash 和 Windows 变体，请参阅 /docs/en/api/sdks/cli#api-key
export ANTHROPIC_API_KEY=sk-ant-api03-...
```

</CodeGroup>

## 工作负载身份联合

工作负载身份联合（WIF）允许工作负载使用您已经信任的身份提供商（IdP）颁发的短期身份 token 进行身份验证，例如 AWS IAM、Google Cloud 或任何符合标准的 OIDC 颁发者（如 GitHub Actions、Kubernetes 服务账户、SPIFFE、Microsoft Entra ID 或 Okta）。工作负载在 `POST /v1/oauth/token` 处将其 IdP 颁发的 JWT 交换为短期 Claude API 访问 token，SDK 在 token 过期前自动刷新它。没有需要生成、分发或轮换的 `sk-ant-api...` 字符串。

联合从您的环境中移除了长期 Claude API 密钥，这缩小了泄露凭据的影响范围，并让您使用已用于云资源的相同 IdP 控制来管理访问。它本身并不保证端到端安全性：信任链仅与您的身份提供商配置一样强，上游一跳的长期密钥（例如，可以生成 IdP token 的静态云凭据）仍然可能破坏它。将联合与您提供商的控制相结合，例如 IP 允许列表、MFA 和审计日志。

要配置联合，您需要在 Claude 控制台中创建三个资源（服务账户、联合颁发者和联合规则），然后将您的 SDK 指向该规则。有关完整的设置演练，请参阅[工作负载身份联合](/docs/en/manage-claude/workload-identity-federation)。

## 后续步骤

<CardGroup cols={2}>
  <Card title="设置工作负载身份联合" icon="lock" href="/docs/en/manage-claude/workload-identity-federation">
    配置颁发者、规则和服务账户，然后交换 token
  </Card>
  <Card title="身份提供商指南" icon="cloud" href="/docs/en/manage-claude/workload-identity-federation#identity-providers">
    AWS、Google Cloud、Azure、GitHub Actions、Kubernetes、SPIFFE 和 Okta 的分步指南
  </Card>
  <Card title="WIF 参考" icon="book" href="/docs/en/manage-claude/wif-reference">
    环境变量、验证规则、配置文件配置和错误参考
  </Card>
  <Card title="客户端 SDK" icon="code" href="/docs/en/api/client-sdks">
    Python、TypeScript、Go、Java、C#、Ruby、PHP 和 CLI
  </Card>
</CardGroup>
