pip install anthropic: Install the SDK and Send Your First Claude Request
Where to get an API key in the Claude Console, how to install the anthropic SDK, store the key securely with dotenv, and send your first request to Claude — from installation to printing the response.
20 min read3 questions in quizReady prompt included
How do I install the Anthropic Python SDK and send a first request?
Run pip install anthropic python-dotenv in a virtual environment (the SDK needs Python 3.10+), put ANTHROPIC_API_KEY in a .env file, then call client.messages.create with model, max_tokens and messages. The lesson uses claude-haiku-5-5; claude-3-haiku from Anthropic's original notebook was retired on April 20, 2026. Read the content block whose type is text.
6 short Python lessons built on Anthropic's open API fundamentals notebooks: setup, the Messages format, choosing a model, parameters, streaming and vision. Each lesson has a task checked by AI and a quiz.
Keys are created in the Claude Console at platform.claude.com (the old console.anthropic.com address redirects there). Create a key there and store it somewhere safe. The Console also has a playground (formerly Workbench) where you can try a request without code.
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.
Read the text blocksblock.type == "text" → block.text
→
Anthropic() reads ANTHROPIC_API_KEY from the environment by itself; call load_dotenv() first if the key lives in .env.
Notes for running the lesson today
Checked on 9 Nov 2026 (Claude API docs and PyPI).
Every Claude 3 model in Anthropic's original notebooks is retired: claude-3-haiku-20240307 on April 20, 2026, claude-3-5-sonnet on October 28, 2025, claude-3-opus on January 5, 2026. Anthropic suggests starting with claude-opus-5-5 ($4 / $20 per million input / output tokens); claude-haiku-5-5 is the cheapest current model, from $0.10 per million input tokens.
A fresh pip install anthropic gives SDK 2.x, which sends API version 2026-10-21. That version removes temperature, top_p and top_k, and any value returns a 400 error. Lesson 4 explains how to steer the output without them.
To read the answer on current models: "".join(b.text for b in message.content if b.type == "text"). A thinking block may or may not come before the text, and its text is omitted by default.
“ANTHROPIC_API_KEY not set” with an existing .env usually means load_dotenv() ran after os.getenv() or the .env file sits in a different folder from where you start Python.
You create the key in the Claude Console at platform.claude.com, and API requests are billed separately from a Claude.ai subscription.
Sources we checked
Claude API docs: Get started
PyPI: anthropic
Claude API docs: Model deprecations
Claude API docs: Models overview
Claude API docs: Migrating to API version 2026-10-21
anthropics/courses: Anthropic API fundamentals (archived)
FAQ
How do I install the anthropic package for Python?+
Run pip install anthropic, ideally inside a virtual environment (python3 -m venv .venv, then activate it). The current version needs Python 3.10 or newer. In Jupyter use %pip install anthropic so the package lands in the kernel's environment. Add python-dotenv if you keep the key in a .env file.
Why does my script say ANTHROPIC_API_KEY is not set?+
Call load_dotenv() before os.getenv("ANTHROPIC_API_KEY"), and check that .env sits in the folder you run Python from. If you export ANTHROPIC_API_KEY in your shell, Anthropic() picks it up without dotenv. Keep the key out of the code: once committed to git, it leaks.
Which model name should I use in my first request?+
claude-haiku-5-5 is the cheapest current model and fine for practice; Anthropic recommends claude-opus-5-5 as the starting point for real work. The lesson uses claude-haiku-5-5 and claude-sonnet-5-5 in the ready prompt. The original notebook's claude-3-haiku-20240307 and claude-3-5-sonnet-20241022 are retired, and requests to them fail.
Why does message.content[0].text fail?+
Current models use adaptive thinking, so the first content block can be a thinking block with no text field. Loop over message.content and take the blocks whose type is "text", as the quickstart in Anthropic's docs does.
Which parameters does client.messages.create need?+
Three: model (the model id), max_tokens (the upper limit on response length; the model can stop earlier) and messages (the conversation as a list of role/content pairs). Lesson 2 covers the format of messages and the response object.
Is this lesson free?+
Yes. It is lesson 1 of 6 in “Anthropic API Fundamentals: Official Course”, and a free account keeps the first two lessons of every Academl course open for good. A free account opens all lessons for 7 days with no card; after that Pro costs $9 a month or $79 a year.
The next lessons cover the Messages format and a simple chatbot, choosing a model by speed and cost, system prompts, streaming and vision. A free account opens all 6 lessons for 7 days.