If your app has its own sessions and no tokens to share, your server asks Gridsay for a short-lived token for the signed-in user, with a secret key. Your page sends that token with each question. This is the Session tokens method on a publishable key.
A token works on every publishable key of your organization that uses session tokens.
Your server sends:
POST /api/v1/sessionsContent-Type: application/jsonx-api-key: <your secret key, on your server only> { "userId": "user_123", "attributes": { "plan": "pro", "ai_quota": 300 }, "ttlSeconds": 3600}userId, required: 1 to 255 characters. A stable id from your own session: it is the user every limit counts.attributes, optional: an object, at most 8 KB as JSON. Limit rules match these, such as plan or ai_quota.ttlSeconds, optional: how long the token lasts, 60 to 86,400 seconds; 3,600 by default.The whole body may be at most 16 KB. The answer, with status 200 and Cache-Control: no-store:
{ "ok": true, "data": { "token": "<a signed token, valid only for your organization>", "expiresAt": "2026-12-02T21:00:00.000Z" }}This route checks your sign-in first and answers 401 if the user is signed out. It takes the id and attributes from your session. Set GRIDSAY_SECRET_KEY and GRIDSAY_URL on your server. If either is missing or empty, the route answers 500 before calling Gridsay.
import { NextResponse } from 'next/server'; export async function POST() { const user = await getSignedInUser(); // your own sign-in if (!user) return NextResponse.json({ error: 'Not signed in' }, { status: 401 }); const key = process.env.GRIDSAY_SECRET_KEY; // a secret key const gateway = process.env.GRIDSAY_URL; if (!key || !gateway) { console.error('[Gridsay] Set GRIDSAY_SECRET_KEY and GRIDSAY_URL on your server, then restart it.'); return NextResponse.json({ error: 'Not configured' }, { status: 500 }); } const response = await fetch(`${gateway}/api/v1/sessions`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': key, }, body: JSON.stringify({ userId: user.id, attributes: { plan: user.plan }, // optional; your limit rules can match on these ttlSeconds: 3600, // 60 to 86,400; default 3,600 }), // Never follow a redirect: it would send your key to another address. redirect: 'error', }); // On success: { ok: true, data: { token, expiresAt } } return NextResponse.json(await response.json(), { status: response.status });}In the console’s version of the route, the Gridsay URL is already filled in.
A token is valid until its expiresAt, with 60 seconds' allowance for clock differences. It cannot be revoked: signing a user out of your app does not end a token already issued. Choose a lifetime that suits how quickly you need a change of plan or access to take effect, since the attributes in the token stay as they were when it was issued.
You may keep a token in the page and reuse it until shortly before expiresAt. The console's page code below asks your route for a new token before every question, which is simpler but makes one more call per question.
Each question carries the publishable key in x-api-key and the token as Authorization: Bearer followed by the token. This is the console's page code for MUI X Data Grid with this method:
'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', // Your end user's token, sent as Authorization: Bearer. getToken: () => fetch('/api/ai-session', { method: 'POST' }) .then((response) => (response.ok ? response.json() : null)) .then((body) => body?.data?.token), apiRef, }), [apiRef],); <DataGridPremium apiRef={apiRef} rows={rows} columns={columns} aiAssistant onPrompt={onPrompt} slots={{ aiAssistantPanel: GridAiAssistantPanel }} showToolbar/>| Status and code | When |
|---|---|
403 forbidden | The token call was sent with a publishable key. It needs a secret key |
403 origin_not_allowed | The token call was sent with a secret key that has allowed origins, and no matching Origin or Referer |
400 invalid_request | The token call's body is not valid JSON, breaks a rule above, or is over 16 KB |
401 unauthenticated_user | A question came with no token, an expired one, or one issued for another organization. Your user sees "Sign in to use the assistant." |
A refused question is not charged, and is listed under Refusals on Usage.