Every call carries a Gridsay key in the x-api-key header. Which key you use decides where the call may come from. You create both kinds in the console: see Keys.
| Key | Starts with | Call from | Also needs |
|---|---|---|---|
| Secret | sk_gs_ | Your server only | Nothing else. Check your own users before you forward their question. |
| Publishable | pk_gs_ | The browser | An allowed origin and the signed-in user's token. |
Keys made earlier start with sk_ayt_ or pk_ayt_ and still work. The key decides the tier that answers; no request field changes it.
Your server forwards the user's question and adds the key. A secret key never appears in browser code.
Origin header must match one of them. Without Origin, the origin of Referer is used. A request with neither, such as curl or most server code, is refused with 403 origin_not_allowed: send an allowed origin as Origin, or forward the browser's, as the quickstart route does.http://localhost and http://127.0.0.1, on any port, are always accepted on a secret key, for development.options.metadata.referenceId names your user in usage reports. It does not authenticate anyone: your server decides who may ask.A publishable key can be called from the page itself. Each call also proves who the user is:
Authorization: Bearer <token>. See JWT and session tokens.// In a page whose origin is on the key's allowed origins.const response = await fetch('https://api.gridsay.com/api/v1/datagrid/prompt', { method: 'POST', credentials: 'omit', headers: { 'Content-Type': 'application/json', 'x-api-key': 'pk_gs_FAKE_EXAMPLE_DO_NOT_USE', Authorization: `Bearer ${await getToken()}`, }, body: JSON.stringify({ query: 'people in Germany or France earning over 100k', context: JSON.stringify([{ field: 'country' }, { field: 'salary', type: 'number' }]), }),});const answer = await response.json();if (!answer.ok) throw new Error(answer.message);The browser sets Origin; your code does not. Cookies are not read: use credentials: 'omit'.
A publishable key answers only pages on its allowed origins, and an empty list allows none.
https://app.example.com. No path, no trailing slash.https://*.example.com allows every subdomain, not https://example.com itself. Scheme and port must match.Origin header; Referer alone is not enough, and there is no localhost exception. Add http://localhost:5173 (your own port) to test locally.The API answers the browser's preflight (OPTIONS) before it reads any key. That is not permission: the POST that follows is checked in full.
| Status | Code | When | Send again unchanged? |
|---|---|---|---|
| 401 | invalid_api_key | The key is missing, malformed, expired, disabled or unknown. | No. Check the key. |
| 403 | origin_not_allowed | The origin is not on the key's list, the publishable key has no list, a publishable call has no Origin, or a call with a secret key that has a list sends neither Origin nor Referer. | No. Fix the origin or its settings. |
| 403 | forbidden | The publishable key has no sign-in method set up. | No. Set up sign-in for the key. |
| 401 | unauthenticated_user | The user's token is missing, cannot be verified, or names no user. | No. Send a valid user token. |
| 429 | rate_limited | More than 20 sign-in checks in a minute for this key and client address, counting checks that failed and checks still in progress. A check that succeeds does not count. | Yes, after the wait. |
| 503 | upstream_unavailable | The sign-in provider could not be reached, or the saved secret could not be read. | Yes, later. Check your sign-in settings if the detail names them. |
None of these is charged. The sentence and the fix for each code are in Errors.