Your First API Call
The API lets your program talk to Claude. By the end of this lesson you'll have run a real request and you'll understand exactly what each piece of it does.
- Create an API key and store it safely as an environment variable
- Install the official SDK for Python or TypeScript (or use plain cURL)
- Send your first messages request and read Claude's reply
- Understand the four core fields: model, max_tokens, messages, and system
The whole journey in three steps
Everything below fits into one simple sequence. Keep this map in mind as you go.
Guided walkthrough1 of 3
- Create a key in the Anthropic Console and export it as an environment variable so it never lives in your code.
- Add the anthropic package for Python or @anthropic-ai/sdk for TypeScript. With cURL there is nothing to install.
- Send a list of messages to the model and print the content it returns.
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 your API key
export ANTHROPIC_API_KEY="sk-ant-..."
- Never commit your key. Keep keys in environment variables or a secrets manager, never in source control. See the Security page (/docs/security).
2. Install the SDK
Pick your language. The SDK reads the key automatically from the environment variable you just set.
- Python
- TypeScript
- cURL
Install (Python)
pip install anthropic
Install (TypeScript)
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. Run the snippet for your language and you'll see Claude answer the question.
- Python
- TypeScript
- cURL
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
message = client.messages.create(
model="claude-sonnet-5",
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-5",
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-5",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "In one sentence, what is an API?"}
]
}'
What just happened
You sent four pieces of information. Here's what each one controls:
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.
The four core fields
Press Enter or Space to flip the card. Use the left and right arrow keys to move between cards.Term shown.1 / 4
- Because the API is stateless, a multi-turn chat is just a growing messages array: append each reply and resend the full list on the next call.
Check yourself
0/3- Three steps: get a key, install the SDK, send a messages request.
- Store the key in ANTHROPIC_API_KEY so the SDK reads it automatically and it never enters your code.
- A request is a list of messages; the reply comes back in content.
- The API is stateless: resend the full conversation history to continue a chat.
- model, max_tokens, messages, and system are the fields you'll reach for first.
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