Skip to main content

Your First API Call

Beginner

The API lets your program talk to Claude. By the end of this lesson you'll have run a real request and you'll understand exactly what each piece of it does.

What you'll learn
  • Create an API key and store it safely as an environment variable
  • Install the official SDK for Python or TypeScript (or use plain cURL)
  • Send your first messages request and read Claude's reply
  • Understand the four core fields: model, max_tokens, messages, and system

The whole journey in three steps

Everything below fits into one simple sequence. Keep this map in mind as you go.

Guided walkthrough1 of 3
  1. Create a key in the Anthropic Console and export it as an environment variable so it never lives in your code.

1. Get an API key

Create one in the Anthropic Console. Then set it as an environment variable so it never lives in your code:

Export your API key

export ANTHROPIC_API_KEY="sk-ant-..."
Watch out
  • Never commit your key. Keep keys in environment variables or a secrets manager, never in source control. See the Security page (/docs/security).

2. Install the SDK

Pick your language. The SDK reads the key automatically from the environment variable you just set.

Install (Python)

pip install anthropic

3. Make the call

Every request is a list of messages. The model replies with content. Run the snippet for your language and you'll see Claude answer the question.

import anthropic

client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment

message = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "In one sentence, what is an API?"}
],
)

print(message.content[0].text)

What just happened

You sent four pieces of information. Here's what each one controls:

  • model — which Claude to use. Don't hard-code blindly; see Choosing a Model.
  • max_tokens — a cap on the reply length (in tokens). It does not set the context window.
  • messages — the conversation so far. The API is stateless: to continue a chat, send the whole history back each time.
  • system (optional) — a top-level instruction that sets Claude's role for the call.
The four core fields
Press Enter or Space to flip the card. Use the left and right arrow keys to move between cards.Term shown.
1 / 4
Pro tip
  • Because the API is stateless, a multi-turn chat is just a growing messages array: append each reply and resend the full list on the next call.

Check yourself

0/3
  1. What does max_tokens control?
  2. How do you continue a multi-turn conversation with the API?
  3. Where should your API key live?
Key takeaways
  • Three steps: get a key, install the SDK, send a messages request.
  • Store the key in ANTHROPIC_API_KEY so the SDK reads it automatically and it never enters your code.
  • A request is a list of messages; the reply comes back in content.
  • The API is stateless: resend the full conversation history to continue a chat.
  • model, max_tokens, messages, and system are the fields you'll reach for first.

Next