SDK / API 文档

SDK 使用与 CLI 相同的环境变量配置。请先按 CLI 配置 完成环境变量设置; 如需手动配置,可参考下方示例(服务地址请按安装指南填写)。

统一配置(Claude SDK)

当前本页主要提供 Claude SDK 示例。Claude SDK 使用 ANTHROPIC_BASE_URLANTHROPIC_API_KEYsk-* 替换为你自己的 Claude Code Key,服务地址按安装指南填写。

macOS / Linux
--
Windows(PowerShell / CMD)
执行后请重新打开终端。
--

Claude 官方 SDK 列表

以下为 Claude 官方 SDK 示例,支持通过环境变量或代码设置服务地址。若示例中出现 <SERVICE_URL>,请替换为 Claude Code 安装指南中的服务地址。

Python SDK
系统要求
Python 3.8+
安装
pip install anthropic
基础使用
import os\nfrom anthropic import Anthropic\n\nclient = Anthropic(api_key=os.environ.get(\"ANTHROPIC_API_KEY\"))\nmessage = client.messages.create(\n    max_tokens=1024,\n    messages=[{\"role\": \"user\", \"content\": \"Hello, Claude\"}],\n    model=\"claude-sonnet-4-5\",\n)\nprint(message.content)
自定义服务地址
from anthropic import Anthropic\n\nclient = Anthropic(\n    api_key=\"你的密钥\",\n    base_url=\"<SERVICE_URL>\",\n)
TypeScript SDK
安装
npm install @anthropic-ai/sdk
基础使用
import Anthropic from '@anthropic-ai/sdk';\n\nconst client = new Anthropic({\n  apiKey: process.env['ANTHROPIC_API_KEY'],\n});\n\nconst message = await client.messages.create({\n  max_tokens: 1024,\n  messages: [{ role: 'user', content: 'Hello, Claude' }],\n  model: 'claude-sonnet-4-5',\n});\n\nconsole.log(message.content);
自定义服务地址
const client = new Anthropic({\n  apiKey: '你的密钥',\n  baseURL: '<SERVICE_URL>',\n});
Java SDK
系统要求
Java 8+
安装
implementation(\"com.anthropic:anthropic-java:2.10.0\")
基础使用
AnthropicClient client = AnthropicOkHttpClient.fromEnv();\nMessageCreateParams params = MessageCreateParams.builder()\n    .maxTokens(1024L)\n    .addUserMessage(\"Hello, Claude\")\n    .model(\"claude-sonnet-4-5\")\n    .build();\nMessage message = client.messages().create(params);
自定义服务地址
AnthropicClient client = AnthropicOkHttpClient.builder()\n    .apiKey(\"你的密钥\")\n    .baseUrl(\"<SERVICE_URL>\")\n    .build();
Go SDK
系统要求
Go 1.22+
安装
go get -u github.com/anthropics/anthropic-sdk-go@v1.17.0
基础使用
client := anthropic.NewClient(\n    option.WithAPIKey(os.Getenv(\"ANTHROPIC_API_KEY\")),\n)
自定义服务地址
client := anthropic.NewClient(\n    option.WithAPIKey(\"你的密钥\"),\n    option.WithBaseURL(\"<SERVICE_URL>\"),\n)
C# SDK(Beta)
系统要求
.NET 8+
安装
dotnet add package Anthropic
基础使用
var client = new AnthropicClient(\n    apiKey: Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\")\n);
自定义服务地址
var client = new AnthropicClient(\n    apiKey: \"你的密钥\",\n    new ClientOptions { BaseUrl = \"<SERVICE_URL>\" }\n);
Ruby SDK
系统要求
Ruby 3.2+
安装
gem \"anthropic\", \"~> 1.13.0\"\n\nbundle install
基础使用
client = Anthropic::Client.new(\n  api_key: ENV['ANTHROPIC_API_KEY']\n)
自定义服务地址
client = Anthropic::Client.new(\n  api_key: '你的密钥',\n  base_url: '<SERVICE_URL>'\n)
PHP SDK
系统要求
PHP 8.1+
安装
composer require anthropic/anthropic-sdk-php
基础使用
$client = Anthropic::client(getenv('ANTHROPIC_API_KEY'));
自定义服务地址
$client = Anthropic::factory()\n    ->withApiKey('你的密钥')\n    ->withBaseUri('<SERVICE_URL>')\n    ->make();