Getting Started with the Claude API: From First Call to Production
Learn how to integrate Claude into your applications using the Messages API, Managed Agents, and client SDKs. Includes Python and TypeScript code examples.
This guide walks you through setting up the Claude API, making your first call with Python or TypeScript, and choosing between the Messages API and Managed Agents for production use.
Getting Started with the Claude API: From First Call to Production
Claude is more than a chatbot—it's a platform. Whether you're building a custom assistant, automating workflows, or embedding reasoning into your application, the Claude API gives you direct access to the most capable models in the Claude family: Opus 4.7, Sonnet 4.6, and Haiku 4.5.
This guide covers everything you need to go from your first API call to a production-ready integration. You'll learn how to choose the right developer surface, install SDKs, write your first message, and decide between the Messages API and Managed Agents.
Choosing Your Developer Surface
Before writing any code, you need to decide how you want to interact with Claude. The platform offers two primary surfaces:
Messages API (Direct Model Access)
With the Messages API, you have full control. You construct every turn of the conversation, manage conversation state yourself, and write your own tool loop. This is ideal for:
- Custom chat interfaces
- Applications requiring fine-grained control over context
- Workflows where you need to inject system prompts or tools
Managed Agents (Fully Managed Infrastructure)
Managed Agents handle the heavy lifting. You define your agent's instructions and tools, and Claude manages stateful sessions with persistent event history. This is best for:
- Autonomous agents that run over long periods
- Applications where you don't want to manage conversation state
- Quick prototyping and deployment
Tip: Start with the Messages API if you want maximum control. Switch to Managed Agents when you need persistence and autonomy without boilerplate.
Prerequisites
To follow along, you'll need:
- A Claude API key
- Python 3.8+ or Node.js 18+
- Basic familiarity with REST APIs
Step 1: Install an SDK
Claude provides official SDKs for Python, TypeScript, Go, Java, Ruby, PHP, C#, and cURL. Here's how to install the two most common ones:
Python
pip install anthropic
TypeScript
npm install @anthropic-ai/sdk
Step 2: Make Your First API Call
Let's send a simple message to Claude. You'll need your API key. Set it as an environment variable for security:
export ANTHROPIC_API_KEY="sk-ant-..."
Python Example
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude"}
]
)
print(message.content[0].text)
TypeScript Example
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
async function main() {
const message = await client.messages.create({
model: 'claude-opus-4-7',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude' }
]
});
console.log(message.content[0].text);
}
main();
Run the script, and you'll see Claude's response printed to the console. That's your first integration!
Step 3: Understand the Messages API Structure
The Messages API is built around a simple request/response pattern. Here's what each field means:
- model: The Claude model you want to use (e.g.,
claude-opus-4-7,claude-sonnet-4-6,claude-haiku-4-5) - max_tokens: The maximum number of tokens in the response
- messages: An array of message objects, each with a
role(user or assistant) andcontent
- system: A system prompt to set Claude's behavior
- tools: Define functions Claude can call
- stream: Enable streaming for real-time responses
Step 4: Choose the Right Model
Claude offers three models optimized for different use cases:
| Model | ID | Best For |
|---|---|---|
| Opus 4.7 | claude-opus-4-7 | Complex analysis, deep reasoning, creative tasks |
| Sonnet 4.6 | claude-sonnet-4-6 | Production workloads needing speed + intelligence |
| Haiku 4.5 | claude-haiku-4-5 | High-volume, latency-sensitive apps |
Step 5: Going Beyond Basic Messages
Once you've made your first call, explore these advanced features:
Extended Thinking
Enable Claude to reason step-by-step before responding. Useful for math, logic, and complex coding tasks.
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=2048,
thinking={"type": "enabled", "budget_tokens": 1024},
messages=[{"role": "user", "content": "Solve this equation step by step: 3x + 7 = 22"}]
)
Tool Use
Define tools that Claude can call to interact with external systems (databases, APIs, file systems).
Structured Outputs
Request JSON or other structured formats directly from Claude for easier parsing.
Streaming
Get responses token-by-token for a real-time user experience.
stream = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.type == "content_block_delta":
print(chunk.delta.text, end="")
Step 6: Evaluate and Ship
Before going to production, follow these best practices:
- Run evals: Test your prompts and model responses against a benchmark dataset.
- Batch testing: Use the batch API to test multiple scenarios at once.
- Safety & guardrails: Implement content filtering and rate limiting.
- Cost optimization: Choose Haiku for simple tasks, Sonnet for standard tasks, and Opus only when needed.
- Monitor usage: Use the Workspaces & Admin dashboard to track API key usage.
Managed Agents: A Quick Look
If you want to skip state management, try Managed Agents. Here's a minimal example:
# Define your agent (conceptual)
agent = client.agents.create(
name="CustomerSupportBot",
instructions="You are a helpful support agent for Acme Corp.",
tools=[
{"type": "function", "function": {"name": "get_order_status", "parameters": {...}}}
]
)
Start a session
session = agent.sessions.create()
response = session.send_message("What's the status of order 12345?")
print(response.text)
Managed Agents automatically persist conversation history and handle tool execution loops.
Next Steps
Now that you've made your first API call and understand the landscape, here's where to go next:
- Explore the API Reference for full parameter details
- Check the Cookbook for code samples and patterns
- Try Claude Code for an agentic coding assistant in your terminal
- Take an interactive Course to master Claude
Key Takeaways
- Choose the right surface: Use the Messages API for full control, Managed Agents for autonomous, stateful sessions.
- Install an SDK: Python and TypeScript SDKs are the quickest path to your first API call.
- Match the model to the task: Opus for deep reasoning, Sonnet for balance, Haiku for speed.
- Leverage advanced features: Extended thinking, tool use, streaming, and structured outputs unlock Claude's full potential.
- Evaluate before shipping: Run evals, batch tests, and monitor usage to ensure quality and cost efficiency in production.