Anthropic's Claude models are a strong choice for building AI features — coding, analysis, agents, and long-context work. This quickstart gets you from zero to a working integration with the official SDK.
Install the SDK
npm install @anthropic-ai/sdkYour First Message
Set ANTHROPIC_API_KEY in your environment, then call the Messages API. claude-opus-4-8 is the most capable model; claude-haiku-4-5 is the fastest and cheapest.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY from the environment
const response = await client.messages.create({
model: "claude-opus-4-8",
max_tokens: 1024,
messages: [{ role: "user", content: "Explain RAG in two sentences." }],
});
for (const block of response.content) {
if (block.type === "text") console.log(block.text);
}Stream the Response
For chat UIs, stream tokens as they're generated so the reply appears in real time:
const stream = client.messages.stream({
model: "claude-opus-4-8",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
});
for await (const event of stream) {
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
process.stdout.write(event.delta.text);
}
}Choosing a Model
claude-opus-4-8— most capable, for complex reasoning and agentic work.claude-sonnet-4-6— the best balance of speed and intelligence.claude-haiku-4-5— fastest and most cost-effective for simple, high-volume tasks.
Keep Your Key Server-Side
Never ship your API key to the browser. Call Claude from your backend (a route handler or server action) and expose only your own endpoint to the client.
Next Steps
From here, add tool use to let Claude call your functions, system prompts to set behavior, and prompt caching to cut costs on repeated context.
