Part ofClaude AI Features: The Complete Overview
How to use Claude API from scratch: get an API key, learn the Messages request, and make your first call in curl and Python — with model and cost basics.
In This Article
9 sectionsHow to use Claude API comes down to three moves: create an account and generate an API key in the Anthropic Console, then send a POST request to the Messages endpoint with a model, a max_tokens value, and a messages array. New accounts get a small starter credit, so you can test your first call for free.
We run this site's entire content pipeline on the Claude API, so we set up new keys and first requests often enough to have a routine. The good news for anyone starting out: the API is a single REST endpoint, it's language-agnostic, and you can go from zero to a working response in about ten minutes. This Claude API tutorial walks through every step — the API key, the Messages API structure, a first request in curl and Python, picking a model, streaming, system prompts, and what it all costs.
How to use the Claude API: the five steps
Here's the whole path at a glance. Each step below expands on one row, and following all five completes the standard Anthropic API getting-started flow.
- Get an API key from the Anthropic Console.
- Learn the Messages API — the one endpoint every request hits.
- Send your first request with curl or an official SDK.
- Pick a model that fits your task and budget.
- Add streaming and a system prompt once the basics work.
None of this requires a framework or a running server. Any language that can send an HTTPS request can call it, which is what makes learning how to use the Claude API so quick.
Step 1: Get your Claude API key
Everything starts with a key. Sign up at the Anthropic Console, verify your email, then open the API Keys section and click Create Key. Copy the value immediately — the console shows the full string only once. That single Claude API key authenticates every call you make.
Two housekeeping rules we never skip. First, store the key in an environment variable named ANTHROPIC_API_KEY; the official SDKs read it automatically, so you never paste it into source code. Second, the Claude API is prepaid pay-as-you-go — add a small amount of credit (new accounts include a starter credit) before your first real request. There is no monthly subscription and no free perpetual tier on the API itself.
Anatomy of a Messages API request
Every Claude API call — no matter the language — hits one endpoint: POST https://api.anthropic.com/v1/messages. Tool use, images, and streaming are all options on this single Messages API, not separate services. A request is a small JSON object, and these are the fields that matter.
| Field | Required? | What it does |
|---|---|---|
model | Yes | The model ID, e.g. claude-opus-4-8, claude-sonnet-5, or claude-haiku-4-5. |
max_tokens | Yes | Hard cap on the tokens the model may generate in its reply. |
messages | Yes | Ordered array of turns; each has a role and content. |
role | Yes (per message) | user or assistant — who is speaking in that turn. |
content | Yes (per message) | The text (or content blocks) for that turn. |
system | No | Top-level string that sets persona, rules, and context. |
stream | No | Set true to receive tokens incrementally as Server-Sent Events. |
Three ideas do most of the work. Roles alternate: your user turn goes in, the model replies as assistant, and a multi-turn conversation is just that array growing. The API is stateless, so you resend the full history on every call. The system prompt sits outside the conversation — it's where you set the model's persona, tone, and constraints, and it persists across every turn. Content is usually a plain string, but it can also be a list of blocks when you send images or documents.
Your first request: curl and Python
With a key in hand, here's how to use the Claude API in practice. Start with curl — it proves the key works before you touch any SDK:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-8",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Explain the Claude API in one sentence."}
]
}'
Two headers carry the weight: x-api-key holds your key, and anthropic-version pins the API version so future changes never break your integration.
For real projects, use an official SDK. Install it, and the Python call to the Claude API is four lines:
pip install anthropic
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from your environment
message = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain the Claude API in one sentence."}
],
)
print(message.content[0].text)
That's the whole loop: build a messages array, hand it to messages.create(), and read the text off the first content block. The official Python and TypeScript SDKs wrap this exact endpoint, and Anthropic's getting-started documentation shows the same snippet in every supported language.
Which Claude model should you pick?
The model field is the one line you'll change most. Anthropic groups its lineup into three tiers, and the right pick comes down to how much reasoning your task needs versus how fast and cheap you want the answer.
| Model | Model ID | Best for | Price (input / output per million tokens) |
|---|---|---|---|
| Claude Opus 4.8 | claude-opus-4-8 | Hardest reasoning, complex agents, large refactors | $5 / $25 |
| Claude Sonnet 5 | claude-sonnet-5 | Balanced everyday coding and writing | $3 / $15 ($2 / $10 intro through Aug 31) |
| Claude Haiku 4.5 | claude-haiku-4-5 | Speed and high-volume, lightweight tasks | $1 / $5 |
Our standing rule: start on Claude Sonnet 5, reach for Claude Opus 4.8 only when Sonnet measurably falls short, and drop to Claude Haiku 4.5 wherever the output is mechanically verifiable. Opus 4.8 leads on the hardest work — it tops the SWE-bench Pro benchmark at 69.2% — but that reasoning is wasted on classification or extraction. For the full lineup and what each tier is built for, see our Claude models explained guide, and remember that how much you can send in one request is bounded by the Claude context window.
Streaming and system prompts
Two upgrades round out how to use the Claude API day to day, and each is a one-line change.
A system prompt sets the rules once and keeps them for the whole conversation. Pass it as the top-level system parameter:
message = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system="You are a concise technical writer. Answer in three sentences or fewer.",
messages=[{"role": "user", "content": "What is prompt caching?"}],
)
Everything you'd otherwise repeat in every prompt — persona, format rules, domain context — belongs in system. Getting the wording right is its own discipline; our Claude prompt engineering guide covers the patterns we rely on.
Streaming returns tokens as they're generated instead of making the caller wait for the full reply — essential for chat UIs and long outputs. Swap .create() for .stream():
with client.messages.stream(
model="claude-opus-4-8",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short poem about APIs."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Under the hood this is a Server-Sent Events stream; the SDK parses each event so you only consume text. Streaming also sidesteps request timeouts on large outputs, which is why we default to it for anything long.
Claude API cost basics
Knowing how to use the Claude API also means knowing what it costs. Pricing is per token, billed separately for input and output, with no monthly fee — you pay only for what you send and receive. Roughly four characters equal one token, so a 750-word page is about 1,000 tokens. Every response returns a usage object with exact input and output counts, so you can track spend precisely rather than guessing.
| Cost lever | Effect |
|---|---|
| Input tokens | Your prompt, system message, and history — billed at the model's input rate. |
| Output tokens | Everything the model generates — billed at 5x the input rate on every model. |
| Starter credit | New accounts get a small credit to test before adding funds. |
| Batch API | 50% off tokens for asynchronous jobs returned within 24 hours. |
For worked examples, per-model math, and the caching and batching discounts that cut real bills, see our Claude API pricing guide.
Next steps after your first call
You now know how to use the Claude API from key to first response, and the surface opens up fast from here. Add tool use to let the model call your own functions, prompt caching to slash the cost of repeated context, and vision to send images alongside text. If you'd rather have an AI agent write the app for you than call the endpoint yourself, that's Claude Code, Anthropic's agentic coding tool — it needs a paid plan or API credits. And to see how the API maps onto the wider product line — claude.ai, Projects, Artifacts — our Claude AI features overview connects the pieces. One limit to know up front: Claude does not generate images, only text.
Claude pricing at a glance
| Plan | Price |
|---|---|
| Free | $0 |
| Pro | $20 / month |
| Max | from $100 / month |
| API | Pay per token |
For the full breakdown of every plan, see our how much Claude costs guide.
Frequently Asked Questions

Written by
InnovateTechie
Writing about Claude and the Anthropic toolkit — models, Claude Code, pricing, features, and fixes, in clear, practical, hands-on guides tested by daily use.
View all posts →





