~/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.
- Make a folder for the project and move into it. You need Python 3.10 or later. Type: mkdir claude-quickstart && cd claude-quickstart.
- 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.
- Install the official Python SDK. Type: pip install anthropic. pip downloads and installs the SDK and the packages it needs.
- 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".
- 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)
- 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.
$ 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.
export ANTHROPIC_API_KEY="your-api-key-here"
$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.
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-idheader 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 calledANTHROPIC_API_KEY, so your code never needs to contain it.Python 3.10 or later for the official
anthropicSDK. If you prefer not to handle keys at all on your own machine, the Anthropic CLI can sign you in withant 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"
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
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.createsends one request to the Messages API, the single endpoint behind almost everything in this level.Three fields are required.
modelis the exact model id (hereclaude-opus-5-5, the model the docs suggest starting with).max_tokensis the most the model may write before it is cut off.messagesis the conversation so far, which for a first call is oneusermessage.The curl version shows what the SDK sends for you: a POST to
https://api.anthropic.com/v1/messageswith your key in anAuthorization: Bearerheader and ananthropic-versionheader. Checked 20 November 2026, the docs use version2026-10-21and prefer the Bearer header; the olderx-api-keyheader 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"}] }'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
Step 5 of 6 · Read
Reading the response
The API returns a message object.
contentis a list of blocks, not a single string. A plain answer has onetextblock, but the same list can also holdthinkingblocks and, later in this level,tool_useblocks. That is why the example loops and checksblock.typeinstead of grabbingcontent[0].text.stop_reasontells you why Claude stopped.end_turnmeans it finished naturally.max_tokensmeans it hit your limit and the answer is cut short, so raisemax_tokensand try again. Other values you will meet aretool_use,pause_turnandrefusal.usagereportsinput_tokensandoutput_tokens, which is what you are billed for. Every response also carries a request id (message._request_idin 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} }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.