First Steps: Installation and Your First Request
How to install the anthropic SDK, securely store your API key with dotenv, and send your first request to Claude — from installation to printing the response.
Install anthropic and python-dotenv, create a .env file with your key, write a script that asks Claude to write a haiku about your city and prints the response text.
Copy and adapt to your context. Text in angle brackets should be replaced.
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Write a haiku about Python programming"}
]
)
print(message.content[0].text)- Hardcoding the API key directly in code — risk of leaking it via git commit.
- Forgetting to call load_dotenv() before os.getenv() — the variable won't be loaded.
- Not checking the Python version — the SDK requires 3.7.1+.