Open History to see the last commit
1---
2title: Send the current user to the server
3id: ui-recipe-request-context
4order: 5
5description: "Pass a user id or tenant from the chat screen to your route with forwardedProps, without putting it in the prompt."
6keywords:
7 - tanstack ai
8 - forwardedProps
9 - useAppChat
10 - runtime context
11 - example
12---
13
14Pass `forwardedProps` to `useAppChat()`. Every request from that instance carries it.
15
16```tsx ignore
17const chat = useAppChat({
18 forwardedProps: { tenantId: 'tenant_456' },
19})
20```
21
22The value never enters the prompt. The model does not see it.
23
24## Client
25
26`forwardedProps` belongs on the screen, not on `chatOptions`, because it changes per user.
27
28```tsx group=request-context
29import { fetchServerSentEvents } from '@tanstack/ai-react'
30import { createChatHook } from '@tanstack/ai-react/ui'
31
32const chatOptions = {
33 connection: fetchServerSentEvents('/api/chat'),
34}
35
36const { useAppChat, useChatContext } = createChatHook({
37 options: chatOptions,
38 components: {
39 input: () => {
40 const chat = useChatContext()
41 return (
42 <button onClick={() => void chat.sendMessage('What are my invoices?')}>
43 Ask
44 </button>
45 )
46 },
47 layout: ({ Messages, Input }) => (
48 <main>
49 <Messages />
50 <Input />
51 </main>
52 ),
53 message: ({ Parts }) => <article><Parts /></article>,
54 },
55 partsComponents: {
56 text: ({ part }) => <p>{part.content}</p>,
57 fallback: () => null,
58 },
59})
60
61export function TenantChat({ tenantId }: { tenantId: string }) {
62 const chat = useAppChat({
63 threadId: `tenant-${tenantId}`,
64 forwardedProps: { tenantId },
65 })
66 return <chat.AppChat />
67}
68```
69
70## Server
71
72Read it with `chatParamsFromRequest`, then map it into `context`. Tools and middleware read `context`.
73
74```tsx
75import {
76 chat,
77 chatParamsFromRequest,
78 toServerSentEventsResponse,
79} from '@tanstack/ai'
80import { openaiText } from '@tanstack/ai-openai'
81
82export async function POST(request: Request) {
83 const params = await chatParamsFromRequest(request)
84
85 const tenantId =
86 typeof params.forwardedProps.tenantId === 'string'
87 ? params.forwardedProps.tenantId
88 : undefined
89 if (!tenantId) return new Response('Missing tenant', { status: 400 })
90
91 const stream = chat({
92 adapter: openaiText('gpt-5.6'),
93 messages: params.messages,
94 context: { tenantId },
95 })
96
97 return toServerSentEventsResponse(stream)
98}
99```
100
101## Validate it. Always.
102
103`forwardedProps` comes from the browser. A user can change it.
104
105Never trust it for identity or authorization. Get the user from your session or token on the server, and use `forwardedProps` only for values that are safe to be wrong:
106
1071. Derive the user id from the request's own auth, not from the client.
1082. Check every forwarded field against a type or a schema.
1093. Reject the request when a required field is missing.
110
111The example above checks `tenantId` is a string, then refuses the request without it. In a real app you would also confirm the authenticated user may use that tenant.
112
113## One chat per user
114
115Pass `threadId` alongside `forwardedProps` when a screen can show more than one chat. Two instances with different thread ids keep separate histories.
116
117## Next
118
119- Typed server dependencies for tools and middleware: [runtime context](../../advanced/runtime-context).
120- Local browser dependencies that stay on the client: [client tools](../../tools/client-tools).