Claude API Node.js / TypeScript Quickstart

Working Node.js code to call the Claude API in 2026. Install @anthropic-ai/sdk, set your API key, and get a response in under 10 lines.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

The official Anthropic JavaScript/TypeScript SDK works in Node.js, Deno, Bun, and Cloudflare Workers.

Installation

npm install @anthropic-ai/sdk

CommonJS (require)

const Anthropic = require("@anthropic-ai/sdk");

const client = new Anthropic(); // reads ANTHROPIC_API_KEY from process.env

async function main() {
  const message = await client.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Hello, Claude. What year is it?" }]
  });
  console.log(message.content[0].text);
}

main();

ESM / TypeScript

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

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain async/await in JavaScript." }]
});

console.log(message.content[0].text);

Key parameters

ParameterTypeNotes
modelstringUse claude-sonnet-4-6 for most tasks. See model comparison.
max_tokensnumberHard cap on output tokens. Set to 512–2048 for typical tasks.
messagesarrayAlternating user/assistant turns.
systemstringSystem prompt, passed separately from messages array.

Reading usage

console.log(message.usage.input_tokens);  // tokens charged for input
console.log(message.usage.output_tokens); // tokens charged for output
console.log(message.stop_reason);         // "end_turn" or "max_tokens"

See the streaming Node.js example for real-time token delivery, or the tool use Node.js example for function calling.

Frequently asked questions

What npm package do I install for the Claude API?
Run `npm install @anthropic-ai/sdk`. The package is `@anthropic-ai/sdk` — not `anthropic` (that's the Python package name). TypeScript types are included.
Does the Node.js SDK support streaming?
Yes. Use `client.messages.stream()` for streaming, or `client.messages.create({ stream: true })`. The streaming helper emits text chunks as they arrive and resolves to the full message when done.
Can I use the SDK in a browser?
The SDK is designed for server-side use (Node.js, Deno, Bun, Cloudflare Workers). In a browser, your API key would be exposed — use a server-side proxy instead.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScriptClaude Prompt Caching in Python