# Node OpenAI streaming chat

An OpenAI chat page streams answers from `gpt-5-mini`, renders safe Markdown, and keeps follow up questions grounded in the current thread. Tokay hosts it as one Web Service and keeps your OpenAI API key out of the browser.

This is a compact example of the Responses API in a real interface. The browser owns the visible conversation, while the server owns provider access and streaming.

## Facts

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

## What it does

The page opens with a trip planning prompt ready to send or rewrite.

OpenAI's answer streams into the conversation and formats lists, links, tables, and code. Your next message includes the earlier exchange, so you can ask for a revision without starting over.

Every answer keeps its elapsed time and token totals beside it. Cached input appears there when the API reports it, and **New chat** resets the 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 OpenAI API key

Create a key on the [OpenAI API keys page](https://platform.openai.com/api-keys). On the Service page, open **Environment** and find `OPENAI_API_KEY` under **From your code**. Choose **Set shared value**, confirm **Secret** visibility, and store the key.

Continue through the deployment tracker when the OpenAI key says **Good to go**. Choose **Go Live** when needed, then use the Service's live URL to start chatting.

## Try it

1. Send the starting prompt and watch the text arrive before the full answer is complete.
2. Ask for one change to the plan and check that the response uses the prior details.
3. Start a new chat and confirm the old messages no longer travel with your request.

## How it works

You see a continuous answer because the Express server turns Responses API events into an SSE stream for the browser. Each turn carries the full conversation plus a stable cache key for eligible prompt prefix reuse.

The server reserves room for reasoning and visible output, then lets OpenAI enforce the model's context boundary.

The page sanitizes rendered Markdown with DOMPurify. The API key remains in the server environment, and the app does not save the conversation.

## Secrets

| Name | Visibility | Required | Purpose |
| --- | --- | --- | --- |
| `OPENAI_API_KEY` | Secret | Yes | Authenticates requests to OpenAI. |

## Grow from here

Try the [AI research queue](/examples/python-fastapi-postgres-ai-research-queue) when a model request needs to become a saved job that survives restarts.

## Source files

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

## Download and source

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