Getting Started with Claude API: Your First Call and Beyond
Learn how to set up your Anthropic Console account, generate an API key, and make your first API call to Claude using cURL, Python, or TypeScript. Includes code examples and next steps.
This guide walks you through creating an Anthropic Console account, obtaining an API key, and making your first API call to Claude using cURL, Python, or TypeScript. You'll also learn the next steps for building with the Messages API.
Introduction
Welcome to the Claude API! Whether you're building a chatbot, an AI-powered assistant, or integrating Claude into your existing workflow, this guide will get you up and running in minutes. By the end, you'll have made your first API call and understand the core patterns you'll use in every Claude integration.
Prerequisites
Before you start, you'll need two things:
- An Anthropic Console account – Sign up at console.anthropic.com. It's free and gives you access to the API dashboard, usage stats, and billing.
- An API key – Once logged in, navigate to the API Keys section and create a new key. Copy it immediately; you won't be able to see it again.
Security tip: Never hardcode your API key in client-side code or public repositories. Use environment variables or a secrets manager.
Making Your First API Call
Claude's API uses the Messages API, a simple and flexible interface for sending prompts and receiving responses. Let's make your first call using three popular methods: cURL, Python, and TypeScript.
Using cURL
Open your terminal and run:
curl https://api.anthropic.com/v1/messages \
--header "x-api-key: YOUR_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data '{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
}'
Replace YOUR_API_KEY with your actual key. You'll receive a JSON response with Claude's reply.
Using Python
First, install the Anthropic SDK:
pip install anthropic
Then create a file hello_claude.py:
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY"
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
print(message.content[0].text)
Run it:
python hello_claude.py
Using TypeScript
Install the SDK:
npm install @anthropic-ai/sdk
Create hello_claude.ts:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: 'YOUR_API_KEY',
});
async function main() {
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude!' }],
});
console.log(message.content[0].text);
}
main();
Run with:
npx ts-node hello_claude.ts
Understanding the Response
The API returns a structured JSON object. Here's what you'll see:
{
"id": "msg_01ABC123...",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"model": "claude-sonnet-4-20250514",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 10,
"output_tokens": 10
}
}
Key fields:
- content: An array of content blocks (usually text).
- stop_reason: Why the response ended (
"end_turn","max_tokens","stop_sequence", or"tool_use"). - usage: Token counts for billing and monitoring.
Next Steps: Mastering the Messages API
You've made your first call! Now it's time to explore the core patterns you'll use in every integration.
Multi-turn Conversations
To maintain context across multiple exchanges, include the entire conversation history in the messages array:
messages = [
{"role": "user", "content": "What's the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What's its population?"}
]
System Prompts
Set the behavior and persona of Claude using the system parameter:
message = 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": "Tell me about the weather."}
]
)
Handling Stop Reasons
Always check stop_reason to determine why Claude stopped. Common scenarios:
- end_turn: Claude finished naturally.
- max_tokens: The response was cut off. Increase
max_tokensor continue the conversation. - tool_use: Claude wants to call a tool (see Tool Use guide).
Exploring Claude's Capabilities
Once you're comfortable with the basics, dive into these powerful features:
| Feature | Description |
|---|---|
| Extended Thinking | Enable step-by-step reasoning for complex tasks. |
| Structured Outputs | Get JSON, XML, or other structured responses. |
| Tool Use | Let Claude call external APIs, databases, or functions. |
| Prompt Caching | Reduce latency and cost for repeated system prompts. |
| Streaming | Receive responses token-by-token for real-time UX. |
| Vision | Analyze images alongside text prompts. |
Choosing the Right Model
Claude offers several models optimized for different use cases:
- Claude Sonnet 4 (recommended): Best balance of speed, cost, and intelligence for most applications.
- Claude Opus 4: Highest capability for complex reasoning and creative tasks.
- Claude Haiku 3.5: Fastest and most affordable for simple, high-volume tasks.
Troubleshooting Common Issues
Authentication Errors
- Ensure your API key is correct and active.
- Check that you're using the correct header (
x-api-key).
Rate Limits
- The API has rate limits based on your plan. Use exponential backoff in your code.
- Consider prompt caching to reduce token usage.
Token Limits
- If you hit
max_tokens, increase the value or split your request into multiple turns. - Use
token countingto estimate costs before sending.
Key Takeaways
- Get started in minutes: Sign up for an Anthropic Console account, generate an API key, and make your first call with cURL, Python, or TypeScript.
- Master the Messages API: Use multi-turn conversations, system prompts, and handle stop reasons to build robust applications.
- Explore advanced features: Leverage tool use, streaming, structured outputs, and vision to unlock Claude's full potential.
- Choose the right model: Select Claude Sonnet 4 for general use, Opus 4 for complex tasks, or Haiku 3.5 for speed and cost efficiency.
- Monitor and optimize: Track token usage, implement prompt caching, and handle rate limits for production-ready applications.