Prerequisites
- Node.js 18+ installed
- An OpenRouter API key. Create one at orr.api.report.finance/settings/keys or set up a new Stripe project
1. Create a project and install the SDK
Set up a new Node.js project and add the OpenRouter client SDK. The SDK is ESM-only, so set the package type tomodule. Install tsx so you can run the TypeScript examples directly.
2. Send your first message
Createchat.ts with a client instance and a single Responses API request. The apiKey reads from the environment so you never hard-code credentials.
Output
chat.py, and run it:
output_text part of the message item in response.output, and the SDK returns token usage in camelCase fields such as inputTokens and outputTokens. The response.output array follows the same shape as the Responses API response. Without stream: true, client.responses.send is typed to return the full response, so the 'output' in response check is only a runtime safety guard.
3. Stream the response
Streaming returns text as it is generated instead of waiting for the full response. Passstream: true and iterate over the returned event stream. Each response.output_text.delta event contains a delta with the new text fragment.
Output
4. Add multi-turn conversation
Multi-turn works by sending the full conversation history with each request. The model uses all previous turns as context. Append each user input and assistant response to aninput array of { role, content } items before the next call.
Output
turns and exits without reading any input. The second answer shows that the model remembers the first turn, because the full input array is sent with each request. To chat interactively, replace the turns list with lines read from standard input.
5. Swap models
OpenRouter gives you access to hundreds of models through one API. Change themodel string to switch providers, no other code changes needed.
Check your work
npx tsx chat.tsprints a streamed response to the console- A multi-turn conversation maintains context across turns (ask a follow-up that references a previous answer)
- Changing the
modelstring switches to a different provider with no other code changes - The non-streaming response includes a
usageobject withinputTokensandoutputTokens
Next steps
- Connect a coding agent to OpenRouter
- Explore the Agent SDK for built-in multi-turn loops and tool execution