跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

Anthropic

最后更新 查看 MarkdownAgent 设置

Anthropic ↗ 帮助构建可靠、可解释和可引导的 AI 系统。

端点

基础 URL

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic

向 /anthropic/v1/messages 提供供应商密钥时,请将其放在 x-api-key 中。不同版本的 SDK 可能使用不同的身份验证标头。在此端点上,AI Gateway 不会将 Authorization: Bearer 识别为供应商凭据。请另行使用 cf-aig-authorization 对 AI Gateway 进行身份验证。

有关已存储密钥和 Unified Billing,请参阅凭据优先级。如果需要供应商凭据但 AI Gateway 找不到,请参阅错误 2044。

示例

cURL

在请求中包含 API 密钥

curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
 --header 'x-api-key: {anthropic_api_key}' \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --header 'anthropic-version: 2023-06-01' \
 --header 'Content-Type: application/json' \
 --data  '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is Cloudflare?"}
    ]
  }'
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
 --header 'x-api-key: {anthropic_api_key}' \
 --header 'anthropic-version: 2023-06-01' \
 --header 'Content-Type: application/json' \
 --data  '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is Cloudflare?"}
    ]
  }'

使用存储密钥 (BYOK) / Unified Billing

curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --header 'anthropic-version: 2023-06-01' \
 --header 'Content-Type: application/json' \
 --data  '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is Cloudflare?"}
    ]
  }'

Anthropic SDK

提供 Anthropic 密钥时,请显式设置 x-api-key。不同版本 SDK 的身份验证标头可能不同。将 Authorization 设为 null 可移除 SDK 生成的 bearer 标头。请单独使用 cf-aig-authorization 对 AI Gateway 进行身份验证。

在请求中包含密钥

import Anthropic from "@anthropic-ai/sdk";

const baseURL = `https://gateway.ai.cloudflare.com/v1/{accountId}/{gatewayId}/anthropic`;
const providerKey = "{ANTHROPIC_API_KEY}";

const anthropic = new Anthropic({
    apiKey: providerKey,
    baseURL,
    defaultHeaders: {
        "x-api-key": providerKey,
        "cf-aig-authorization": "Bearer {CF_AIG_TOKEN}",
        Authorization: null,
    },
});

const message = await anthropic.messages.create({
    model: "claude-sonnet-4-5",
    messages: [{ role: "user", content: "What is Cloudflare?" }],
    max_tokens: 1024,
});
import Anthropic from "@anthropic-ai/sdk";

const baseURL = `https://gateway.ai.cloudflare.com/v1/{accountId}/{gatewayId}/anthropic`;
const providerKey = "{ANTHROPIC_API_KEY}";

const anthropic = new Anthropic({
    apiKey: providerKey,
    baseURL,
    defaultHeaders: {
        "x-api-key": providerKey,
        Authorization: null,
    },
});

const message = await anthropic.messages.create({
    model: "claude-sonnet-4-5",
    messages: [{ role: "user", content: "What is Cloudflare?" }],
    max_tokens: 1024,
});

使用存储密钥 (BYOK) / Unified Billing

import Anthropic from "@anthropic-ai/sdk";

const baseURL = `https://gateway.ai.cloudflare.com/v1/{accountId}/{gatewayId}/anthropic`;

const anthropic = new Anthropic({
    apiKey: "placeholder",
    baseURL,
    defaultHeaders: {
        "cf-aig-authorization": "Bearer {CF_AIG_TOKEN}",
        "x-api-key": null,
        Authorization: null,
    },
});

const message = await anthropic.messages.create({
    model: "claude-sonnet-4-5",
    messages: [{ role: "user", content: "What is Cloudflare?" }],
    max_tokens: 1024,
});

OpenAI 兼容端点

你也可以通过 REST API 使用 OpenAI API schema 访问 Anthropic 模型。将请求发送至:

https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions

指定:


{
      "model": "anthropic/{model}"
}

这篇文档对您有帮助吗?