Skip to main content

Your First API Call

Beginner

The API lets your program talk to Claude. Three steps: get a key, install the SDK, send a message.

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 ANTHROPIC_API_KEY="sk-ant-..."

:::warning Never commit your key Keep keys in environment variables or a secrets manager — never in source control. See Security. :::

2. Install the SDK

pip install anthropic

3. Make the call

Every request is a list of messages. The model replies with content.

import anthropic

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

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

print(message.content[0].text)

What just happened

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

Next