← Course | Module 02 - The API Lesson 7
MODULE 02

Getting Your API Key and Making Your First Request

🕑 10 min read 🎯 Beginner

Getting Your API Key

To use Claude programmatically, you need an API key from Anthropic's console.

  1. Go to console.anthropic.com
  2. Create an account (or sign in)
  3. Navigate to API Keys
  4. Click "Create Key" and name it
  5. 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

🖨 Download PDF 🐦 Share on X
← Previous