Open History to see the last commit
1---
2title: Quick Start
3id: quick-start
4order: 2
5description: "Add a streaming TanStack AI chat to your app. Pick a framework, install, stream from a server route, and render with the matching hook."
6keywords:
7 - tanstack ai
8 - quick start
9 - useChat
10 - injectChat
11 - createChat
12 - streaming chat
13 - openai
14 - react
15 - vue
16 - solid
17 - svelte
18 - preact
19 - angular
20 - octane
21 - remix
22redirect_from:
23 - /getting-started/quick-start-vue
24 - /getting-started/quick-start-svelte
25 - /getting-started/quick-start-angular
26 - /getting-started/quick-start-octane
27---
28
29You want a streaming chat in your app. TanStack AI streams from a server route. The hook for your framework renders the tokens.
30
31For a React + Start app with BYOK, open [Basic Chat](../tutorials/basic-chat).
32
33For an MCP server with a tool, a resource, and a prompt, open [Build an MCP Server](../tutorials/mcp-server).
34
35For an image from a prompt, open [Generate Image](../tutorials/generate-image).
36
37For a table that fills as JSON streams, open [Streaming Structured Table](../tutorials/streaming-structured-table).
38
39For a blog desk where a researcher and an SEO agent run together and a writer uses their notes, open [Subagents](../tutorials/subagents).
40
41> [!TIP]
42> If you do not want a key per provider, [OpenRouter](../adapters/openrouter) gives you 300+ models with one API key.
43
44React Native or Expo needs an absolute server URL and an XHR transport. See [Quick Start: React Native](./quick-start-react-native).
45
46No UI: see [Quick Start: Server Only](./quick-start-server).
47
48## 1. Install
49
50<!-- ::start:tabs variant="package-manager" mode="install" -->
51
52react: @tanstack/ai @tanstack/ai-react @tanstack/ai-openai
53vue: @tanstack/ai @tanstack/ai-vue @tanstack/ai-openai
54solid: @tanstack/ai @tanstack/ai-solid @tanstack/ai-openai
55svelte: @tanstack/ai @tanstack/ai-svelte @tanstack/ai-openai
56preact: @tanstack/ai @tanstack/ai-preact @tanstack/ai-openai
57angular: @tanstack/ai @tanstack/ai-angular @tanstack/ai-openai
58vanilla: @tanstack/ai @tanstack/ai-client @tanstack/ai-openai
59octane: @tanstack/ai @tanstack/ai-octane @tanstack/ai-openai octane
60remix: @tanstack/ai @tanstack/ai-remix @tanstack/ai-openai remix
61
62<!-- ::end:tabs -->
63
64## 2. Stream from the server
65
66Call `chat()`. Then wrap the result with `toServerSentEventsResponse`.
67
68```typescript
69import {
70 chat,
71 chatParamsFromRequest,
72 toServerSentEventsResponse,
73} from "@tanstack/ai";
74import { openaiText } from "@tanstack/ai-openai";
75
76export async function POST(request: Request) {
77 const { messages, threadId, runId } = await chatParamsFromRequest(request);
78
79 const stream = chat({
80 adapter: openaiText("gpt-5.6"),
81 messages,
82 threadId,
83 runId,
84 });
85
86 return toServerSentEventsResponse(stream);
87}
88```
89
90This works with TanStack Start, Next.js, SvelteKit, Hono, and any host that returns a Web `Response`.
91
92A Remix controller action can return this same `Response`.
93
94If your server is Node streams (Express), see [Quick Start: Server Only](./quick-start-server).
95
96Put the API key on the server:
97
98```bash
99OPENAI_API_KEY=your-openai-api-key
100```
101
102The adapter reads `OPENAI_API_KEY` at runtime. Do not send this key to the browser.
103
104If you do not want a server key, see [Bring Your Own Key](../advanced/byok).
105
106## 3. Render the chat
107
108<!-- ::start:framework -->
109
110# React
111
112Call `useChat` from `@tanstack/ai-react`. Hold the composer text in `useState`. Pass it to `sendMessage`.
113
114```tsx
115import { useState } from "react";
116import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
117
118export function Chat() {
119 const [input, setInput] = useState("");
120 const { messages, sendMessage, isLoading, stop } = useChat({
121 connection: fetchServerSentEvents("/api/chat"),
122 });
123
124 return (
125 <>
126 {messages.map((message) => (
127 <div key={message.id}>
128 {message.parts.map((part, index) =>
129 part.type === "text" ? <p key={index}>{part.content}</p> : null,
130 )}
131 </div>
132 ))}
133 <form
134 onSubmit={(event) => {
135 event.preventDefault();
136 if (input.trim() === "") {
137 return;
138 }
139 sendMessage(input);
140 setInput("");
141 }}
142 >
143 <input
144 value={input}
145 onChange={(event) => setInput(event.target.value)}
146 />
147 {isLoading ? (
148 <button type="button" onClick={stop}>
149 Stop
150 </button>
151 ) : (
152 <button type="submit">Send</button>
153 )}
154 </form>
155 </>
156 );
157}
158```
159
160`messages` updates as chunks arrive. `isLoading` is `true` while the run is in flight.
161
162See the [React API](../api/ai-react).
163
164# Vue
165
166Call `useChat` from `@tanstack/ai-vue`. If you are in `<script setup>`, read refs with `.value`. The template unwraps them.
167
168```vue
169<script setup lang="ts">
170import { ref } from 'vue'
171import { useChat, fetchServerSentEvents } from '@tanstack/ai-vue'
172
173const input = ref('')
174
175const { messages, sendMessage, isLoading, stop } = useChat({
176 connection: fetchServerSentEvents('/api/chat'),
177})
178
179function handleSubmit() {
180 if (input.value.trim() && !isLoading.value) {
181 sendMessage(input.value)
182 input.value = ''
183 }
184}
185</script>
186
187<template>
188 <div>
189 <div v-for="message in messages" :key="message.id">
190 <template v-for="(part, index) in message.parts" :key="index">
191 <p v-if="part.type === 'text'">{{ part.content }}</p>
192 </template>
193 </div>
194 <form @submit.prevent="handleSubmit">
195 <input v-model="input" :disabled="isLoading" />
196 <button v-if="isLoading" type="button" @click="stop">Stop</button>
197 <button v-else type="submit" :disabled="!input.trim()">Send</button>
198 </form>
199 </div>
200</template>
201```
202
203The composable stops in-flight requests when the component unmounts.
204
205See the [Vue API](../api/ai-vue).
206
207# Solid
208
209Call `useChat` from `@tanstack/ai-solid`. `messages` and `isLoading` are accessors. Call them as functions.
210
211```tsx ignore
212import { For, createSignal } from "solid-js";
213import { useChat, fetchServerSentEvents } from "@tanstack/ai-solid";
214
215export function Chat() {
216 const [input, setInput] = createSignal("");
217 const { messages, sendMessage, isLoading, stop } = useChat({
218 connection: fetchServerSentEvents("/api/chat"),
219 });
220
221 return (
222 <>
223 <For each={messages()}>
224 {(message) => (
225 <div>
226 <For each={message.parts}>
227 {(part) =>
228 part.type === "text" ? <p>{part.content}</p> : null
229 }
230 </For>
231 </div>
232 )}
233 </For>
234 <form
235 onSubmit={(event) => {
236 event.preventDefault();
237 if (input().trim() === "") {
238 return;
239 }
240 sendMessage(input());
241 setInput("");
242 }}
243 >
244 <input
245 value={input()}
246 onInput={(event) => setInput(event.currentTarget.value)}
247 />
248 {isLoading() ? (
249 <button type="button" onClick={stop}>
250 Stop
251 </button>
252 ) : (
253 <button type="submit">Send</button>
254 )}
255 </form>
256 </>
257 );
258}
259```
260
261See the [Solid API](../api/ai-solid).
262
263# Svelte
264
265Call `createChat` from `@tanstack/ai-svelte`. The return object uses reactive getters. Read `chat.messages` and `chat.isLoading` with no extra wrapper.
266
267```svelte
268<script lang="ts">
269import { createChat, fetchServerSentEvents } from '@tanstack/ai-svelte'
270
271let input = $state('')
272
273const chat = createChat({
274 connection: fetchServerSentEvents('/api/chat'),
275})
276
277function handleSubmit() {
278 if (input.trim() && !chat.isLoading) {
279 chat.sendMessage(input)
280 input = ''
281 }
282}
283</script>
284
285<div>
286 {#each chat.messages as message (message.id)}
287 <div>
288 {#each message.parts as part}
289 {#if part.type === 'text'}
290 <p>{part.content}</p>
291 {/if}
292 {/each}
293 </div>
294 {/each}
295
296 <form onsubmit={handleSubmit}>
297 <input bind:value={input} disabled={chat.isLoading} />
298 {#if chat.isLoading}
299 <button type="button" onclick={() => chat.stop()}>Stop</button>
300 {:else}
301 <button type="submit" disabled={!input.trim()}>Send</button>
302 {/if}
303 </form>
304</div>
305```
306
307If the component can unmount while a response streams, call `chat.stop()` in `onDestroy`.
308
309See the [Svelte API](../api/ai-svelte).
310
311# Preact
312
313Call `useChat` from `@tanstack/ai-preact`. Hold the composer text with `useState` from `preact/hooks`.
314
315```tsx ignore
316import { useState } from "preact/hooks";
317import { useChat, fetchServerSentEvents } from "@tanstack/ai-preact";
318
319export function Chat() {
320 const [input, setInput] = useState("");
321 const { messages, sendMessage, isLoading, stop } = useChat({
322 connection: fetchServerSentEvents("/api/chat"),
323 });
324
325 return (
326 <>
327 {messages.map((message) => (
328 <div key={message.id}>
329 {message.parts.map((part, index) =>
330 part.type === "text" ? <p key={index}>{part.content}</p> : null,
331 )}
332 </div>
333 ))}
334 <form
335 onSubmit={(event) => {
336 event.preventDefault();
337 if (input.trim() === "") {
338 return;
339 }
340 sendMessage(input);
341 setInput("");
342 }}
343 >
344 <input
345 value={input}
346 onInput={(event) => setInput(event.currentTarget.value)}
347 />
348 {isLoading ? (
349 <button type="button" onClick={stop}>
350 Stop
351 </button>
352 ) : (
353 <button type="submit">Send</button>
354 )}
355 </form>
356 </>
357 );
358}
359```
360
361See the [Preact API](../api/ai-preact).
362
363# Angular
364
365Call `injectChat` in a field initializer. State is a `Signal`. Read it by calling the function.
366
367```typescript ignore
368import { Component } from "@angular/core";
369import { FormsModule } from "@angular/forms";
370import { injectChat, fetchServerSentEvents } from "@tanstack/ai-angular";
371
372@Component({
373 selector: "app-chat",
374 standalone: true,
375 imports: [FormsModule],
376 template: `
377 <div>
378 @for (message of chat.messages(); track message.id) {
379 <div>
380 @for (part of message.parts; track $index) {
381 @if (part.type === "text") {
382 <p>{{ part.content }}</p>
383 }
384 }
385 </div>
386 }
387 <form (submit)="send($event)">
388 <input [(ngModel)]="draft" name="draft" [disabled]="chat.isLoading()" />
389 @if (chat.isLoading()) {
390 <button type="button" (click)="chat.stop()">Stop</button>
391 } @else {
392 <button type="submit" [disabled]="!draft.trim()">Send</button>
393 }
394 </form>
395 </div>
396 `,
397})
398export class ChatComponent {
399 chat = injectChat({
400 connection: fetchServerSentEvents("/api/chat"),
401 });
402
403 draft = "";
404
405 send(event: Event) {
406 event.preventDefault();
407 const text = this.draft.trim();
408 if (text && !this.chat.isLoading()) {
409 void this.chat.sendMessage(text);
410 this.draft = "";
411 }
412 }
413}
414```
415
416Call `injectChat` in a field initializer or the constructor. A call in `ngOnInit` throws.
417
418`injectChat` subscribes to `DestroyRef`. In-flight requests stop when the component is destroyed.
419
420See the [Angular API](../api/ai-angular).
421
422# Octane
423
424Call `useChat` from `@tanstack/ai-octane`. Hold the composer text in `useState` from `octane`. Octane text controls fire `onInput`.
425
426`@tanstack/ai-octane` publishes uncompiled `.tsrx` source. Add `octane/compiler/vite` (or the rspack / rspeedy equivalent) to the app build.
427
428```tsx ignore
429import { useState } from "octane";
430import { useChat, fetchServerSentEvents } from "@tanstack/ai-octane";
431
432export function Chat() {
433 const [input, setInput] = useState("");
434 const { messages, sendMessage, isLoading, stop } = useChat({
435 connection: fetchServerSentEvents("/api/chat"),
436 });
437
438 return (
439 <div>
440 {messages.map((message) => (
441 <div key={message.id}>
442 <p>
443 {message.parts
444 .filter((part) => part.type === "text")
445 .map((part) => part.content)
446 .join("")}
447 </p>
448 </div>
449 ))}
450 <form
451 onSubmit={(event) => {
452 event.preventDefault();
453 if (input.trim() === "") {
454 return;
455 }
456 void sendMessage(input);
457 setInput("");
458 }}
459 >
460 <input
461 value={input}
462 disabled={isLoading}
463 onInput={(event) => setInput(event.currentTarget.value)}
464 />
465 {isLoading ? (
466 <button type="button" onClick={stop}>
467 Stop
468 </button>
469 ) : (
470 <button type="submit">Send</button>
471 )}
472 </form>
473 </div>
474 );
475}
476```
477
478The hook calls `attach()` on mount. It calls `detach()` and `dispose()` on unmount.
479
480See the [Octane API](../api/ai-octane).
481
482# Remix
483
484Call `createChat(handle, options)` from `@tanstack/ai-remix` inside a `clientEntry` island. Put `connection` and `tools` in setup. They are not serializable `clientEntry` props.
485
486`@tanstack/ai-remix` publishes uncompiled source. Remix compiles JSX through `jsxImportSource` `remix/ui`. Bind the form with the Remix `on` mixin.
487
488```tsx ignore
489import { createChat, fetchServerSentEvents } from "@tanstack/ai-remix";
490import { clientEntry, on, type Handle } from "remix/ui";
491
492export const Chat = clientEntry(
493 import.meta.url,
494 function Chat(handle: Handle) {
495 const chat = createChat(handle, {
496 connection: fetchServerSentEvents("/api/chat"),
497 });
498
499 return () => (
500 <div>
501 {chat.messages.map((message) => (
502 <div key={message.id}>
503 {message.parts.map((part, index) =>
504 part.type === "text" ? <p key={index}>{part.content}</p> : null,
505 )}
506 </div>
507 ))}
508 <form
509 mix={on("submit", (event) => {
510 event.preventDefault();
511 const form = event.currentTarget;
512 const text = String(
513 new FormData(form).get("message") ?? "",
514 ).trim();
515 if (text === "") {
516 return;
517 }
518 form.reset();
519 void chat.sendMessage(text);
520 })}
521 >
522 <input name="message" disabled={chat.isLoading} />
523 {chat.isLoading ? (
524 <button type="button" mix={on("click", () => chat.stop())}>
525 Stop
526 </button>
527 ) : (
528 <button type="submit">Send</button>
529 )}
530 </form>
531 </div>
532 );
533 },
534);
535```
536
537Read `chat.messages` and `chat.isLoading` in the render function so each paint sees the latest values.
538
539See the [Remix API](../api/ai-remix). For a typed headless chat UI, see [Remix Chat UI](../ui/remix).
540
541<!-- ::end:framework -->
542
543Send a message. Tokens show up in the UI.
544
545## Later
546
547- [Tools](../tools/tools) for function calling
548- [Streaming](../chat/streaming) for cancel, callbacks, and transports
549- [Adapters](../adapters/openai) for other providers