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
- Go to console.anthropic.com
- Create an account (email + password, or Google)
- Add payment information (you won't be charged until you use the API)
- Navigate to API Keys → Create Key
- 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
Node.js / TypeScript:
npm install @anthropic-ai/sdk
# or
bun add @anthropic-ai/sdk
Step 3: Set your API key
macOS/Linux (add to ~/.zshrc or ~/.bashrc for persistence):
export ANTHROPIC_API_KEY="sk-ant-..."
Windows (Command Prompt):
set ANTHROPIC_API_KEY=sk-ant-...
Python project (.env file):
ANTHROPIC_API_KEY=sk-ant-...
Then load with python-dotenv:
from dotenv import load_dotenv
load_dotenv()
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."
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);
Run it:
python first_call.py
# or
npx ts-node first_call.ts
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
]
)
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
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.*
Top comments (0)