Skip to content
Source
Sign inSign up
TanStack/ai

Add TanStack Start server code example for chat API #141

Mergedharlley(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/141/headmerged by AlemTuzlak View on GitHub

Showing 1 changed file with 57 additions and 2 deletions.

x
0 of 1 viewed, 1 left
57 additions and 2 deletionsdocs/getting-started/quick-start.mdModified
View file
@@ -20,10 +20,65 @@
​
First, create an API route that handles chat requests. Here's a simplified example:
​
+### TanStack Start
+​
```typescript
-// app/api/chat/route.ts (Next.js)
-// or src/routes/api/chat.ts (TanStack Start)
import { chat, toStreamResponse } from "@tanstack/ai";
+import { openai } from "@tanstack/ai-openai";
+import { createFileRoute } from "@tanstack/react-router";
+​
+export const Route = createFileRoute("/api/chat")({
+ server: {
+ handlers: {
+ POST: async ({ request }) => {
+ // Check for API key
+ if (!process.env.OPENAI_API_KEY) {
+ return new Response(
+ JSON.stringify({
+ error: "OPENAI_API_KEY not configured",
+ }),
+ {
+ status: 500,
+ headers: { "Content-Type": "application/json" },