C.W.K.
Stream
Lesson 01 of 06 · published

Install, Types, and the One Client

~14 min · install, types, esm, cjs

Level 0Observer
0 XP0/64 lessons0/13 achievements
0/150 XP to next level150 XP to go0% complete

One package, ESM and CJS

@anthropic-ai/sdk is the official TypeScript SDK. It ships dual builds (ESM and CommonJS), strict types out of the box, and works on Node, Bun, Deno, and Edge runtimes (with caveats). One package; pick by import style.

Strict types are your friend

Every request and response shape is exported. Use Anthropic.MessageParam for message arrays, Anthropic.Message for full responses, Anthropic.TextBlock / Anthropic.ToolUseBlock for content blocks. Strict types catch tool-loop bugs at compile time that would silently break in Python.

One client, async by default

Unlike Python's two-client split, the TypeScript SDK has one async client. Call it from Node, from a Next.js Route Handler, from a worker, anywhere. The constructor reads ANTHROPIC_API_KEY from env automatically; pass it explicitly only when you need per-call key rotation.

Principle: Use the exported types. Hand-typing message shapes is how you ship tool loops with the wrong assistant content list.

Code

Install and verify·bash
npm install @anthropic-ai/sdk
# or: pnpm add @anthropic-ai/sdk / yarn add @anthropic-ai/sdk

# Verify
node -e "console.log(require('@anthropic-ai/sdk/package.json').version)"
node --input-type=module -e "import Anthropic from '@anthropic-ai/sdk'; const c = new Anthropic(); console.log(typeof c.messages.create);"
Typed Messages call (modern import)·typescript
import Anthropic from '@anthropic-ai/sdk';
import type { MessageParam, Message } from '@anthropic-ai/sdk/resources/messages';

const client = new Anthropic();

const messages: MessageParam[] = [
  { role: 'user', content: 'One sentence on why typed SDKs reduce production bugs.' },
];

const response: Message = await client.messages.create({
  model: 'claude-sonnet-4-6',
  max_tokens: 256,
  system: 'Answer in exactly one sentence.',
  messages,
});

const firstText = response.content.find(b => b.type === 'text');
if (firstText && firstText.type === 'text') {
  console.log(firstText.text);
}

External links

Exercise

Add the SDK to a TypeScript project. Write a function that takes a user string and returns a typed string answer, using exported types end-to-end. No any, no as Message.
Hint
If you reach for as, narrow the union instead — that is what the discriminated content array is for.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.