Claude API Tutorial: Your First App in Python & TypeScript

A step-by-step Claude API tutorial for beginners: get an API key, install the SDK, send messages, add a system prompt, hold a conversation and stream responses in Python and TypeScript.

Short answer: to call the Claude API, create an API key in the Claude Console, install the official SDK (pip install anthropic or npm install @anthropic-ai/sdk), set ANTHROPIC_API_KEY, and send a list of messages to client.messages.create() with a model name and max_tokens. The rest of this tutorial walks through each step in Python and TypeScript, then covers system prompts, conversations, streaming and the mistakes beginners hit most.

What you need

  • An Anthropic account and an API key from the Claude Console
  • Python 3.9+ or Node.js 18+
  • About 15 minutes

Step 1: Get an API key and keep it out of your code

Create a key in the Claude Console, then store it as an environment variable. Both SDKs read ANTHROPIC_API_KEY automatically, so the key never appears in your source code or your git history.

export ANTHROPIC_API_KEY="sk-ant-..."

In production, put it in your host’s secret manager instead of a .env file that might get committed.

Step 2: Install the SDK

# Python
pip install anthropic

# TypeScript / JavaScript
npm install @anthropic-ai/sdk

Step 3: Send your first message

Python:

import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain what an API is in two sentences."}],
)

for block in response.content:
    if block.type == "text":
        print(block.text)

TypeScript:

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic(); // reads ANTHROPIC_API_KEY

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain what an API is in two sentences." }],
});

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

The response’s content is a list of blocks, not a single string. Check each block’s type before reading text; later, when you add tools, the same list also holds tool calls.

Step 4: Add a system prompt

The system parameter sets the role, rules and output format for the whole conversation. Keep product rules here rather than repeating them in every user message.

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    system="You are a support assistant for an online course. Answer in plain English, under 120 words.",
    messages=[{"role": "user", "content": "Can I pay in installments?"}],
)

Step 5: Hold a conversation

The API is stateless: it remembers nothing between calls. To continue a conversation, send the earlier turns back with each request, alternating user and assistant.

messages = [
    {"role": "user", "content": "Suggest a name for a habit-tracking app."},
    {"role": "assistant", "content": "How about Streakly?"},
    {"role": "user", "content": "Give me three more in the same style."},
]
response = client.messages.create(model="claude-opus-5-5", max_tokens=1024, messages=messages)

Long conversations get expensive because every call resends the history. Summarise or trim old turns once a conversation grows.

Step 6: Stream the answer

For anything a user waits on, stream the text so it appears word by word instead of after a long pause.

with client.messages.stream(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short welcome email for new students."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Choosing a model

  • Claude Opus 5.5 (claude-opus-5-5): the most capable; use it for complex reasoning, coding and agents.
  • Claude Sonnet 5.5 (claude-sonnet-5-5): a strong balance of quality and cost for most product features.
  • Claude Haiku 5.5 (claude-haiku-5-5): the fastest and cheapest; good for classification, routing and high-volume tasks.

Start with the most capable model while you build, measure quality with a small set of test cases, then try a cheaper model and keep it only if those tests still pass.

Common beginner mistakes

  • Reading content[0].text blindly. The first block is not always text. Loop over blocks and check the type.
  • Setting max_tokens too low. If stop_reason is max_tokens, the answer was cut off; raise the limit or ask for shorter output.
  • Hardcoding the API key. Use the environment variable, and rotate any key that was ever committed.
  • Writing your own retry loop. The SDKs already retry rate limits (429) and server errors with backoff.
  • No tests for prompts. Keep 20 to 30 real inputs and rerun them on every prompt change. Our prompt debugging checklist shows what to check.

Where to go next

Once single calls work, the next step is letting Claude use tools, which turns a chatbot into an agent. Read how to build an AI agent with Claude, then MCP explained to connect it to real systems, and how to ship a production AI product for evals, cost control and launch.

If you want to build a complete product with guidance, the live cohort covers all of this over five weeks, and the self-paced course lets you follow the same curriculum on your own schedule.

FAQ

Is the Claude API free?

No. The API is billed per token, separately from a Claude.ai chat subscription. New accounts can check the Claude Console for any available credits.

What is the difference between Claude.ai and the Claude API?

Claude.ai is the chat app for people. The Claude API is for developers who want to call Claude from their own code and build it into a product.

Which programming languages are supported?

Anthropic publishes official SDKs for Python, TypeScript, Java, Go, Ruby, C# and PHP. Any language can also call the HTTP API directly.

What does max_tokens mean?

It is the maximum length of Claude’s reply, in tokens. It does not limit the input. If a reply hits the limit, it is cut off and stop_reason is max_tokens.

Start building your AI product

Five weeks from now you could have a working AI product with real users. Join the next live cohort, or start the self-paced course today.

Join the live cohortStart self-paced today