# Node OpenRouter configurable model chat

A streaming chat page lets you hold a conversation with any model available through OpenRouter and change that model without editing the app. Tokay runs it as one Web Service while keeping the API key private and the model choice visible as Config.

This separation is useful when you want to compare providers or let a team choose the model without giving everyone access to its credential. The server reads both values at runtime and refuses to guess when either one is missing.

## Facts

- Runtime: Node.js
- Framework: Express
- Service types: Web Service
- Resources: None
- Required secrets: `OPENROUTER_API_KEY`

## What it does

The page starts with a trip planning prompt that you can replace with any question.

Send a message and the answer appears as it streams. Follow up in the same thread and the model receives the earlier conversation for context.

Each answer shows its response time and token use. When OpenRouter reports cached input, the same line shows how many tokens were reused. Press **New chat** whenever you want a clean thread.

## Deploy it

Bring this folder to Tokay any way you like. Paste or upload it in the dashboard, push it with git, or hand it to your AI agent along with [our agent instructions](https://app.tokay.io/llms.txt). Tokay figures out the rest.

Working in Claude, ChatGPT, VS Code, or another MCP client? Connect the [Tokay MCP server](https://tokay.io/docs/platform-mcp) and ask your agent to deploy this example. It signs in through your browser, so there is no token to paste.

New to Tokay? The [getting started guide](https://tokay.io/getting-started) walks you through your first deploy.

## Add your OpenRouter key and model

1. Create a key on the [OpenRouter keys page](https://openrouter.ai/settings/keys).
2. Choose a model from the [OpenRouter model list](https://openrouter.ai/models) and copy its full slug, such as `provider/model-name`.
3. On the Service page, open **Environment** and find `OPENROUTER_API_KEY` under **From your code**. Choose **Set shared value**, keep **Visibility** on **Secret**, and save the key.
4. Find `OPENROUTER_MODEL` and choose **Set shared value**. Change **Visibility** to **Config**, paste the model slug, and save it.

Finish the deployment tracker after both OpenRouter rows say **Good to go**. Choose **Go Live** if Tokay presents it. The live URL at the top of the Service opens the chat.

## Try it

1. Send the starting question, then ask a follow up that depends on the first answer.
2. Change `OPENROUTER_MODEL` in Project Config and restart the Service.
3. Start a new chat. The status line names the new model without exposing your API key.

## How it works

You can switch models because the browser never calls a provider directly. It sends the full conversation to the Express server, which selects `OPENROUTER_MODEL` and streams the provider's answer back over SSE.

At startup, the server asks OpenRouter for the chosen model's context length. A stable session ID keeps the conversation with the same provider and gives eligible prompt caching a consistent key.

Your API key stays on the server and is never written into the conversation or application data.

## Secrets

| Name | Visibility | Required | Purpose |
| --- | --- | --- | --- |
| `OPENROUTER_API_KEY` | Secret | Yes | Authenticates requests to OpenRouter. |
| `OPENROUTER_MODEL` | Config | Yes | Selects the OpenRouter model slug used for every request. |

## Grow from here

Try the [AI research queue](/examples/python-fastapi-postgres-ai-research-queue) when model work should continue after the browser closes.

## Source files

- [package.json](https://tokay.io/examples/src/node-openrouter-chat/package.json)
- [src/server.ts](https://tokay.io/examples/src/node-openrouter-chat/src/server.ts)
- [tsconfig.json](https://tokay.io/examples/src/node-openrouter-chat/tsconfig.json)

## Download and source

- [Download the complete project](https://tokay.io/examples/downloads/node-openrouter-chat.zip)
- Last updated: 2026-07-31
- Example revision: `52c7b7f`
- [View this revision on GitHub](https://github.com/tokayio/tokay-examples/tree/52c7b7ffd52e157375b08f1f11ff4fbe6c498642/node-openrouter-chat)
