Skip to content
Academy ofAI Learning

~/academy $ claude learn first-api-call

Make your first Claude API call

How to get a Claude API key from the Console, install the Python SDK, send your first Messages API request and read what comes back. 8 min

Watch it done: make your first Claude API call in Python

$

Step 1 of 6

Make a folder for the project and move into it. You need Python 3.10 or later.

  1. Make a folder for the project and move into it. You need Python 3.10 or later. Type: mkdir claude-quickstart && cd claude-quickstart.
  2. Create a virtual environment and switch it on, so the SDK installs into this project only. Type: python3 -m venv .venv && source .venv/bin/activate.
  3. Install the official Python SDK. Type: pip install anthropic. pip downloads and installs the SDK and the packages it needs.
  4. Create a key on the API keys page of the Claude Console, then put it in this variable in place of the placeholder. The key never goes in your code. Type: export ANTHROPIC_API_KEY="your-api-key-here".
  5. Create quickstart.py. The client finds your key in the environment, and messages.create sends one request with the three required fields: model, max_tokens and messages. Create quickstart.py containing: import anthropic client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY message = client.messages.create( model="claude-opus-5-5", max_tokens=1000, messages=[ {"role": "user", "content": "Suggest three subject lines for our November newsletter."} ], ) for block in message.content: if block.type == "text": print(block.text)
  6. Run it. The reply is a list of content blocks, so the script prints only the text blocks rather than assuming the first block is text. Type: python quickstart.py. prints Claude's reply, for example three subject lines. The wording is different every time.

Commands and code from the official Claude API quickstart and Python SDK docs, checked 18 November 2026. The Windows virtual environment command is from the Python venv docs. The key shown is a placeholder. Lines marked Note describe what happens and are not real output.

Step 1 of 6

$ Read

What you need before you write any code

A Claude Console account. Sign up or sign in at platform.claude.com. This is separate from a claude.ai chat subscription: the API is billed per token from your Console account.

An API key. Create one on the API keys page of the Console (platform.claude.com/settings/keys) and scope it to a workspace when you create it: a key with no workspace needs an extra anthropic-workspace-id header on every request. The Console shows the key only once. Treat it like a password. Never paste it into your code, a chat, a screenshot or a Git repository. The official SDKs read it from an environment variable called ANTHROPIC_API_KEY, so your code never needs to contain it.

Python 3.10 or later for the official anthropic SDK. If you prefer not to handle keys at all on your own machine, the Anthropic CLI can sign you in with ant auth login, and the SDKs then pick up those credentials automatically.

Mac, Linux or WSL
export ANTHROPIC_API_KEY="your-api-key-here"
Windows PowerShell
$env:ANTHROPIC_API_KEY = "your-api-key-here"

Lesson notes: Make your first Claude API call

Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You send a working request to Claude from your own Python code and know what every part of the response means.

  1. Step 1 of 6 · Read

    What you need before you write any code

    A Claude Console account. Sign up or sign in at platform.claude.com. This is separate from a claude.ai chat subscription: the API is billed per token from your Console account.

    An API key. Create one on the API keys page of the Console (platform.claude.com/settings/keys) and scope it to a workspace when you create it: a key with no workspace needs an extra anthropic-workspace-id header on every request. The Console shows the key only once. Treat it like a password. Never paste it into your code, a chat, a screenshot or a Git repository. The official SDKs read it from an environment variable called ANTHROPIC_API_KEY, so your code never needs to contain it.

    Python 3.10 or later for the official anthropic SDK. If you prefer not to handle keys at all on your own machine, the Anthropic CLI can sign you in with ant auth login, and the SDKs then pick up those credentials automatically.

    Mac, Linux or WSL
    export ANTHROPIC_API_KEY="your-api-key-here"
    Windows PowerShell
    $env:ANTHROPIC_API_KEY = "your-api-key-here"
  2. Step 2 of 6 · Try it in the terminal

    Install the SDK

    You are inside a fresh project folder with a virtual environment switched on. Type the command that installs the official Python SDK.

    Hint: Use pip, and the package name is the company name in lower case.

    Show the command
    $ pip install anthropic
    Collecting anthropic
    Installing collected packages: anthropic
    Successfully installed anthropic
  3. Step 3 of 6 · Read

    Your first request

    Save the Python below as quickstart.py. anthropic.Anthropic() creates a client that finds your key in the environment. messages.create sends one request to the Messages API, the single endpoint behind almost everything in this level.

    Three fields are required. model is the exact model id (here claude-opus-5-5, the model the docs suggest starting with). max_tokens is the most the model may write before it is cut off. messages is the conversation so far, which for a first call is one user message.

    The curl version shows what the SDK sends for you: a POST to https://api.anthropic.com/v1/messages with your key in an Authorization: Bearer header and an anthropic-version header. Checked 20 November 2026, the docs use version 2026-10-21 and prefer the Bearer header; the older x-api-key header still works. The Python SDK sets this header itself, so you only see it when you call the API by hand.

    quickstart.py
    import anthropic
    
    client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY
    
    message = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=1000,
        messages=[
            {"role": "user", "content": "Suggest three subject lines for our November newsletter."}
        ],
    )
    
    for block in message.content:
        if block.type == "text":
            print(block.text)
    The same call with curl
    curl https://api.anthropic.com/v1/messages \
      -H "content-type: application/json" \
      -H "Authorization: Bearer $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2026-10-21" \
      -d '{
        "model": "claude-opus-5-5",
        "max_tokens": 1000,
        "messages": [{"role": "user", "content": "Hello, Claude"}]
      }'
  4. Step 4 of 6 · Try it in the terminal

    Run it

    Your key is set and quickstart.py is saved in the current folder. Type the command that runs the script.

    Hint: Run the file with Python.

    Show the command
    $ python quickstart.py
    1. Your November round-up: what changed this month
    2. Five things worth knowing before December
    3. Quick wins for a busy month
  5. Step 5 of 6 · Read

    Reading the response

    The API returns a message object. content is a list of blocks, not a single string. A plain answer has one text block, but the same list can also hold thinking blocks and, later in this level, tool_use blocks. That is why the example loops and checks block.type instead of grabbing content[0].text.

    stop_reason tells you why Claude stopped. end_turn means it finished naturally. max_tokens means it hit your limit and the answer is cut short, so raise max_tokens and try again. Other values you will meet are tool_use, pause_turn and refusal.

    usage reports input_tokens and output_tokens, which is what you are billed for. Every response also carries a request id (message._request_id in Python). Log it: support asks for it when something goes wrong.

    Response (shortened)
    {
      "id": "msg_013mHbppMPd2PrVJzGMZPt2D",
      "type": "message",
      "role": "assistant",
      "model": "claude-opus-5-5",
      "content": [{"type": "text", "text": "1. Your November round-up..."}],
      "stop_reason": "end_turn",
      "stop_sequence": null,
      "usage": {"input_tokens": 21, "output_tokens": 305}
    }
  6. Step 6 of 6 · Quiz

    Check your understanding

    A colleague wants to share a working script with the team. Where should the API key live?

    • At the top of the script, so it works for everyone straight away.
    • In a comment, so it is easy to swap out.
    • In each person's `ANTHROPIC_API_KEY` environment variable, or a secrets manager on the server.
    Show the answer

    In each person's `ANTHROPIC_API_KEY` environment variable, or a secrets manager on the server. The SDK reads the variable automatically, so the script works without ever containing a key. Each person can use their own key.