Your First API Call
The API lets your program talk to Claude. Three steps: get a key, install the SDK, send a message.
1. Get an API key
Create one in the Anthropic Console. Then set it as an environment variable so it never lives in your code:
export ANTHROPIC_API_KEY="sk-ant-..."
:::warning Never commit your key Keep keys in environment variables or a secrets manager — never in source control. See Security. :::
2. Install the SDK
- Python
- TypeScript
- cURL
pip install anthropic
npm install @anthropic-ai/sdk
Nothing to install — you just need curl.
3. Make the call
Every request is a list of messages. The model replies with content.
- Python
- TypeScript
- cURL
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "In one sentence, what is an API?"}
],
)
print(message.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY from the environment
const message = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [
{ role: "user", content: "In one sentence, what is an API?" },
],
});
console.log(message.content[0].text);
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-6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "In one sentence, what is an API?"}
]
}'
What just happened
model— which Claude to use. Don't hard-code blindly; see Choosing a Model.max_tokens— a cap on the reply length (in tokens). It does not set the context window.messages— the conversation so far. The API is stateless: to continue a chat, send the whole history back each time.system(optional) — a top-level instruction that sets Claude's role for the call.
Next
- Pick the right model & estimate cost → Choosing a Model · Tokens & Pricing
- Stream responses and hold a conversation → Streaming & Multi-Turn
- Let Claude call your functions → Tool Use
- Production-ready snippets → API Snippets