To use the Claude API, create an API key in the Claude Console, store it in the ANTHROPIC_API_KEY environment variable, and send a POST request to https://api.anthropic.com/v1/messages with a model ID, max_tokens and a messages list. The official Python and TypeScript SDKs read the key from that variable automatically, so a first working request is about ten lines of code.
Everything below follows Anthropic's own documentation as of October 2026. Model IDs and prices change, so check the models overview before you hard-code one.
Step 1: Get an API key
You need a Claude Console account at platform.claude.com and an API key created there. API access is billed per token. Treat the key like a password: anyone who has it can spend your credit. If one ever ends up in a repository or a screenshot, follow what to do when an API key leaks.
Step 2: Put the key in an environment variable
macOS and Linux (bash or zsh), for the current terminal:
export ANTHROPIC_API_KEY="your-api-key-here"
Windows PowerShell, for the current window:
$env:ANTHROPIC_API_KEY = "your-api-key-here"
Both only last until you close the terminal. To make the variable permanent, see how to set environment variables on Windows, macOS and Linux. Keeping the key out of your source code means you can share or commit the code without leaking it.
Step 3: Send your first request with curl
This is the request from Anthropic's quickstart, with a shorter prompt:
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-5-5",
"max_tokens": 1000,
"messages": [
{"role": "user", "content": "Explain what an API key is in two sentences."}
]
}'
Three headers are required: x-api-key carries the key, anthropic-version selects the API version (Anthropic's quickstart uses 2023-06-01; the newest, 2026-10-21, removes a few parameters), and content-type says the body is JSON. In the body, model picks the model, max_tokens caps the length of the reply, and messages holds the conversation. The PowerShell line-continuation character is a backtick, not a backslash, so on Windows it is easier to run this from Git Bash or WSL, or to use an SDK.
What the response looks like
A successful call returns JSON shaped like this (example from Anthropic's docs):
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "Hello!" }
],
"model": "claude-opus-5-5",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": { "input_tokens": 12, "output_tokens": 6 }
}
The reply text is in content, which is a list of blocks, not a plain string. stop_reason tells you why it stopped: end_turn means Claude finished, and max_tokens means it hit your limit mid-answer, so raise max_tokens if replies are cut off. usage is what you are billed on; how LLM API pricing works shows how to turn it into a cost.
Python: install the SDK and make the call
python -m venv .venv
# macOS/Linux: source .venv/bin/activate Windows: .venv\Scripts\activate
pip install anthropic
Create quickstart.py:
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1000,
messages=[
{"role": "user", "content": "Explain what an API key is in two sentences."}
],
)
for block in message.content:
if block.type == "text":
print(block.text)
Run it with python quickstart.py. As of October 2026, pip install anthropic installs version 2.3.0.
TypeScript / Node.js: the same call
npm init -y
npm pkg set type=module
npm install @anthropic-ai/sdk
Create quickstart.ts:
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const message = await client.messages.create({
model: "claude-opus-5-5",
max_tokens: 1000,
messages: [
{ role: "user", content: "Explain what an API key is in two sentences." }
]
});
for (const block of message.content) {
if (block.type === "text") console.log(block.text);
}
Run it with npx tsx quickstart.ts. Anthropic also publishes official SDKs for C#, Go, Java, PHP and Ruby, with the same request shape.
Which model ID to use
Anthropic's models overview lists these current models (as of October 2026):
| Model | API ID | Price per million tokens (input / output) | Positioned for |
|---|---|---|---|
| Claude Fable 5.1 | claude-fable-5-1 | $10 / $50 | Demanding reasoning, long-horizon agentic work |
| Claude Opus 5.5 | claude-opus-5-5 | $4 / $20 | The suggested starting point for most workloads |
| Claude Sonnet 5.5 | claude-sonnet-5-5 | $2 / $10 | Speed and intelligence balance |
| Claude Haiku 5.5 | claude-haiku-5-5 | from $0.10 / from $0.50 | High-volume, latency-sensitive tasks |
All four have a 1M-token context window and up to 128K output tokens on the synchronous API. The docs note that every model ID is a pinned snapshot, including these dateless ones, so the same ID keeps pointing at the same model.
Add a system prompt and keep a conversation going
Instructions that apply to the whole conversation go in the top-level system field, not in the messages list. The API is stateless: it does not remember earlier requests, so you send the full history every time, alternating user and assistant turns:
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system="You answer in plain English for beginners.",
messages=[
{"role": "user", "content": "Hello, Claude"},
{"role": "assistant", "content": "Hello!"},
{"role": "user", "content": "Can you describe LLMs to me?"},
],
)
Every earlier turn counts as input tokens again on each request, so long chats get more expensive per message. System prompt vs user prompt covers what belongs in each.
Errors you will see first
These are real responses from the API, captured with curl on 26 October 2026 using a fake key and then no key:
{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"},"request_id":"req_011CgRFGJ9s8YN8NYeDH5NNt"}
{"type":"error","error":{"type":"authentication_error","message":"x-api-key header is required"},"request_id":"req_011CgRFGKoaLcsqstmeHUocn"}
In Python, creating the client with no key set fails before any request is sent:
TypeError: "Could not resolve authentication method. Expected one of api_key, auth_token, or credentials to be set. Or for the `Authorization` header to be explicitly omitted"
That almost always means the variable was set in a different terminal, or the IDE was started before you set it. The main HTTP errors, per Anthropic's error reference:
| Status | Type | Usual cause |
|---|---|---|
| 400 | invalid_request_error | Malformed body, unsupported parameter, or a spend limit you set was reached |
| 401 | authentication_error | Key missing, malformed, revoked or expired |
| 402 | billing_error | Billing or payment problem |
| 429 | rate_limit_error | Rate limit or monthly spend cap hit |
| 529 | overloaded_error | API temporarily overloaded |
The SDKs retry connection errors, 429s and 5xx errors twice by default with exponential backoff. For persistent 429s, see rate limit error 429 in LLM APIs. Every response carries a request-id header; include it if you contact Anthropic support.
If it still doesn't work
Print the length of the key your program sees (never the key itself) to confirm the variable reached it. Check that the model ID is spelled exactly as in the table. If you copied an older tutorial that sets temperature or prefills the start of Claude's reply, remove those: Anthropic's docs say current models reject non-default temperature, top_p and top_k values and do not support assistant-message prefill.
Frequently asked questions
- Is the Claude API free?
- No. The API is billed per input and output token. As of October 2026 Anthropic's models overview lists Claude Opus 5.5 at $4 per million input tokens and $20 per million output tokens, and Claude Haiku 5.5 from $0.10 and $0.50. Check the pricing page before you build, because prices change.
- Does the Claude API remember previous messages?
- No. Anthropic's docs describe the Messages API as stateless: every request must include the full conversation history in the messages array. To continue a chat, append Claude's reply as an assistant message and your next question as a user message, then send the whole list again.
- What does the anthropic-version header do?
- Every HTTP request must send it, and it selects the API version your code expects. Within a version, Anthropic says it preserves existing input and output parameters. The quickstart uses 2023-06-01; the newest version as of October 2026 is 2026-10-21, which removes temperature, top_p and top_k. The official SDKs set the header for you.
- Why do I get a 400 error when I set temperature?
- Anthropic's docs say the temperature, top_p and top_k parameters are not supported on Claude 4.7 and later models: setting them to a non-default value returns a 400 error. Leave them out and steer the output through the prompt instead.