DEV Community

Sangmin Lee
Sangmin Lee

Posted on • Originally published at claudeguide.io

Claude API for Beginners: Your First API Call in 10 Minutes

Originally published at claudeguide.io/claude-api-beginners-guide

Claude API for Beginners: Your First API Call in 10 Minutes

The Claude API lets you build applications with Claude — chatbots, coding assistants, content tools, data extractors, and agents. Getting started takes 10 minutes: create an Anthropic account, get an API key, install the SDK, and send your first message. This guide walks through each step with complete code.


Step 1: Get an API key

  1. Go to console.anthropic.com
  2. Create an account (email + password, or Google)
  3. Add payment information (you won't be charged until you use the API)
  4. Navigate to API KeysCreate Key
  5. Copy the key immediately — you won't see it again

API key format: sk-ant-api03-... (starts with sk-ant-)

Security rule: never commit your API key to git. Never share it. Store it as an environment variable.


Step 2: Install the SDK

Python:

pip install anthropic
# or
pip install anthropic[bedrock,vertex]  # if using AWS or GCP
Enter fullscreen mode Exit fullscreen mode

Node.js / TypeScript:

npm install @anthropic-ai/sdk
# or
bun add @anthropic-ai/sdk
Enter fullscreen mode Exit fullscreen mode

Step 3: Set your API key

macOS/Linux (add to ~/.zshrc or ~/.bashrc for persistence):

export ANTHROPIC_API_KEY="sk-ant-..."
Enter fullscreen mode Exit fullscreen mode

Windows (Command Prompt):

set ANTHROPIC_API_KEY=sk-ant-...
Enter fullscreen mode Exit fullscreen mode

Python project (.env file):

ANTHROPIC_API_KEY=sk-ant-...
Enter fullscreen mode Exit fullscreen mode

Then load with python-dotenv:

from dotenv import load_dotenv
load_dotenv()
Enter fullscreen mode Exit fullscreen mode

Never put the key directly in your code — it's easy to accidentally commit to git.


Step 4: Your first API call

Python:

import anthropic

client = anthropic.Anthropic()
# The client automatically reads ANTHROPIC_API_KEY from your environment

message = client.messages.create(
    model="claude-haiku-4-5",  # Cheapest model for learning
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude! What's 2 + 2?"}
    ]
)

print(message.content[0].text)
# Output: "2 + 2 equals 4."
Enter fullscreen mode Exit fullscreen mode

TypeScript:

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

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Hello, Claude! What's 2 + 2?" }
  ],
});

console.log(message.content[0].text);
Enter fullscreen mode Exit fullscreen mode

Run it:

python first_call.py
# or
npx ts-node first_call.ts
Enter fullscreen mode Exit fullscreen mode

You should see Claude's response printed. If you see an AuthenticationError, your API key isn't set correctly.


Understanding the request structure

Every API call has the same structure:

client.messages.create(
    model="...",       # Which Claude model to use
    max_tokens=1024,   # Maximum length of Claude's response
    system="...",      # Optional: instructions for Claude's behavior (system prompt)
    messages=[         # The conversation history
        {"role": "user", "content": "..."},       # User messages
        {"role": "assistant", "content": "..."},  # Claude's previous responses
        {"role": "user", "content": "..."},       # Another user message
    ]
)
Enter fullscreen mode Exit fullscreen mode

model: which Claude to use. Start with claude-haiku-4-5 (cheapest, fastest).
max_tokens: maximum tokens in Claude's response. 1024 = ~750 words. Set higher for long responses.
system: tells Claude its role and behavior. Optional but powerful.
messages: the conversation. Always starts and ends with a user message.


The response structure

message = client.messages.create(...)

print(message.content[0].text)   # The response text
print(message.model)              # Which model was used
print(message.stop_reason)        # Why Claude stopped: "end_turn" or "max_tokens"
print(message.usage.input_tokens) # Tokens in your request
print(message.usage.output_tokens) # Tokens in Claude's response
Enter fullscreen mode Exit fullscreen mode

Multi-turn conversations (chat)

To have a back-and-forth conversation, pass the full history each time:


python
import anthropic

client = anthropic.Anthropic()

conversation = []

def chat(user_message: str) -

[→ Get the Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-api-beginners-guide)

*30-day money-back guarantee. Instant download.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)