BeClaude
Guide2026-04-25

Getting Started with Claude: Your First API Call and Beyond

Learn how to set up your Anthropic account, get an API key, and make your first API call to Claude using Python, TypeScript, and cURL. Includes practical code examples and next steps.

Quick Answer

This guide walks you through creating an Anthropic Console account, generating an API key, and making your first API call to Claude using cURL, Python, TypeScript, or Java. You'll also learn about the Messages API and key next steps like models, features, and SDKs.

Claude APIQuickstartPythonTypeScriptAnthropic Console

Introduction

Claude is Anthropic's powerful AI assistant, accessible via a simple API. Whether you're building a chatbot, automating workflows, or experimenting with AI, getting started with Claude is straightforward. This guide covers everything you need to make your first API call and understand the core patterns you'll use in every integration.

By the end, you'll have a working API call and a clear path to explore Claude's advanced capabilities like tools, context management, and structured outputs.

Prerequisites

Before you start, you'll need two things:

  • An Anthropic Console account – Sign up at console.anthropic.com.
  • An API key – Generate one in the Console under the API Keys section.
Note: Keep your API key secure. Never share it or commit it to version control.

Making Your First API Call

Claude's API uses the Messages API, which accepts a list of messages (user, assistant, system) and returns a response. Below are examples in the most common languages.

cURL

curl https://api.anthropic.com/v1/messages \
  --header "x-api-key: $ANTHROPIC_API_KEY" \
  --header "anthropic-version: 2023-06-01" \
  --header "content-type: application/json" \
  --data '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, Claude!"}
    ]
  }'

Python

import anthropic

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

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({ apiKey: 'YOUR_API_KEY', });

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();

Java

import com.anthropic.AnthropicClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;

public class Quickstart { public static void main(String[] args) { AnthropicClient client = new AnthropicClient("YOUR_API_KEY");

MessageCreateParams params = MessageCreateParams.builder() .model("claude-sonnet-4-20250514") .maxTokens(1024) .addUserMessage("Hello, Claude!") .build();

Message message = client.messages().create(params); System.out.println(message.content().get(0).text()); } }

Understanding the Response

When you make a successful API call, Claude returns a JSON object like this:

{
  "id": "msg_01ABC123...",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! How can I assist you today?"
    }
  ],
  "model": "claude-sonnet-4-20250514",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 10
  }
}

Key fields to note:

  • content: An array of content blocks (text, tool_use, etc.).
  • stop_reason: Why the model stopped (e.g., "end_turn", "max_tokens", "tool_use").
  • usage: Token counts for billing and monitoring.

Next Steps: Mastering the Messages API

Now that you've made your first call, it's time to explore the core patterns you'll use in every integration:

Multi-turn Conversations

To have a back-and-forth conversation, include the assistant's previous response in the messages array:

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is the capital of France?"},
        {"role": "assistant", "content": "The capital of France is Paris."},
        {"role": "user", "content": "Tell me more about its history."}
    ]
)

System Prompts

Set the behavior and personality of Claude using a system prompt:

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a helpful assistant that speaks like a pirate.",
    messages=[
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

Handling Stop Reasons

The stop_reason field tells you why Claude stopped generating. Common values:

  • "end_turn": Claude finished naturally.
  • "max_tokens": The response hit the token limit.
  • "tool_use": Claude wants to call a tool (used in function calling).
  • "stop_sequence": A custom stop sequence was encountered.

Exploring Advanced Features

Once you're comfortable with the basics, dive into Claude's advanced capabilities:

Models Overview

Compare Claude models by capability and cost:

  • Claude Sonnet 4: Best balance of speed and intelligence.
  • Claude Haiku 3.5: Fast and affordable for simple tasks.
  • Claude Opus 4: Most powerful for complex reasoning.

Tools (Function Calling)

Claude can call external tools and APIs. Learn how to define tools and handle tool calls in the Tools documentation.

Context Management

Claude supports large context windows (up to 200K tokens). Use prompt caching and compaction to optimize costs and performance.

Structured Outputs

Get Claude to return JSON or other structured formats reliably using the structured_outputs feature.

Streaming

For real-time applications, stream responses token by token:

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a story."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Client SDKs

Anthropic provides official SDKs for:

  • Python: pip install anthropic
  • TypeScript/JavaScript: npm install @anthropic-ai/sdk
  • Java: Available via Maven/Gradle
These SDKs handle authentication, retries, and streaming automatically.

Troubleshooting Tips

  • 401 Unauthorized: Check your API key.
  • Rate limiting: Implement exponential backoff.
  • Token limits: Reduce max_tokens or shorten input.
  • Model not found: Ensure you're using a valid model name (e.g., claude-sonnet-4-20250514).

Key Takeaways

  • Get an API key from the Anthropic Console to authenticate your requests.
  • Use the Messages API for all interactions – it supports multi-turn, system prompts, and streaming.
  • Start with a simple call in your preferred language (Python, TypeScript, cURL, Java) and verify the response.
  • Explore advanced features like tools, context management, and structured outputs to build powerful applications.
  • Leverage official SDKs to simplify development and handle edge cases automatically.