The official TypeScript SDK is the npm package @anthropic-ai/sdk, installed with npm install @anthropic-ai/sdk. It runs on Node.js 20 LTS and newer, plus Deno 1.28.0+, Bun 1.0+, Cloudflare Workers, Vercel Edge Runtime, Jest 28+ (node environment), and Nitro 2.6+ — which covers essentially every server and edge runtime an enterprise web team is likely to deploy on. Browsers are deliberately disabled by default, because a client that runs in the browser would expose your API key; we cover the correct browser pattern in streaming with Server-Sent Events.
The non-streaming baseline, verbatim from the SDK's documentation, is:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env['ANTHROPIC_API_KEY'],
});
const message = await client.messages.create({
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-sonnet-5',
});
That call resolves once with the complete message. Streaming replaces the single resolution with a flow of events you consume incrementally.
Two consumption styles
The TypeScript SDK gives you two idiomatic ways to consume a stream, and they map onto two different programming temperaments.
Async iteration (for await ... of). A streaming request returns an object that implements JavaScript's async-iterator protocol. You loop over it with for await, and each iteration hands you the next event from the wire — text deltas as the model writes, block boundaries, and the closing metadata. This style keeps everything in one linear function: easy to read, easy to wrap in try/catch, and a natural fit when you are forwarding fragments into an HTTP response.
Event listeners (stream() with .on('text')). The SDK's higher-level stream() helper additionally exposes listener-style hooks, so you can register a callback for exactly the events you care about — most commonly the text deltas — while the helper handles everything else, including accumulating the full message in the background. This style suits UI code where different components react to different events, and it pairs with a finalizer that resolves to the complete assembled message when the stream ends.
The exact method names, event types, and TypeScript types for both styles are documented in the TypeScript SDK repository; rather than paraphrase its API surface here, treat the README as the source of truth for signatures, and this article as the map of which style to reach for.
for await when one function owns the whole stream (server route handlers, pipelines); use stream() with .on('text') listeners when several parts of your code need to observe the same stream, or when you want the SDK to assemble the final message for you.What stays the same across platforms
Everything above targets the first-party Claude API. If your organization runs Claude through a third-party platform, the story is: streaming is available on all four platforms, and on Claude Platform on AWS the wire format is the same SSE stream as the first-party API — not AWS EventStream — so your event-handling code does not change shape. For Claude Platform on AWS specifically there is a dedicated TypeScript package: install @anthropic-ai/aws-sdk and construct the client with import AnthropicAws from "@anthropic-ai/aws-sdk"; const client = new AnthropicAws(). It signs requests with AWS SigV4 and sets the workspace header for you; note that these platform-specific SDK clients are in beta. For Bedrock, Vertex AI, and Foundry, the TypeScript SDK's own README does not document platform client classes — check each platform's official documentation for the recommended integration path rather than assuming a class name.
Practical notes for production
Type safety is the quiet win here: because events are discriminated by type, TypeScript can narrow each event in a switch and catch at compile time the case where you treated a metadata event as text. Beyond that, three habits pay off. Handle mid-stream errors explicitly — a stream can die after you have emitted half an answer to the user, so decide up front whether you retry silently or surface a "regenerating" state. Always consume or explicitly abort streams; an abandoned stream holds a connection open. And log the final assembled message, not your concatenated fragments, so your records include stop reason and token usage.
Where to go next
Read streaming basics for the protocol underneath, streaming helpers for accumulators and finalizers, or the quickstart to get credentials set up.