Getting Started with the Claude API: Your First Integration in Minutes
Learn how to make your first API call to Claude, understand the Messages API, and explore core patterns for building AI-powered applications with Anthropic's platform.
This guide walks you through creating an Anthropic Console account, obtaining an API key, and making your first API call using cURL, Python, or TypeScript. You'll also learn the foundational patterns of the Messages API for multi-turn conversations, system prompts, and stop reasons.
Introduction
Claude is a powerful, safe, and versatile AI assistant built by Anthropic. Whether you're building a chatbot, a content generator, a code assistant, or an agentic workflow, the Claude API gives you programmatic access to Claude's capabilities. This guide will take you from zero to your first working API call in minutes.
By the end of this article, you will:
- Set up an Anthropic Console account and obtain an API key
- Make your first API call using cURL, Python, or TypeScript
- Understand the core structure of the Messages API
- Know where to go next to build more complex integrations
Prerequisites
Before you begin, you'll need:
- A web browser and internet connection
- Basic familiarity with command-line tools or a programming language (Python or TypeScript)
- (Optional) A code editor like VS Code
Step 1: Create an Anthropic Console Account
- Go to console.anthropic.com
- Click Sign Up and create your account using email or a Google/GitHub login
- Verify your email address
- Once logged in, navigate to the API Keys section in the left sidebar
- Click Create Key and give it a name (e.g., "My First Claude App")
- Copy the generated key and store it securely. You will not be able to see it again.
Security Tip: Never hardcode your API key in client-side code or commit it to version control. Use environment variables or a secrets manager.
Step 2: Make Your First API Call
You can call the Claude API using any HTTP client. Below are examples for cURL, Python, and TypeScript.
Using cURL
Open your terminal and run:
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!"}
]
}'
Replace $ANTHROPIC_API_KEY with your actual key or set it as an environment variable.
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_HERE"
)
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
You should see Claude's friendly greeting.
Using TypeScript
First, install the Anthropic SDK:
npm install @anthropic-ai/sdk
Then create a file hello_claude.ts:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: 'YOUR_API_KEY_HERE',
});
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 it with:
npx ts-node hello_claude.ts
Understanding the Messages API
The Messages API is the primary way to interact with Claude. Every request follows this structure:
Request Body Fields
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | The Claude model identifier (e.g., claude-sonnet-4-20250514) |
max_tokens | integer | Yes | Maximum number of tokens in the response |
messages | array | Yes | Array of message objects representing the conversation |
system | string | No | System prompt to set Claude's behavior |
temperature | number | No | Sampling temperature (0.0 to 1.0, default 1.0) |
stop_sequences | array | No | Custom strings that stop Claude from generating further |
Message Objects
Each message in the messages array has:
role: Either"user"or"assistant"content: The text content of the message
{
"messages": [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"}
]
}
System Prompts
System prompts allow you to set the tone, style, and constraints for Claude. For example:
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."}
]
)
Stop Reasons
When Claude finishes generating, the response includes a stop_reason field. Common values:
"end_turn": Claude naturally finished its response"max_tokens": The response reached themax_tokenslimit"stop_sequence": A custom stop sequence was encountered"tool_use": Claude wants to call a tool (advanced)
stop_reason to decide next steps in your application logic.
Next Steps
Congratulations! You've made your first API call. Here's what to explore next:
1. Working with the Messages API
Learn multi-turn conversations, system prompts, stop reasons, and other core patterns in the Messages API documentation.2. Explore Claude Models
Compare Claude models by capability and cost in the Models overview.3. Browse All Capabilities
Discover tools, context management, structured outputs, streaming, and more in the Features overview.4. Client SDKs
Reference the official SDKs for Python, TypeScript, Java, and other languages at the Client SDKs page.5. Build an Agent
Once you're comfortable with the basics, learn how to build a tool-using agent with the Tool Use guide.Troubleshooting Tips
- 401 Unauthorized: Check that your API key is correct and properly set in the request header.
- 400 Bad Request: Verify that your JSON payload is valid and includes all required fields (
model,max_tokens,messages). - Rate Limiting: If you hit rate limits, implement exponential backoff in your retry logic.
- Context Length: If your messages are too long, consider truncating or summarizing older messages.
Key Takeaways
- Start with the Messages API: It's the foundational endpoint for all Claude interactions, supporting single and multi-turn conversations.
- Use environment variables for API keys: Never hardcode secrets; use
$ANTHROPIC_API_KEYor a.envfile. - System prompts are powerful: They let you control Claude's behavior without modifying your application logic.
- Check stop reasons: Use
stop_reasonto handle edge cases like token limits or tool calls gracefully. - Explore the feature ecosystem: From streaming to tool use, Claude's API offers a rich set of capabilities for building production-ready applications.