BeClaude
GuideBeginnerAPI2026-05-20

Getting Started with the Claude API: A Beginner's Guide to Building with Claude

Learn how to set up, authenticate, and make your first API calls to Claude. Covers SDK installation, Messages API basics, model selection, and key features for developers.

Quick Answer

This guide walks you through setting up the Claude API, making your first request with the Messages API, choosing the right model, and exploring key capabilities like vision, structured outputs, and tool use.

Claude APIMessages APIQuickstartPython SDKDeveloper Guide

Introduction

Anthropic's Claude API gives developers direct access to the latest Claude models—Opus 4.7, Sonnet 4.6, and Haiku 4.5—for building custom AI-powered applications. Whether you're creating a chatbot, a code assistant, a document analyzer, or an autonomous agent, the Claude API provides the flexibility and control you need.

This guide covers everything you need to go from zero to a working Claude integration: setting up your environment, understanding the Messages API, choosing the right model, and exploring Claude's most powerful features.

Prerequisites

Before you start, you'll need:

  • An Anthropic Console account
  • An API key (generated in the Console)
  • Python 3.8+ or Node.js 18+ installed
  • Basic familiarity with REST APIs and JSON

Step 1: Make Your First API Call

Install the SDK

Anthropic provides official SDKs for Python and TypeScript. Install the one for your language:

Python:
pip install anthropic
TypeScript/JavaScript:
npm install @anthropic-ai/sdk

Set Your API Key

Set your API key as an environment variable for security:

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

Send Your First Message

Here's the simplest possible API call—a single-turn conversation where you ask Claude to introduce itself:

Python:
import anthropic

client = anthropic.Anthropic()

message = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, messages=[ {"role": "user", "content": "Hello, Claude!"} ] )

print(message.content[0].text)

TypeScript:
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

async function main() { const message = await client.messages.create({ model: 'claude-sonnet-4-20250514', max_tokens: 1024, messages: [{ role: 'user', content: 'Hello, Claude!' }], });

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

main();

If everything works, you'll see Claude's friendly response printed to your terminal.

Step 2: Understand the Messages API

The Messages API is the core interface for communicating with Claude. Every request follows the same structure:

Request Structure

FieldTypeRequiredDescription
modelstringYesThe model identifier (e.g., claude-sonnet-4-20250514)
messagesarrayYesArray of message objects representing the conversation
max_tokensintegerYesMaximum number of tokens in the response
systemstringNoSystem prompt to set Claude's behavior
temperaturefloatNoSampling temperature (0.0 to 1.0, default 0.7)
stop_sequencesarrayNoCustom strings that stop response generation

Multi-Turn Conversations

To continue a conversation, include the full message history. Each message has a role (either "user" or "assistant") and content:

messages = [
    {"role": "user", "content": "What is the capital of France?"},
    {"role": "assistant", "content": "The capital of France is Paris."},
    {"role": "user", "content": "What is its population?"}
]

response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=256, messages=messages )

System Prompts

System prompts let you set the tone, constraints, and persona for Claude:

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a helpful coding assistant. Always provide code examples in Python.",
    messages=[
        {"role": "user", "content": "How do I read a CSV file?"}
    ]
)

Stop Reasons

Every response includes a stop_reason field that tells you why Claude stopped generating:

  • "end_turn" — Claude finished its response naturally
  • "max_tokens" — The response hit the max_tokens limit
  • "stop_sequence" — A custom stop sequence was encountered
  • "tool_use" — Claude is requesting to use a tool (see Tool Use below)

Step 3: Choose the Right Model

Claude offers three model tiers, each optimized for different use cases:

ModelBest ForSpeedCost
Claude Opus 4.7Complex reasoning, agentic coding, researchSlowestHighest
Claude Sonnet 4.6General coding, agents, enterprise workflowsFastModerate
Claude Haiku 4.5Simple tasks, classification, real-time chatFastestLowest
Recommendation: Start with Sonnet 4.6 for most applications. Switch to Haiku for high-volume, low-latency tasks, and upgrade to Opus when you need maximum reasoning capability.

Step 4: Explore Key Features

Claude's API supports a rich set of features beyond basic text generation:

Extended Thinking

Enable chain-of-thought reasoning for complex problems. Claude shows its internal reasoning process before giving the final answer:

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=2048,
    thinking={"type": "enabled", "budget_tokens": 1024},
    messages=[
        {"role": "user", "content": "Solve this math problem step by step: 23 * 47"}
    ]
)

Structured Outputs

Get Claude to return JSON-formatted responses reliably by specifying a schema:

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Extract the name, age, and city from: John is 28 and lives in Berlin."}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person_info",
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                    "city": {"type": "string"}
                },
                "required": ["name", "age", "city"]
            }
        }
    }
)

Vision

Claude can analyze images and PDFs. Pass image data as base64 or use a URL:

import base64

with open("chart.png", "rb") as f: image_data = base64.b64encode(f.read()).decode("utf-8")

response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, messages=[ { "role": "user", "content": [ {"type": "text", "text": "Describe this chart in detail."}, { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": image_data } } ] } ] )

Tool Use (Function Calling)

Give Claude the ability to call external functions. Define tools with JSON Schema, and Claude will request to use them when needed:

tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location"]
        }
    }
]

response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, tools=tools, messages=[ {"role": "user", "content": "What's the weather in Tokyo?"} ] )

Check if Claude wants to use a tool

if response.stop_reason == "tool_use": for block in response.content: if block.type == "tool_use": print(f"Tool: {block.name}") print(f"Input: {block.input}")

Streaming

Get responses in real-time as Claude generates them, ideal for chat interfaces:

stream = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a short story."}],
    stream=True
)

for event in stream: if event.type == "content_block_delta": print(event.delta.text, end="", flush=True)

Prompt Caching

Reduce latency and cost for repeated system prompts or large context by caching them:

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "You are an expert in Python programming...",
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[
        {"role": "user", "content": "Explain decorators."}
    ]
)

Developer Tools

Anthropic provides several tools to accelerate your development:

  • Developer Console — Prototype and test prompts in your browser with the Workbench and prompt generator.
  • API Reference — Full documentation for the Messages API and client SDKs.
  • Claude Cookbook — Interactive Jupyter notebooks covering PDF processing, embeddings, tool use, and more.

Next Steps

Now that you have a working Claude integration, here's what to explore next:

  • Build a multi-turn chatbot — Implement conversation history management.
  • Add tool use — Connect Claude to your own APIs and databases.
  • Implement streaming — Create a real-time chat interface.
  • Optimize with prompt caching — Reduce costs for high-volume applications.
  • Explore Managed Agents — For long-running, asynchronous tasks, check out Claude Managed Agents.

Key Takeaways

  • The Claude API uses the Messages API with a simple request/response structure. Every call requires a model, messages array, and max_tokens.
  • Choose your model based on task complexity: Haiku for speed and cost, Sonnet for general use, Opus for advanced reasoning.
  • Key features like extended thinking, structured outputs, vision, and tool use are available directly through the API with minimal configuration.
  • Use streaming for real-time applications and prompt caching to reduce latency and cost on repeated content.
  • Start with the Python or TypeScript SDK, prototype in the Developer Console, and refer to the Cookbook for ready-to-run examples.