I've been building with the Claude API for a while now and it's one of the cleaner AI APIs to work with. Here's a practical intro to get you from zero to something useful.
Installation
npm install @anthropic-ai/sdk
Set your API key as an environment variable:
ANTHROPIC_API_KEY=your_key_here
Your first message
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [
{ role: "user", content: "Explain what a closure is in JavaScript" }
]
});
console.log(response.content[0].text);
That's it. response.content[0].text is your answer.
System prompts
The system prompt is where you give the model its instructions and persona. It sits outside the conversation and shapes how the model behaves throughout.
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
system: "You are a concise technical assistant. Answer in plain English, no jargon.",
messages: [
{ role: "user", content: "What is a load balancer?" }
]
});
A good system prompt is the single biggest lever for improving output quality. Be specific about what you want and don't want.
Multi-turn conversations
To maintain a conversation, keep appending messages to the array:
const messages = [
{ role: "user", content: "What is React?" }
];
const first = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages
});
messages.push({ role: "assistant", content: first.content[0].text });
messages.push({ role: "user", content: "How does it compare to Vue?" });
const second = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages
});
The model has no memory between calls — you're responsible for passing the history back each time.
Tool use
Tools let the model call functions you define. You pass a list of tools with the request; if the model wants to use one, it returns a tool call instead of text.
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
tools: [{
name: "get_weather",
description: "Get the current weather for a city",
input_schema: {
type: "object",
properties: {
city: { type: "string" }
},
required: ["city"]
}
}],
messages: [{ role: "user", content: "What's the weather in London?" }]
});
if (response.stop_reason === "tool_use") {
const toolCall = response.content.find(b => b.type === "tool_use");
// execute the tool, then feed the result back in a new message
}
This is the basis of building agents — I've written more about that here.
Key parameters to know
model— which Claude model to use (more on that here)max_tokens— maximum length of the responsetemperature— randomness, 0 to 1. Lower is more deterministic, useful for structured taskssystem— the system prompt
The Anthropic docs are worth a read once you've got the basics down — the API is well documented.