Send your first message and stream a response with the OpenRouter SDK
Goal: Learn the fundamentals of OpenRouter by building a TypeScript chat
app that sends messages and streams responses through OpenRouter.Outcome: A working multi-turn conversation loop that can talk to any of the
600+ models available on the platform by changing a single string.
Want to get started faster? Copy this prompt into your coding agent.
Set up a new Node.js project and add the OpenRouter client SDK. The SDK is
ESM-only, so set the package type to module. Install tsx so you can run the
TypeScript examples directly.
mkdir openrouter-chat && cd openrouter-chatnpm init -ynpm pkg set type=modulenpm install @openrouter/sdknpm install --save-dev tsx
Create chat.ts with a client instance and a single chat completion request.
The apiKey reads from the environment so you never hard-code credentials.
import { OpenRouter } from '@openrouter/sdk';const client = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY,});const completion = await client.chat.send({ chatRequest: { model: 'google/gemini-3.1-flash-lite', messages: [ { role: 'user', content: 'Say hello in one sentence.' }, ], },});console.log(completion.choices[0]?.message.content);console.log({ promptTokens: completion.usage?.promptTokens, completionTokens: completion.usage?.completionTokens,});
Run it with your API key:
OPENROUTER_API_KEY=sk-or-v1-... npx tsx chat.ts
You should see a single text response printed to the console. The SDK returns
token usage in camelCase fields such as promptTokens and
completionTokens. The
completion.choices array follows the same shape as the
Chat Completions response.
Streaming returns text as it is generated instead of waiting for the full
response. Pass stream: true and iterate over the returned async iterable.
Each chunk contains a delta with the new text fragment.
import { OpenRouter } from '@openrouter/sdk';const client = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY,});const stream = await client.chat.send({ chatRequest: { model: 'google/gemini-3.1-flash-lite', messages: [ { role: 'user', content: 'Explain how routers work in three sentences.' }, ], stream: true, },});for await (const chunk of stream) { const delta = chunk.choices[0]?.delta?.content; if (delta) process.stdout.write(delta);}console.log();
Text now prints incrementally. See the Streaming reference
for the full SSE event format.
Multi-turn works by sending the full message history with each request. The
model uses all previous messages as context. Append each user input and
assistant response to a messages array before the next call.
OpenRouter gives you access to hundreds of models through one API. Change the
model string to switch providers — no other code changes needed.
// Use OpenAI's latest chat modelmodel: 'openai/gpt-chat-latest',// Use Anthropic Claude Sonnet latestmodel: '~anthropic/claude-sonnet-latest',// Use a free modelmodel: 'openrouter/free',