Skip to content
InnovateTechie
Claude API

How to Use the Claude API: A Getting-Started Guide

EdithBy Edith14 min read
Share
How to use the Claude API — API key, a Messages API request, and a first call in curl and Python

Part ofClaude AI Features: Everything It Can Do (2026)

Quick answer

Learn how to use Claude API: get an API key from the Anthropic Console, understand the Messages API, and make your first request in curl or Python.

How 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.

Model IDs, endpoints, and rates verified 31 July 2026 against Anthropic's getting-started docs.

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. In practice, how to use Claude API is mostly a matter of learning this one endpoint and its handful of required fields.

Key takeaway

To use the Claude API, generate a key in the Anthropic Console and POST to https://api.anthropic.com/v1/messages with a model, a max_tokens value, and a messages array — new accounts get a small starter credit, and calls cost from $1/$5 (Haiku 4.5) to $5/$25 (Opus 5) per million input/output tokens.

How to use Claude API: the five steps

Get a key, learn the Messages endpoint, send a first request, pick a model, then add streaming and a system prompt. No framework and no server required.

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. That five-step sequence is really all how to use Claude API involves.

  1. Get an API key from the Anthropic Console.
  2. Learn the Messages API — the one endpoint every request hits.
  3. Send your first request with curl or an official SDK.
  4. Pick a model that fits your task and budget.
  5. 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 Claude API so quick.

Step 1: Get your Claude API key

Sign up at the Anthropic Console, open API Keys, click Create Key, and copy it immediately — the full string is shown only once.

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. Getting this key is the true starting point for how to use Claude API.

Anatomy of a Messages API request

Every call hits one endpoint: POST https://api.anthropic.com/v1/messages. Three fields are required — model, max_tokens, and messages.

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.

FieldRequired?What it does
modelYesThe model ID, e.g. claude-opus-5, claude-sonnet-5, or claude-haiku-4-5.
max_tokensYesHard cap on the tokens the model may generate in its reply.
messagesYesOrdered array of turns; each has a role and content.
roleYes (per message)user or assistant — who is speaking in that turn.
contentYes (per message)The text (or content blocks) for that turn.
systemNoTop-level string that sets persona, rules, and context.
streamNoSet 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. Get any of these wrong — a missing max_tokens, an empty array, roles that don't alternate — and the request is rejected before the model ever runs, which is what our guide to the Claude API 400 error unpacks cause by cause.

Anatomy of a Claude API Messages request showing the model, max_tokens, messages, roles, and system fields

Your first request: curl and Python

Start with curl to prove the key works, then move to an official SDK. The Python version is four lines once anthropic is installed.

With a key in hand, here's calling the 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-5",
    "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-5",
    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?

Start on Sonnet 5, escalate to Opus 5 only when Sonnet measurably falls short, and drop to Haiku 4.5 wherever output is mechanically verifiable.

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.

ModelModel IDBest forPrice (input / output per million tokens)
Claude Opus 5claude-opus-5Hardest reasoning, complex agents, large refactors$5 / $25
Claude Sonnet 5claude-sonnet-5Balanced everyday coding and writing$3 / $15 ($2 / $10 intro through Aug 31)
Claude Haiku 4.5claude-haiku-4-5Speed and high-volume, lightweight tasks$1 / $5

Our standing rule: start on Claude Sonnet 5, reach for Claude Opus 5 only when Sonnet measurably falls short, and drop to Claude Haiku 4.5 wherever the output is mechanically verifiable. The Opus tier leads on the hardest work — Opus 4.8 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.

How to use the Claude API — choosing between Claude Opus 5, Sonnet 5, and Haiku 4.5 by task and budget

Streaming and system prompts

Two one-line upgrades: a top-level system parameter sets persona and rules for the whole conversation, and .stream() returns tokens as they generate.

Two upgrades round out calling the 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-5",
    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. If you would rather not start from a blank page, the Claude prompt generator in the Console drafts a structured template for you from a plain description of the task.

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-5",
    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)

Together, streaming and system prompts are the last piece of calling the API comfortably in production. 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 — though if the connection goes quiet mid-response you can hit a stream idle timeout that leaves you with a partial response.

Claude API cost basics

Per token, billed separately for input and output, with no monthly fee. Output costs 5× input on every model, and every response returns exact counts in usage.

Knowing calling the 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 is the final variable in calling the API without surprises.

Cost leverEffect
Input tokensYour prompt, system message, and history — billed at the model's input rate.
Output tokensEverything the model generates — billed at 5x the input rate on every model.
Starter creditNew accounts get a small credit to test before adding funds.
Batch API50% 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

Tool use, prompt caching, structured output, and vision are all options on the same endpoint — compose them as you need them.

You now know calling the API from key to first response, and the surface opens up fast from here. From this point, calling the API is really about composing these optional features as you need them. Add tool use to let the model call your own functions — just watch for the 400 "tool use concurrency" error once you start firing parallel calls — prompt caching to slash the cost of repeated context, and vision to send images alongside text. If your code has to parse the reply rather than display it, Claude API structured output constrains the response to a JSON schema so it cannot come back as prose. 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

New API accounts get a small starter credit, so your first calls cost nothing. Beyond that it is strictly pay-per-token.

PlanPrice
Free$0
Pro$20 / month
Maxfrom $100 / month
APIPay per token

For the full breakdown of every plan, see our how much Claude costs guide. Once your first call works, the Claude API reference maps the rest of the surface — endpoints, response blocks, streaming.

Deploying through AWS? Claude Platform on AWS explains how it differs from Amazon Bedrock — they are not the same thing.

Prefer to test prompts before you write code? The Claude Workbench is Anthropic Console's playground — draft, run, and export the exact API call.

Sending the same document to Claude on every request? Upload it once with the Claude Files API and reference it by ID instead of re-sending the bytes each time.

Working in Python? The Claude Python SDK guide takes you from pip install anthropic to a first streamed response, with typed errors and retries handled for you.

Listing batches, files, or models? Claude API pagination covers the cursor loop — has_more, last_id, after_id — and the SDK shortcut that pages through everything for you.

Frequently Asked Questions

Sign up at the Anthropic Console, verify your email, then open the API Keys section and click Create Key. Copy the value immediately — it's shown in full only once. Store it in an environment variable rather than pasting it into code, and add prepaid credit before your first request.

No — the Claude API is prepaid, pay-as-you-go, billed per input and output token with no monthly fee. New accounts do get a small starter credit, so you can run your first requests and experiment for free before adding funds. Beyond that credit, you pay only for what you use.

Pick by task. Claude Opus 5 handles the hardest reasoning, complex agents, and large refactors. Claude Sonnet 5 gives the best balance of price and performance for everyday coding and writing. Claude Haiku 4.5 is fastest and cheapest for lightweight, high-volume work. Start on Sonnet and escalate only when it falls short.

The Claude API is a REST endpoint you call from your own code to build apps. Claude Code is Anthropic's AI coding tool that writes and edits the app for you in your terminal. The API is a building block; Claude Code is a finished agent built on top of it. See What is Claude Code?

Store the key in an environment variable and load it from there — never hard-code it. Keep it out of frontend JavaScript, where anyone can read it, and never commit it to a public repository. If a key leaks, revoke it in the Console and generate a new one immediately.

No. The Claude API is language-agnostic REST over HTTP, so any language that can send an HTTPS request works — including a plain curl command. Anthropic ships official SDKs for Python and TypeScript to make it easier, but they're optional wrappers around the same Messages endpoint, not a requirement.

Pass a top-level system parameter to messages.create() (or the system field in raw JSON). It sets the model's persona, context, and constraints, and those instructions persist across every turn of the conversation, separate from the user and assistant messages. Keep task rules and formatting requirements there rather than repeating them each turn.
Edith

Written by

Edith

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 →