If your server can say who a user is from a value your page already has, Gridsay can ask it. Your page sends that value in a header with each question; Gridsay forwards it, signed, to an endpoint on your server, which answers with the user. This is the Identity webhook method on a publishable key.
Open Auth and limits, choose the publishable key, set Method to Identity webhook, and fill in:
| Field | What to enter |
|---|---|
| Endpoint URL | A public https address on your server |
| Header to forward | The header your page sets, such as authorization or x-user-token, up to 100 characters. Not cookie: the browser sends no cookies to Gridsay |
| Cache answers for (seconds) | How long Gridsay reuses your answer for the same header value: 0 to 3,600, 60 by default. 0 asks your server every time |
Choose Save authentication, then Create secret. Copy the Signing secret: it is shown once. Keep it on your server, never in your page.
When a question arrives with a header value Gridsay has no cached answer for, your endpoint receives:
POST /api/ai-identityContent-Type: application/jsonx-gridsay-signature: t=1796241600,v1=<hex HMAC-SHA256 of "1796241600.<raw body>"> {"credential":"<the value of the header your page sent>"}credential is the header's whole value, exactly as the page sent it. With authorization that includes the word Bearer.
x-gridsay-signature holds a Unix time t and a signature v1: the hex HMAC-SHA256, keyed with your signing secret, of t, a full stop, and the raw request body. Before you trust the request:
GRIDSAY_WEBHOOK_SECRET is set on your server. If it is missing or empty, the endpoint answers 500 before reading the body or computing a signature.t in decimal digits and one v1 of 64 lower-case hex characters. Every comma-separated field must be name=value, with no spaces. The endpoint ignores other well-formed fields. A malformed field, a repeated t or v1, or a timestamp more than five minutes from your clock gets a 401.v1 in constant time. A mismatch gets a 401.The console gives you this endpoint. It looks up the user only after the signature matches:
import { createHmac, timingSafeEqual } from 'node:crypto'; export async function POST(request: Request) { const secret = process.env.GRIDSAY_WEBHOOK_SECRET; if (!secret) { console.error('[Gridsay] Set GRIDSAY_WEBHOOK_SECRET to your key’s webhook signing secret. Until then every identity request is refused.'); return new Response(null, { status: 500 }); } // x-gridsay-signature: t=<unix seconds>,v1=<hex HMAC-SHA256 of "t.body">. Fields added later are ignored. const fields = new Map<string, string>(); for (const field of (request.headers.get('x-gridsay-signature') ?? '').split(',')) { const [, name, value] = /^([\w-]+)=(\S+)$/.exec(field) ?? []; const repeated = (name === 't' || name === 'v1') && fields.has(name); if (!name || !value || repeated) return new Response(null, { status: 401 }); fields.set(name, value); } const t = fields.get('t') ?? ''; const v1 = fields.get('v1') ?? ''; if (!/^\d+$/.test(t) || !/^[0-9a-f]{64}$/.test(v1) || Math.abs(Date.now() / 1000 - Number(t)) > 300) { return new Response(null, { status: 401 }); } const body = await request.text(); const expected = createHmac('sha256', secret).update(`${t}.${body}`).digest(); // v1 is exactly 64 hex characters, so both sides are 32 bytes and the comparison cannot throw. if (!timingSafeEqual(Uint8Array.from(Buffer.from(v1, 'hex')), Uint8Array.from(expected))) { return new Response(null, { status: 401 }); } // The value of the header the page sent, exactly as received. const { credential } = JSON.parse(body); const user = await findUserByCredential(credential); // your own lookup if (!user) return new Response(null, { status: 401 }); return Response.json({ userId: user.id, attributes: { plan: user.plan } });}For a valid credential, answer 200 with the user:
{ "userId": "user_123", "attributes": { "plan": "pro", "ai_quota": 300 }}userId is 1 to 255 characters: the user every limit counts.attributes is required; send {} if you have none. It may be at most 8 KB as JSON. Limit rules match these, such as plan or ai_quota.Your endpoint has three seconds to answer. Anything other than the answers above makes the question fail with 503 upstream_unavailable, and your user sees "The assistant is temporarily unavailable. Try again.":
userId and attributes, attributes over 8 KB included;A question that arrives without the header gets 401 unauthenticated_user. Over 20 rejected credentials in a minute for the key from one client address get 429 rate_limited, "Too many attempts. Try again in N seconds." None of these is charged; they are listed under Refusals on Usage.
A successful answer is reused for the same header value for as long as Cache answers for (seconds) says. A change to a user's access or attributes can take that long to reach Gridsay. A refusal is not cached: the next question asks again. Changing the method's settings or the signing secret starts afresh.
Choose Rotate secret beside Signing secret and confirm. Gridsay signs with the new secret at once, with no overlap, so update your endpoint's secret in the same step.
Your page sets the header with each question. This is the console's page code for MUI X Data Grid, with x-user-token as the header; getSessionToken is yours:
'use client'; import { createPromptResolver } from '@gridsay/mui-x-data-grid';import { DataGridPremium, GridAiAssistantPanel, useGridApiRef,} from '@mui/x-data-grid-premium';import { useMemo } from 'react'; // Inside your client component, using your own rows and columns:const apiRef = useGridApiRef();const onPrompt = useMemo( () => createPromptResolver({ url: 'https://api.gridsay.com/api/v1/datagrid/prompt', publishableKey: 'pk_gs_FAKE_EXAMPLE_DO_NOT_USE', headers: async () => ({ 'x-user-token': await getSessionToken() }), apiRef, }), [apiRef],); <DataGridPremium apiRef={apiRef} rows={rows} columns={columns} aiAssistant onPrompt={onPrompt} slots={{ aiAssistantPanel: GridAiAssistantPanel }} showToolbar/>