Getting Started with the Claude API: A Beginner’s Guide to Building with Anthropic’s Models
Learn how to set up the Claude API, make your first call, understand the Messages API, and explore key features like tools, streaming, and structured outputs.
This guide walks you through setting up the Claude API, making your first request with Python, understanding the Messages API structure, and exploring key capabilities like streaming, tools, and structured outputs.
Introduction
Anthropic’s Claude API gives developers direct access to the latest generation of Claude models—Claude Opus 4.7, Claude Sonnet 4.6, and Claude Haiku 4.5—for building custom AI-powered applications. Whether you’re creating a chatbot, an agentic coding assistant, or an enterprise workflow, the API provides the flexibility and control you need.
This guide is designed for beginners who want to go from zero to a working Claude integration. You’ll learn how to set up your environment, make your first API call, understand the core request/response structure, and explore key features that make Claude powerful.
By the end of this article, you’ll be ready to build your own applications with Claude.
Prerequisites
Before you start, make sure you have:
- An Anthropic account and an API key (get one from the Anthropic Console)
- Python 3.8+ installed on your machine
- Basic familiarity with Python and REST APIs
Step 1: Set Up Your Environment
Install the official Anthropic Python SDK:
pip install anthropic
Set your API key as an environment variable (recommended for security):
export ANTHROPIC_API_KEY="your-api-key-here"
Alternatively, you can pass the key directly in your code, but avoid hardcoding it in production.
Step 2: Make Your First API Call
Create a file named first_call.py and add the following code:
import anthropic
client = anthropic.Anthropic()
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)
Run the script:
python first_call.py
You should see Claude’s response printed in your terminal. Congratulations—you’ve made your first API call!
Understanding the Response
The response object contains:
id: Unique message IDmodel: The model usedrole: Always"assistant"content: A list of content blocks (usually text)stop_reason: Why the model stopped (e.g.,"end_turn","max_tokens","stop_sequence")usage: Token usage information
Step 3: Understand the Messages API
The Messages API is the core interface for communicating with Claude. It supports:
- Multi-turn conversations by passing the full message history
- System prompts to set behavior and context
- Stop reasons to control when the model stops generating
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="You are a helpful assistant that speaks like a pirate.",
messages=[
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "Arrr, the capital o' France be Paris!"},
{"role": "user", "content": "Tell me more about it."}
]
)
print(response.content[0].text)
Key Parameters
| Parameter | Description |
|---|---|
model | The model ID (e.g., claude-sonnet-4-20250514) |
max_tokens | Maximum tokens in the response |
system | System prompt for setting behavior |
messages | Array of message objects with role and content |
temperature | Controls randomness (0.0 to 1.0) |
stop_sequences | Array of strings that stop generation |
Step 4: Choose the Right Model
Anthropic offers three model families, each optimized for different use cases:
| Model | Best For | Speed | Cost |
|---|---|---|---|
| Claude Opus 4.7 | Complex reasoning, agentic coding, research | Slower | Highest |
| Claude Sonnet 4.6 | General coding, agents, enterprise workflows | Fast | Moderate |
| Claude Haiku 4.5 | Simple tasks, classification, real-time apps | Fastest | Lowest |
Step 5: Explore Key Features
Streaming Responses
For real-time applications, use streaming to receive tokens as they’re generated:
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short poem about AI."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Using Tools (Function Calling)
Claude can use external tools to perform actions like web searches, file operations, or API calls:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
],
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
tool_call = response.content[1]
print(f"Tool requested: {tool_call.name}")
print(f"Arguments: {tool_call.input}")
Structured Outputs
You can ask Claude to return structured JSON data by specifying the format in the system prompt:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="Always respond in JSON format with keys: 'name', 'age', 'occupation'.",
messages=[{"role": "user", "content": "Tell me about Marie Curie."}]
)
print(response.content[0].text)
File and Image Processing
Claude can process PDFs and images. Here’s an example with an image:
import anthropic
import base64
client = anthropic.Anthropic()
with open("photo.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_data
}
}
]
}
]
)
print(response.content[0].text)
Next Steps
Once you’re comfortable with the basics, explore these advanced topics:
- Extended Thinking: Enable Claude to reason step-by-step before responding
- Batch Processing: Send multiple requests asynchronously for efficiency
- Prompt Caching: Reduce latency and cost for repeated prompts
- Managed Agents: Use Anthropic’s pre-built agent harness for long-running tasks
Key Takeaways
- The Messages API is the core interface for communicating with Claude, supporting multi-turn conversations, system prompts, and stop reasons.
- Choose the right model for your use case: Opus for complex reasoning, Sonnet for balanced performance, Haiku for speed and cost efficiency.
- Key features like streaming, tool use, structured outputs, and file processing unlock powerful applications.
- Always set your API key as an environment variable for security, and start with the Python SDK for the smoothest onboarding.
- Explore the Anthropic Console and Cookbook for hands-on learning and advanced patterns.