Skip to content
InnovateTechie
Claude API

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

InnovateTechieBy InnovateTechie10 min read
Share
How to Use the Claude API: A Getting-Started Guide

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.

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.

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.

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

FieldRequired?What it does
modelYesThe model ID, e.g. claude-opus-4-8, 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.

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

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.

ModelModel IDBest forPrice (input / output per million tokens)
Claude Opus 4.8claude-opus-4-8Hardest 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 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.

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

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

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

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.

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 4.8 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?](/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.
InnovateTechie

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 →