How to Use the Anthropic API: Getting Started Guide
The Anthropic API gives you direct access to Claude. This guide covers setup, authentication, your first request, and the core concepts you need to build production apps.
The Anthropic API gives developers direct access to Claude. The setup is straightforward. This guide covers the essentials to get from zero to a working request in under 30 minutes.
Step 1: Get an API Key
Go to console.anthropic.com and create an account. Once verified, navigate to API Keys and generate a key. Store it securely -- treat it like a password.
Set it as an environment variable:
export ANTHROPIC_API_KEY=your_key_hereNever hardcode keys into source code or commit them to version control.
Step 2: Install the SDK
Anthropic provides official SDKs for Python and TypeScript.
Python:
pip install anthropicNode.js:
npm install @anthropic-ai/sdkStep 3: Your First Request
Python:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain API rate limiting in plain English."}
]
)
print(message.content[0].text)Node.js:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
const message = await client.messages.create({
model: 'claude-opus-4-5',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Explain API rate limiting in plain English.' }
],
});
console.log(message.content[0].text);Key Concepts
Model selection: Start with claude-haiku-3-5 for fast, cheap prototyping. Move to claude-sonnet-4-5 for balanced production work. Use claude-opus-4-5 for complex tasks where quality is critical.
System prompts: Add a system field to the request to set the model's behavior. System prompts are separate from the messages array.
Max tokens: Always set max_tokens. It controls the maximum length of the response and prevents runaway costs.
Token usage: Check message.usage in the response to see how many tokens were consumed. This helps you manage costs.
Streaming Responses
For real-time UX, use streaming:
with client.messages.stream(
model="claude-opus-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short story."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Streaming reduces perceived latency significantly for longer responses.
Rate Limits and Errors
Anthropic enforces rate limits on requests per minute and tokens per minute. Handle RateLimitError with exponential backoff. Handle APIStatusError for server-side failures.
The SDK includes built-in retry logic for transient errors. Check the docs for configuration options.
Next Steps
Once you have a working request, explore:
- Multi-turn conversations (pass message history in the `messages` array)
- Function calling / tool use (structured outputs, external integrations)
- Vision (pass image data alongside text)
The Anthropic documentation at docs.anthropic.com is thorough and kept up to date.
Want to deploy Anthropic AI in your business? Book a free consultation.