Claude Platform Docs
Messages入門

開始使用 Claude

對 Claude 發出您的第一個 API 呼叫,並建構一個簡單的網頁搜尋助理。

先決條件

呼叫 API

  1. 設定您的 API 金鑰

    將您的 API 金鑰匯出為環境變數。SDK 會自動讀取 ANTHROPIC_API_KEY

    export ANTHROPIC_API_KEY="your-api-key-here"
  2. 建立專案並安裝 SDK

    mkdir claude-quickstart && cd claude-quickstart
    python3 -m venv .venv && source .venv/bin/activate
    pip install anthropic
  3. 建立您的程式碼

    建立一個名為 quickstart.py 的檔案:

    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    message = client.messages.create(
        model="claude-opus-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)
  4. 執行您的程式碼

    python quickstart.py
    Output
    Here are some effective search strategies to find the latest developments in renewable energy:
    
    ## General Search Terms
    - "Renewable energy news 2025"
    - ...

後續步驟

您已完成第一個 API 呼叫。接下來,學習您在每個 Claude 整合中都會用到的 Messages API 模式。

學習多輪對話、「system prompt」(系統提示)、停止原因以及其他核心模式。

當您熟悉基礎知識後,可進一步探索:

依能力與成本比較 Claude 模型。

瀏覽所有 Claude 功能:工具、上下文管理、結構化輸出等。

Python、TypeScript、C# 及其他用戶端函式庫的參考文件。

比較 API 金鑰與 Workload Identity Federation,並設定金鑰到期時間。

Was this page helpful?