Getting Your API Key
To use Claude programmatically, you need an API key from Anthropic's console.
- Go to console.anthropic.com
- Create an account (or sign in)
- Navigate to API Keys
- Click "Create Key" and name it
- Copy and store it securely - you won't see it again
Security rule
NEVER put your API key in code that gets committed to Git. Use environment variables: export ANTHROPIC_API_KEY=sk-ant-...
Your First API Call
# Install the SDK
pip install anthropic
# Your first call
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude! What can you do?"}
]
)
print(message.content[0].text)What just happened
You sent a single message to Claude Sonnet and got a response. The messages array is the conversation - each entry has a role (user or assistant) and content. Claude's response comes back in message.content[0].text.
Understanding the Response
# The response object contains useful metadata
print(message.model) # "claude-sonnet-4-20250514"
print(message.usage.input_tokens) # tokens you sent
print(message.usage.output_tokens) # tokens Claude generated
print(message.stop_reason) # "end_turn" = natural stop
# Calculate cost
input_cost = message.usage.input_tokens * 3.00 / 1_000_000
output_cost = message.usage.output_tokens * 15.00 / 1_000_000
print(f"This call cost: ${input_cost + output_cost:.4f}")Key Takeaways
- This lesson covered getting your api key and making your first request
- Apply these concepts in your own projects before moving on
- Refer back to this lesson when you encounter related challenges