Modified

Claude API: get started step by step

The Claude API is Anthropic's interface for programs that want to use Claude. Instead of you chatting on claude.ai, your program sends a question to Anthropic's servers and gets Claude's answer back as data. You create an account in the Claude Console, add credits and create an API key. Then you pay per token, a piece of text a few characters long. This guide is part 3 of 3 and assumes no prior knowledge of APIs.

  1. Create a Claude Console accountat platform.claude.com
  2. Add creditsyou pay per token
  3. Create an API keyit is shown only once
  4. Store the key in ANTHROPIC_API_KEYnever directly in the code
  5. Send your first requestwith curl, Python or TypeScript
Five steps from account to Claude's first answer in your own program.

Get started

What is an API and an API key?

An API is a way for programs to talk to each other. Your program sends a request to an address on the internet. The service answers with data in a fixed format, usually JSON, a text format for data.

The Claude API works the same way. Your program sends text to Claude, and Claude's answer comes back to the program.

An API key is a password that shows who is sending the request, so the right account gets the bill. The key is secret. Anyone who has your key can use Claude at your expense. So never put the key in your code, in an email or in git, the program that keeps versions of code.

How an API request travels

Your programan app, a script, a web page$ANTHROPIC_API_KEY
The question and the keythe key shows who pays
The answer as dataand how many tokens it used
The company's serverapi.anthropic.comClaude
Tokens inwhat you send
Tokens outwhat the model writes, costs more
You pay for tokens in both directions. The key stays in the environment variable, never in the code.

Who is the Claude API for?

The Claude API is for you if you want a program of your own to use Claude.

The apps in part 1 are enough if you just want to use Claude yourself. Claude Code in part 2 is a better fit if Claude should write code in your files.

Part 2 also explains the terminal, the window where you run the commands in this guide, and the words you meet there.

Things you can build with the Claude API:

  • a chatbot on a website
  • a script that summarises documents
  • a service that sorts customer emails
  • your own AI agent

How do you create an account and an API key?

You create the account in the Claude Console at platform.claude.com. There you buy credits, and Anthropic deducts each request from them.

You create the key on the API keys page in the Console, platform.claude.com/settings/keys. Click Create key, give the key a name and choose how long it should stay valid.

The Console shows the full key only once, according to Anthropic's API key guide. The key starts with sk-ant-. Copy it at once and keep it somewhere safe, such as a password manager. If you lose the key, create a new one.

Next, store the key in the ANTHROPIC_API_KEY environment variable. Anthropic's libraries, ready-made code your program uses, read the variable automatically, according to Anthropic's quickstart. That way the key never has to appear in your code.

The export command on Mac, Linux and WSL and the $env: command in PowerShell last until you close the terminal window. With setx, Windows saves the key for all new terminal windows, according to Anthropic's page on authentication for its CLI. Open a new window afterwards, and replace the example value with your own key.

Store the API key as an environment variable
export ANTHROPIC_API_KEY="your-api-key-here"
Store the API key permanently on Windows
Terminal
setx ANTHROPIC_API_KEY "sk-ant-api03-..."

How do you send your first request to the Claude API?

You call the Claude API with curl straight from the terminal, or with Anthropic's libraries for the Python and TypeScript programming languages. The examples below come from Anthropic's quickstart. Every example reads the key from ANTHROPIC_API_KEY and asks the same model the same question. The model name in the examples is correct at the time of writing, and Anthropic's models page lists the current names.

curl is a program that sends requests over the internet. The request goes to https://api.anthropic.com/v1/messages with three headers. The headers carry the key, the API version and a note that the content is JSON. The answer comes back as JSON, with Claude's text under content.

Run the curl example on Mac, Linux or WSL. The backslashes \ at the end of the lines do not work in PowerShell.

The Python library needs Python 3.10 or later, according to Anthropic's page on the library. First create a folder and a virtual environment, a separate place for the project's libraries. Then install the anthropic library with pip, the Python installer. Save the code as quickstart.py in the folder and run it with python quickstart.py.

You install the TypeScript library @anthropic-ai/sdk with npm. Save the code as quickstart.ts and run it with npx tsx quickstart.ts. The program prints Claude's answer in the terminal.

First request with curl (Mac, Linux, WSL)
Terminal
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1000,
    "messages": [
      {
        "role": "user",
        "content": "What should I search for to find the latest developments in renewable energy?"
      }
    ]
  }'
Install the Python library
mkdir claude-quickstart && cd claude-quickstart
python3 -m venv .venv && source .venv/bin/activate
pip install anthropic
First request in Python (quickstart.py)
Terminal
import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1000,
    messages=[
        {
            "role": "user",
            "content": "What should I search for to find the latest developments in renewable energy?",
        }
    ],
)

for block in message.content:
    if block.type == "text":
        print(block.text)
Install the TypeScript library
Terminal
mkdir claude-quickstart && cd claude-quickstart
npm init -y
npm pkg set type=module
npm install @anthropic-ai/sdk
First request in TypeScript (quickstart.ts)
Terminal
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1000,
  messages: [
    {
      role: "user",
      content: "What should I search for to find the latest developments in renewable energy?"
    }
  ]
});

for (const block of message.content) {
  if (block.type === "text") {
    console.log(block.text);
  }
}

How is a request to Claude built?

A request to the Messages API, the part of the Claude API that takes conversations, has three parts in all of Anthropic's examples. model says which model should answer. max_tokens sets the longest the answer may be. messages is the conversation.

Each message in the conversation has a role. The user role is you, and the assistant role is Claude.

The API remembers nothing between requests, Anthropic points out. To continue a conversation, you send the whole history every time.

A system prompt goes in the system field. It holds the rules for the whole conversation, for example "always answer briefly and in Swedish". The prompt guide shows how to write good instructions.

The answer has a stop_reason field that says why Claude stopped. Anthropic's stop reasons page lists several values. end_turn means the answer is complete. max_tokens means the limit was reached. tool_use means Claude wants to use a tool.

Models and features

Which models are there and what does the Claude API cost?

The Claude API is priced per million tokens according to Anthropic's pricing page, with one price for what you send in and a higher price for what Claude writes. Larger models cost more per token than smaller ones. Text read from the cache costs less, and requests in a batch cost less, as the sections on prompt caching and the Batch API show.

Opus costs 4 dollars per million tokens in and 20 dollars per million tokens out at the time of writing, according to Anthropic's pricing page. Prices change, so always check the pricing page.

The models come in four families, and Anthropic describes them like this:

  • Fable is the largest family, for the hardest and longest tasks.
  • Opus is for complex work and advanced code. At the time of writing, Anthropic suggests most people start with Opus.
  • Sonnet is the balanced family and suits everyday coding and agent work.
  • Haiku is the fastest and cheapest family, according to Anthropic.

What is streaming?

Streaming means the answer arrives piece by piece while Claude writes, instead of all at once. You turn streaming on with "stream": true in the request. The answer then arrives as server-sent events, a stream of small messages.

Streaming makes a chatbot feel fast, because the user sees the first words right away.

What is tool use and function calling?

Tool use, also called function calling, lets Claude ask your program for help. A tool could, for example, be called get_weather and take a location.

You describe the tools in the tools field. Each tool has a name in name, a description in description and a schema in input_schema. The schema says what input the tool takes.

When Claude needs the tool, the answer ends with stop_reason set to tool_use. The answer also contains a block with the tool name and the input.

Your program runs the tool and sends the result back as a tool_result in the next message. Claude then writes its answer from the result. Most AI agents work this way.

Anthropic also has ready-made tools that run on Anthropic's servers, for example web search, web page fetching and code execution.

How a tool call travels

  1. Sends the question and the available tools
  2. Replies: run the tool, with these values
  3. Runs the code itself, for example looks up an order
  4. Sends the result back
  5. Writes the answer with the result
The model never runs the code itself. Check the values before your program does something that cannot be undone.

What is prompt caching?

Prompt caching lets Anthropic store the start of a request you send many times, for example a long system prompt or a document. You mark that part with cache_control. The next request then reads it from the cache, a fast store, instead of processing it again.

The cache lasts five minutes by default, according to Anthropic's prompt caching page. Each time the cache is used, Anthropic refreshes it at no extra cost.

Reading from the cache costs only a small part of the normal input price, according to the prompt caching page. Writing to the cache costs somewhat more than the normal input price, and a cache that should last longer costs more to write. The same page lists the factors for each model.

What is the Batch API?

The Batch API, also called Message Batches, lets you send many requests at once and get the answers later, at half the price at the time of writing, according to Anthropic.

A batch has a cap on the number of requests and on its size, and a batch that is not done in time expires, according to Anthropic's documentation. The batch processing documentation lists the caps and the times.

Batches suit large jobs that are not urgent, for example summarising thousands of documents overnight.

What is the Claude Agent SDK?

The Claude Agent SDK is the same engine that powers Claude Code, as a library for Python and TypeScript. SDK stands for software development kit, a ready-made package for people who build programs.

With the SDK your program gets Claude Code's agent loop, sessions and hooks. The program also gets tools for reading and changing files and running commands. You do not have to build any of it yourself.

The SDK needs Python 3.10 or later or Node.js 18.14 or later, according to Anthropic's Agent SDK overview. Node.js is the program that runs JavaScript and TypeScript outside the browser. You also need an API key in ANTHROPIC_API_KEY.

Install the Claude Agent SDK
python3 -m venv .venv
source .venv/bin/activate
pip install claude-agent-sdk

Terms and comparison

What limits does the Claude API have?

The Claude API has limits on how much you may send. Anthropic counts the limits in requests per minute and in tokens per minute, both for what you send in and for what Claude writes.

Error code 429 means you have gone over a limit. The answer often has a retry-after header that says how many seconds to wait.

The limits depend on the account's tier. Anthropic moves the account up automatically based on its usage history and standing, according to the documentation.

Each tier also caps how much you can spend per month. Anthropic's rate limits page lists the caps and limits for each tier.

Is your data used to train Claude?

Anthropic does not train its models on what you send to the API or on the answers, according to Anthropic's privacy center at the time of writing. The API runs under Anthropic's commercial terms.

Anthropic deletes inputs and outputs within 30 days, according to the privacy center. The exceptions include zero-retention agreements, breaches of the usage policy and legal requirements.

Zero Data Retention is an agreement that companies can sign. Under it, Anthropic does not store prompts and answers after the answer has been sent, according to Anthropic's page on API data retention.

If you build a service for others, you are responsible for their personal data. Is AI safe? covers security in general. Also read about prompt injection, text that tries to trick an AI. The risk applies to programs built on the API too.

Is the API included in Claude Pro?

The Claude API is not included in Claude Pro. Anthropic's help page says the API is billed separately from the paid Claude plans. Max, Ultra and Team do include monthly credits for the Claude API, according to the same page.

A subscription suits you when you use Claude yourself. The API suits a program doing the work.

Claude API, OpenAI API or Gemini API?

The Claude API, the OpenAI API and the Gemini API are the same kind of service from three companies, Anthropic, OpenAI and Google. They differ in their models, their prices and their ready-made tools.

Try the same task against two of them. Compare quality and cost before you build further.

Common questions about the Claude API

Is the Claude API free?

The Claude API is not free. You pay per token with credits you buy in the Claude Console. New users get a small amount of free credits to test the API, according to Anthropic's pricing page.

How do you see what a request cost?

Every answer has a usage field that shows how many tokens the question and the answer used, according to Anthropic's documentation. Multiply the count by the model's price per million tokens to get the cost.

Can you cap what the API costs?

You set spend limits on the Billing page in the Claude Console, platform.claude.com/settings/billing, according to Anthropic's rate limits page. The account's tier also sets its own monthly cap.

Can the Claude API answer in Swedish?

The Claude API answers in Swedish when you write in Swedish. Anthropic writes that Claude handles most of the world's languages. To be sure, write "answer in Swedish" in the system prompt.

Can you use the Claude API without programming?

The Claude API needs a little programming. You can still run the curl example in this guide by pasting it into the terminal on Mac, Linux or WSL. If you want to avoid code entirely, the apps at claude.ai suit you better.

Sources: Anthropic: quickstart · Anthropic: API key · Anthropic: models · Anthropic: API pricing · Anthropic: rate limits · Anthropic: Agent SDK · Anthropic: data retention · Anthropic: training on data