Claude APIAnthropicTutorialBeginners

How to Use the Claude API: Complete Beginner's Guide (2026)

Step-by-step guide to getting started with the Anthropic API. Send your first message to Claude in under 10 minutes — no experience required.

February 28, 2026·12 min read
Advertisement

The Anthropic API lets you build applications powered by Claude. Customer support bots, document analyzers, coding assistants, automated research pipelines, content generation tools — if the task involves reading, reasoning, or writing, Claude's API can handle it. And unlike building on top of a closed black-box service, Anthropic's API is well-documented, developer-friendly, and gives you access to some of the most capable AI models available today.

This guide takes you from zero to your first working API integration in under 10 minutes. No AI experience required. By the end, you'll understand the core concepts well enough to start building something real.


Key Takeaways

  • You can send your first API call to Claude in under 10 minutes with Python or Node.js
  • Claude Sonnet provides the best quality-to-cost ratio for most applications
  • API pricing is per token: a typical request costs less than $0.01 with Sonnet
  • System prompts are the primary mechanism for customizing Claude's behavior for your use case
  • Multi-turn conversation requires passing the full message history with each request
  • Always use environment variables for API keys — never hardcode them

Prerequisites

  • A computer with Python 3.8+ or Node.js 16+ installed
  • An Anthropic account (free to create)
  • A payment method for API usage (Anthropic offers free credits to new accounts)

No prior AI or machine learning experience required. If you can write a basic script in Python or JavaScript, you can use the Anthropic API.


Step 1: Create an Anthropic Account

Go to console.anthropic.com and create a free account. After verifying your email, you'll have access to the Anthropic Console — the dashboard where you manage API keys, monitor usage, and review billing.

New accounts receive free API credits to get started. No credit card is required for the initial signup.


Step 2: Get Your API Key

  1. In the Console, click API Keys in the left sidebar
  2. Click Create Key
  3. Name it something memorable (e.g., "my-first-project")
  4. Copy the key immediately — you will not be able to see it again after closing this screen

Security rule: Never share your API key or commit it to a GitHub repository. Anyone with your API key can make calls charged to your account. Store it as an environment variable, not as a string in your code.


Step 3: Install the SDK

Python:

pip install anthropic

Node.js:

npm install @anthropic-ai/sdk

Both SDKs are officially maintained by Anthropic and follow the same conceptual structure. Examples in this guide use Python, but Node.js equivalents are provided for key sections.


Step 4: Send Your First Message

Python:

import anthropic

client = anthropic.Anthropic(api_key="YOUR_API_KEY")

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain machine learning in one paragraph."}
    ]
)

print(message.content[0].text)

Node.js:

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

const client = new Anthropic({ apiKey: "YOUR_API_KEY" });

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Explain machine learning in one paragraph." }
  ],
});

console.log(message.content[0].text);

Run this and you'll get a response from Claude in 1-3 seconds. That's your first API call.


Understanding the API Response

The API returns a Message object with this structure:

{
  "id": "msg_01abc...",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Machine learning is a branch of artificial intelligence..."
    }
  ],
  "model": "claude-sonnet-4-6",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 15,
    "output_tokens": 120
  }
}

Key fields to understand:

FieldWhat it means
content[0].textThe actual response text — this is what you display to users
stop_reasonWhy Claude stopped: end_turn (natural end), max_tokens (hit your limit), stop_sequence
usage.input_tokensTokens consumed by your prompt — affects your cost
usage.output_tokensTokens in Claude's response — also affects your cost
idUnique identifier for this message — useful for logging

The text you want is at message.content[0].text.


Choosing the Right Model

Anthropic offers several Claude models in 2026, each with different speed, capability, and cost tradeoffs:

ModelSpeedIntelligenceCost (per 1M tokens in/out)Best for
claude-haiku-4-5FastestGood$0.80 / $4.00High-volume simple tasks, classification
claude-sonnet-4-6BalancedExcellent$3.00 / $15.00Most use cases — the default choice
claude-opus-4-6SlowestBest$15.00 / $75.00Complex reasoning, high-stakes outputs

Decision guide:

  • Start with claude-sonnet-4-6 for everything. It handles 95% of use cases well.
  • Switch to claude-haiku-4-5 if you're making thousands of calls and cost is a constraint (customer support classification, document tagging, etc.)
  • Only use claude-opus-4-6 for tasks where quality genuinely matters more than speed or cost (contract analysis, complex code generation, research synthesis)

A typical sonnet-4-6 API call processes around 500 input tokens and 300 output tokens, costing approximately $0.006 — less than a cent per call.


System Prompts: Customizing Claude's Behavior

System prompts are the most powerful mechanism for shaping Claude's behavior for your specific use case. They define Claude's role, constraints, tone, and knowledge for the entire conversation.

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a customer support agent for Acme Software. Your job is to help users troubleshoot technical issues. Be concise, professional, and empathetic. Only answer questions related to Acme Software products. If a question is outside your scope, say so clearly and offer to escalate.",
    messages=[
        {"role": "user", "content": "How do I reset my password?"}
    ]
)

System prompts are how you turn Claude into a specialized assistant. Examples:

  • A legal research assistant that cites sources and flags uncertainty
  • A customer support bot that only answers questions about your product
  • A writing editor that enforces your brand's style guide
  • A code reviewer that checks for security vulnerabilities specifically

A well-written system prompt does 80% of the work of prompt engineering. Invest time here before optimizing elsewhere.


Multi-Turn Conversations

Claude does not maintain conversation history between API calls. To have a back-and-forth conversation, you pass the full message history with each request:

messages = []

# Turn 1
messages.append({"role": "user", "content": "What is the capital of France?"})
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=messages
)
print(response.content[0].text)  # "Paris"

# Add Claude's response to history
messages.append({"role": "assistant", "content": response.content[0].text})

# Turn 2 — Claude remembers the context
messages.append({"role": "user", "content": "What is the population of that city?"})
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=messages
)
print(response.content[0].text)  # Responds with Paris's population

This pattern — append user message, call API, append assistant response, repeat — is the foundation of every Claude-powered chatbot.

Important: Message history accumulates tokens. A 20-turn conversation may have 5,000+ tokens of history. Monitor token usage in production to avoid unexpected costs on long conversations.


Streaming Responses

For real user-facing applications, streaming provides a much better experience. Instead of waiting for the full response, text appears word by word — like watching Claude type in real time.

Python:

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short story about a robot learning to cook."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Node.js:

const stream = await client.messages.stream({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a short story about a robot learning to cook." }],
});

for await (const chunk of stream) {
  if (chunk.type === "content_block_delta") {
    process.stdout.write(chunk.delta.text);
  }
}

Streaming is a one-line change and makes a significant difference in perceived responsiveness for end users. Use it whenever you're building a user-facing interface.


API Pricing (2026)

ModelInput (per 1M tokens)Output (per 1M tokens)
Claude Haiku 4.5$0.80$4.00
Claude Sonnet 4.6$3.00$15.00
Claude Opus 4.6$15.00$75.00

Practical cost estimates:

  • A 500-word document summarization (Sonnet): ~$0.005
  • A customer support response (Sonnet): ~$0.003
  • Processing 1,000 customer emails per day (Haiku): ~$1.50/day
  • A complex contract analysis (Opus): ~$0.20 per document

For most early-stage applications, API costs are negligible compared to development time. Cost optimization becomes relevant at scale (100,000+ calls/month).

For a comparison of Claude's API value against competitors, see our Claude vs ChatGPT vs Gemini breakdown.


Best Practices

1. Use environment variables for API keys

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

Then in your code:

import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))

Never hardcode API keys. Use .env files locally (with python-dotenv) and secrets management in production (AWS Secrets Manager, Heroku Config Vars, etc.).

2. Handle errors gracefully

import anthropic

try:
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}]
    )
except anthropic.APIConnectionError:
    print("Network error — check your connection")
except anthropic.RateLimitError:
    print("Rate limit hit — implement exponential backoff")
except anthropic.APIStatusError as e:
    print(f"API error {e.status_code}: {e.message}")

For production systems, implement exponential backoff on rate limit errors and log all API errors with their request IDs for debugging.

3. Set appropriate max_tokens

Don't set max_tokens to an arbitrarily large value. It doesn't cost you money unless Claude generates those tokens, but it signals to the model that long responses are acceptable.

Use caseRecommended max_tokens
Classification / simple Q&A64-256
Customer support response256-512
Document summarization512-1024
Code generation1024-4096
Long-form writing2048-8192

4. Write strong system prompts

The quality of your system prompt is the primary determinant of output quality. A good system prompt:

  • States Claude's role clearly ("You are...")
  • Specifies the audience and tone
  • Lists explicit constraints ("Only answer questions about X")
  • Describes the output format expected
  • Handles edge cases ("If you don't know, say so")

5. Monitor token usage in production

Track usage.input_tokens and usage.output_tokens per request. Store them in your database. This data helps you optimize costs, detect runaway conversations, and understand usage patterns.


Common Use Cases with Example Patterns

Document Summarization

def summarize_document(text: str) -> str:
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=512,
        system="You are a document summarizer. Create concise summaries that capture the key points, decisions, and action items. Format as bullet points.",
        messages=[{"role": "user", "content": f"Summarize this document:\n\n{text}"}]
    )
    return response.content[0].text

Customer Support Classification

def classify_ticket(ticket_text: str) -> str:
    response = client.messages.create(
        model="claude-haiku-4-5",  # Haiku for high-volume, low-complexity task
        max_tokens=64,
        system="Classify the customer support ticket into exactly one category: billing, technical, account, feature-request, or other. Respond with only the category name.",
        messages=[{"role": "user", "content": ticket_text}]
    )
    return response.content[0].text.strip()

Content Generation with Constraints

def generate_product_description(product_name: str, features: list) -> str:
    features_text = "\n".join(f"- {f}" for f in features)
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=300,
        system="You write product descriptions for an e-commerce site. Descriptions should be 2-3 sentences, benefit-focused (not feature-focused), and end with a clear value statement.",
        messages=[{"role": "user", "content": f"Product: {product_name}\nFeatures:\n{features_text}"}]
    )
    return response.content[0].text

Next Steps

Once you've sent your first message, the next concepts to explore are:

  • Tool use (function calling) — Give Claude access to your own functions and APIs. Claude can decide when to call them and incorporate the results into its response. Official docs on tool use
  • Prompt caching — For prompts with large shared context (a long system prompt, a large document), caching can reduce latency and cost by up to 90%.
  • Batch API — For high-volume offline processing, the Batch API processes large numbers of requests at 50% reduced cost.
  • MCP integration — For agentic applications, connect Claude to external tools via the Model Context Protocol. See our best MCP servers guide.
  • Claude Code — For software development use cases, Claude Code provides an agentic interface built on top of the same models. See our Claude Code vs Cursor comparison.

The API is the starting point for anything you want to build with Claude. Once you've sent your first message, the rest is iteration — improving your system prompts, handling edge cases, and scaling up to real users.


FAQ

Do I need a credit card to start using the API? Anthropic allows account creation without a credit card. You receive free credits to get started. Once those credits are exhausted, you'll need to add a payment method to continue making API calls.

What's the difference between the API and Claude.ai? Claude.ai is the consumer product — a chat interface with a subscription model. The API gives you programmatic access to the same models so you can build your own applications. The API is billed per token; Claude.ai is a flat monthly subscription.

How do I stay under my API budget? Set usage limits in the Anthropic Console under Billing > Usage Limits. You can set a monthly spend cap that will pause API access if exceeded. Monitor your usage dashboard regularly.

Can I fine-tune Claude on my own data? As of early 2026, Anthropic does not offer fine-tuning for most Claude models. The standard approach is prompt engineering with system prompts and few-shot examples. For highly specialized tasks, Claude's base capabilities with good prompting outperform fine-tuned smaller models.

How do I handle Claude refusing to answer certain requests? Claude has built-in safety behaviors that may decline certain requests. For legitimate business use cases being declined, the best approach is to provide more context in your system prompt about your use case and audience. If you need to adjust safety settings for a legitimate application, Anthropic's enterprise plans offer configurable safety controls.

What's the maximum context length? Claude Sonnet and Opus support a 200K token context window — approximately 150,000 words or 500 pages of text. This is enough to process entire books, large codebases, or thousands of customer emails in a single call.

Advertisement

Ready to try Claude?

Free to use. No credit card required.

Try Claude Free →