Messages入门
Claude 快速入门
向 Claude 发起您的第一次 API 调用,并构建一个简单的网页搜索助手。
前提条件
- 一个 Claude Console 账户
- 一个 API 密钥
调用 API
设置您的 API 密钥
将您的 API 密钥导出为环境变量。SDK 会自动读取
ANTHROPIC_API_KEY。export ANTHROPIC_API_KEY="your-api-key-here"创建项目并安装 SDK
mkdir claude-quickstart && cd claude-quickstart python3 -m venv .venv && source .venv/bin/activate pip install anthropic编写代码
创建一个名为
quickstart.py的文件:Pythonimport anthropic client = anthropic.Anthropic() message = client.messages.create( model="claude-opus-5-5", max_tokens=1000, messages=[ { "role": "user", "content": "What should I search for to find the latest developments in renewable energy?", } ], ) for block in message.content: if block.type == "text": print(block.text)运行代码
python quickstart.pyOutputHere are some effective search strategies to find the latest developments in renewable energy: ## General Search Terms - "Renewable energy news 2025" - ...
后续步骤
您已经完成了第一次 API 调用。接下来,学习您将在每个 Claude 集成中使用的 Messages API 模式。
学习多轮对话、系统提示、停止原因以及其他核心模式。
熟悉基础知识后,可进一步探索:
Was this page helpful?