Getting Started with Claude API: Your First Call and Beyond
Learn how to set up your Anthropic account, obtain an API key, and make your first call to Claude using cURL, Python, or TypeScript. Includes code examples and next steps.
This guide walks you through creating an Anthropic Console account, generating an API key, and making your first API call to Claude using cURL, Python, or TypeScript. You'll also learn where to go next with the Messages API.
Introduction
Claude is a powerful AI assistant built by Anthropic, designed to handle a wide range of tasks from simple Q&A to complex reasoning, code generation, and tool use. Whether you're building a chatbot, automating workflows, or integrating AI into your application, the Claude API gives you programmatic access to Claude's capabilities.
This guide will take you from zero to your first successful API call. By the end, you'll have a working API key, know how to authenticate requests, and understand the basic structure of a Claude API interaction.
Prerequisites
Before you start, you'll need two things:
- An Anthropic Console account – Sign up at console.anthropic.com.
- An API key – Generate one from the Console's API Keys section.
Security note: Treat your API key like a password. Never hardcode it in client-side code or commit it to version control. Use environment variables or a secrets manager.
Step 1: Get Your API Key
- Log in to the Anthropic Console.
- Navigate to API Keys in the left sidebar.
- Click Create Key and give it a descriptive name (e.g., "My Dev Key").
- Copy the key immediately – you won't be able to see it again.
export ANTHROPIC_API_KEY="sk-ant-..."
Step 2: Make Your First API Call
Claude's API uses the Messages API – a simple, flexible interface for sending prompts and receiving responses. Below are examples in three common formats.
Using cURL
curl https://api.anthropic.com/v1/messages \
--header "x-api-key: $ANTHROPIC_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!"}
]
}'
What's happening?
x-api-key: Your authentication token.anthropic-version: The API version you're targeting.model: Which Claude model to use (e.g.,claude-sonnet-4-20250514).max_tokens: Maximum length of the response.messages: An array of conversation turns. Each turn has arole(userorassistant) andcontent.
Using Python
Install the official SDK:
pip install anthropic
Then make a call:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
print(message.content[0].text)
The SDK automatically reads your ANTHROPIC_API_KEY environment variable. You can also pass it explicitly: Anthropic(api_key="...").
Using TypeScript / JavaScript
Install the SDK:
npm install @anthropic-ai/sdk
Then use it:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
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();
Understanding the Response
A successful response from the Messages API looks like this:
{
"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. For simple text responses, there's one block withtype: "text".stop_reason: Why the model stopped generating. Common values:
"end_turn": The model finished naturally.
- "max_tokens": The response hit the token limit.
- "stop_sequence": A custom stop sequence was encountered.
usage: Token counts for billing and monitoring.
Step 3: Explore Next Steps
You've made your first API call – congratulations! Now it's time to build on that foundation.
Learn the Messages API Patterns
The Messages API supports:
- Multi-turn conversations – Send multiple messages to maintain context.
- System prompts – Set behavior and constraints for Claude.
- Stop reasons – Handle different completion states programmatically.
Compare Claude Models
Different Claude models balance capability, speed, and cost:
| Model | Best For |
|---|---|
| Claude Opus | Complex reasoning, analysis, coding |
| Claude Sonnet | Balanced performance and speed |
| Claude Haiku | Fast, lightweight tasks |
Explore Advanced Features
Claude's API offers many advanced capabilities:
- Tools (function calling) – Let Claude use external tools and APIs.
- Structured outputs – Get JSON responses for reliable parsing.
- Streaming – Receive responses token-by-token for real-time UX.
- Vision – Analyze images alongside text.
- Prompt caching – Reduce latency and cost for repeated prompts.
Use Client SDKs
Official SDKs are available for:
- Python
- TypeScript / JavaScript
- Java
Troubleshooting Common Issues
| Problem | Likely Cause | Solution |
|---|---|---|
401 Unauthorized | Invalid or missing API key | Check your key and environment variable |
400 Bad Request | Malformed request body | Validate JSON and required fields |
429 Too Many Requests | Rate limit exceeded | Implement exponential backoff |
529 Overloaded | Server temporarily busy | Retry after a short delay |
Key Takeaways
- Start with an API key – Create it in the Anthropic Console and store it securely as an environment variable.
- Use the Messages API – It's the core interface for all Claude interactions, supporting single and multi-turn conversations.
- Pick the right model – Choose Claude Opus for complex tasks, Sonnet for balanced performance, or Haiku for speed.
- Explore advanced features – Tools, streaming, vision, and structured outputs unlock powerful applications.
- Leverage official SDKs – Python, TypeScript, and Java SDKs simplify integration and reduce boilerplate.