The Anthropic Developer Platform – From First Call to Production

Lesson 2: Authentication and Your First API Call

Lesson Objectives

By the end of this lesson, students should be able to:

  • Set up authentication using environment variables
  • Install and configure the Anthropic SDK for Python or TypeScript
  • Make a first successful Messages API call
  • Read and interpret the API response structure

Lesson Content

Setting up your API key.

  1. Create an Anthropic account at console.anthropic.com
  2. Navigate to API Keys and generate a new key
  3. Copy the key immediately – it is shown only once
  4. Store it in an environment variable: ANTHROPIC_API_KEY=sk-ant-...

Never hardcode the key in source files. Use environment variables in development and a secrets manager (AWS Secrets Manager, Google Secret Manager, etc.) in production.

Installing the SDK.

Python: pip install anthropic TypeScript/Node: npm install @anthropic-ai/sdk

Always verify current SDK versions and installation instructions at docs.anthropic.com/en/docs/quickstart.

Your first API call (Python example pattern).

import anthropic

client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from environment

message = client.messages.create(
model="claude-sonnet-4-6", # replace with current model ID from docs
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude."}
]
)

print(message.content[0].text)

This is a minimal working call. The model parameter should use a current model ID from docs.anthropic.com – not hardcoded values from this course.

The response structure.

The Messages API response contains:

  • id: unique message identifier
  • type: "message"
  • role: "assistant"
  • content: array of content blocks (typically a text block with .text property)
  • model: the model that generated the response
  • stop_reason: why generation stopped (end_turn, max_tokens, tool_use)
  • usage: input and output token counts for this request

The usage field is important: log token counts in production to track costs and debug unexpectedly large requests.

Adding a system prompt.

message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a helpful assistant for a software development team.",
messages=[
{"role": "user", "content": "What are the main risk factors in this PR?"}
]
)

The system parameter sets the persistent instruction context for the model – equivalent to Custom Instructions in the chat interface.

Enroll to continue this lesson.

The preview above shows the lesson objectives and opening lesson content. Enroll to view the full lesson, complete the practice work, and take the lesson quiz.

Log In / Create Account
Back to Course