Skip to Content
API ReferenceAuthorization

Authorization

Authentication proves who a request is. Authorization decides what that request can reach. This page is the request-level reference: given a credential and a query, what does the API return, and why. For the conceptual model behind it — tenancy, owner planes, visibility, and grants — see Authorization & Access.

Two things carry authorization on every request:

  • the API key, which binds the request to a project (or an allow-listed set of projects) and carries the scopes that bound what it may do;
  • an optional user token, which identifies the signed-in end user, so user-owned data resolves to that person.

The key answers may this request perform this operation at all. The database answers which rows it may touch. Both must pass.

An operation’s endpoint carries neither: it calls Foir with the dispatch token it was handed, which holds the operation’s declared grants and the user who ran it. See the three credential classes.

How a request resolves

Every request is checked in this order. The first failing gate ends the request:

  1. Authentication: the x-api-key must be a valid, active key. A request that sends no key is served on a signed-in person’s token alone when it carries one that verifies, and that token is then its whole authority. A bad key, or a request with neither a key nor a verifying token, is UNAUTHENTICATED.
  2. Project binding — a key serves the project it was created in, and nothing the request carries can move it.
  3. Organization binding: a key confined to an organization reads that organization, every organization nested under it and the project-shared tier, and writes only into its own organization. A project key stands in none unless it names one with X-Org-Id, which must be an active organization of its own project; a confined key may only name its own.
  4. Scope — the operation must be permitted by the caller’s scopes. For an admin key those are the key’s own. For a publishable or a server key they are what the key gives, the list its row keeps for whoever comes through it (a key’s two lists), with the signed-in user’s role scopes beside it. Nothing is worked out from what such a key itself holds, so a key with an empty list gives a visitor nothing. A missing scope is missing required scope. There is no separate key-polarity gate: a visitor’s write through a publishable key is refused because the scope is not in that union, not because of the key’s prefix.
  5. Row rules — for the rows the query actually touches, the database applies owner plane, organization containment, visibility, the user identity from the JWT, whose rows the caller’s grants reach, and any grants. Rows you can’t see are simply not returned, and a targeted read of one resolves to null.

Gates 1–4 are about the call; gate 5 is about the rows. A key can be fully authorized to run a query and still legitimately get back fewer rows than exist — that’s gate 5 doing its job, not an error.

What each credential can reach

CredentialReaches
Publishable key aloneWhat the key gives a visitor. For a read it gives: published records whose visibility is public. Safe in a browser.
Publishable key + user tokenThe above, plus the authenticated user’s own records (currentUser, and on a user-owned model my<Model> for a singleton or my<Models> for a collection) and anything explicitly granted to them. currentUser needs no grant. The self-service reads need the model’s read grant in the caller’s authority, which the signed-in user’s role can give, so the key need not give it.
Dispatch token (an operation’s endpoint)What the operation declared in its grants, confined to the user who ran it when one did. A grant with no reach is for the project’s own records, or that user’s; every user’s take the grant written .all (records:read.all:<model>).
Server key, alone or with a user tokenWhat a publishable key reaches, from the server that renders your pages. Never shipped to a browser.
Admin key (a machine of your own)What its grants say. The project’s own records and files, the ones no person owns, take project:stand beside the grant; a user’s take the grant written .all (records:read.all, records:write.all). Held by an integration, a CI job or your back-office tooling, never by your app and never shipped to a browser. Nobody signs in through it. In a project with organizations, a key standing in no organization reaches the project-shared tier only; see what a credential with no organization reads.
A preview environment’s own publishable key + drafts:readWhat a publishable key reaches, plus unpublished draft content via preview: true and the draft channel.

The pattern to internalize: a publishable key is the anonymous, published, public view, and a server key is the same view from the server that renders your pages; a user token adds that user’s private data on top; an operation’s dispatch token is what the operation declared; an admin key is a machine’s view within its scopes. You widen access by presenting a stronger credential, never by changing the query. Which credential is your app’s is on App architecture.

# a publishable key alone → published, public only query PublicCatalog { products(first: 20) { edges { node { _id title } } } }

The same read through the data client:

const products = await foir.product.findMany({ first: 20 });

Nothing in that call names the credential either. The client was created with the key, so the same five gates run against it — and a client generated from a publishable key has no create or update on foir.product at all, because the manifest it was generated from is a projection of what that key may do. That is a compile error rather than a runtime 403; the server still checks independently.

# a publishable key + user Bearer token → the same catalog, plus this user's orders, # resolved to them by the database, not by a filter in the query. query MyAccount { currentUser { id email } orders(first: 20) { edges { node { _id total status } } } }

This one does not survive the trip whole. The data client sends one operation per request and reads one root field back, so a two-root query becomes two calls:

// The user's bearer token is on the client, not in the query. const foir = createFoirClient({ apiUrl, apiKey, generated, bearerToken: () => getUserToken(session), }); const me = await foir.ns.auth.currentUser(); const orders = await foir.order.findMany({ first: 20 });

Neither call names the user. The token is on the client, so the row rules resolve the orders to whoever it belongs to — and currentUser resolves to the same person for the same reason.

Authenticate with the auth SDK and hand it the token as bearerToken; foir.ns.auth.currentUser() is then the user’s own account, and foir.ns.auth.whoami() is the credential the request resolved to, which is a different question and a useful one when a read returns less than you expected.

What you pay is the round trip: what GraphQL fetches in one request the data client fetches in two. See Choosing an API Style.

Scopes gate the call, not the rows

A scope decides whether a request may perform an operation; it never widens which rows that operation returns. An admin key with records:read reads the rows of a user-owned model that no person owns, and every owner’s with records:read.all, while the same read made with a publishable key and a user token returns only that user’s rows. Same query, same model; the credential changes what the row layer resolves.

This is why you can hand out records:read broadly without it becoming a data-exposure decision: the scope opens the door to the model, and ownership plus visibility still decide what’s on the other side. Per-model scopes (records:read:<model>, records:write:<model>, …) narrow the door further for tokens that should only reach specific models — see scopes.

Writes

A write needs the matching write scope (records:write, records:delete, records:publish) in the caller’s scopes: a signed-in user’s role on your publishable key, an operation’s declared grants, or a machine’s admin key. A visitor who is not signed in gets no write unless you named one for anonymous callers (how). Beyond the scope, the row rules still apply: a write only lands on rows the credential is allowed to modify.

Your app’s own logic writes from an operation, never from a key of its own: see App architecture.

Users write their own data through the generated self-service mutation setMy<Model>, which a user-owned singleton model has. It needs the model’s write grant, which the user’s role can give, so the key needs no write scope of its own. Ownership makes the target unambiguous: a user’s write can only ever address their own record. See User-owned data.

Preview and drafts

By default reads return published content. Pass preview: true to read the latest working version instead. Preview is gated by the drafts:read scope, which belongs on a preview environment’s own key and never on the key your production app ships, so draft content reaches only the environment you gave that key to.

A preview: true read made without drafts:read fails loudly with an authorization error rather than silently returning published data, so a misconfigured preview deploy breaks visibly instead of serving stale content. Preview responses also come back Cache-Control: no-store. See Preview Mode Access.

Rows you can’t see resolve to null

When a targeted read (by id or natural key) names a record your credential can’t see, the field resolves to null — the same answer as a record that genuinely doesn’t exist. The API deliberately does not distinguish “doesn’t exist” from “exists but not yours,” so existence itself isn’t leaked to a caller who lacks access. The documented case is a record that is unpublished and read without preview: true: it comes back null, not a permission error.

There is no errors entry for either case. A read that returns null succeeded; see a missing record is not an error.

Contrast this with the call-level failures (UNAUTHENTICATED, PERMISSION_DENIED, missing required scope), which are about the key and are reported plainly because they carry no information about any specific row. The rule of thumb:

  • Wrong or under-scoped key → a permission/auth error. Fix the credential.
  • Right key, row you can’t see → null. The credential is fine; that row isn’t yours.

Caching honors the credential

The response cache never serves one caller’s private data to another. Entries computed for a user are keyed PRIVATE, which folds the user id into the cache key, so each user gets isolated entries; only public, published responses are cached PUBLIC and shared across callers whose query, variables, schema channel, and scopes match. Authorization and caching can’t disagree, because the cache scope is derived from what the request was allowed to see.

Authorization error reference

ConditionHTTPextensions.codeMessage
Missing / invalid / disabled key401UNAUTHENTICATEDAPI key or a signed-in person's token required / Invalid or expired API key
An admin key past its expiry401UNAUTHENTICATEDThis API key expired on <date>. Renew it, or make another (https://docs.foir.dev/credentials)
A field the credential’s schema doesn’t hold, such as a write through a publishable key that gives no write200(none)Cannot query field "<field>" on type "Mutation".
A user’s token that doesn’t verify, sent beside a key200(no error)served as nobody, and extensions.auth says why
A server key or an admin key sent from a browser403PERMISSION_DENIEDThis key cannot be used from a browser: ...
A user’s token sent beside an admin key403PERMISSION_DENIEDNobody signs in through an admin key: ...
An admin key made for a workspace itself403PERMISSION_DENIEDThe refusal says the data API serves projects
Key valid but lacks a required scope200PERMISSION_DENIEDmissing required scope: <scope>
X-Org-Id is not an active organization of the key’s project404NOT_FOUNDorganization not found
Organization-confined key, X-Org-Id names another403PERMISSION_DENIEDX-Org-Id names an organization other than the one this credential is confined to
X-Org-Url is under no active organization’s address of the key’s project404NOT_FOUNDorganization not found
X-Org-Id and X-Org-Url name different organizations400INVALID_ARGUMENTX-Org-Id and X-Org-Url name different organizations
Targeted read of a row you can’t see (incl. unpublished without preview)200(no error)field resolves to null

Two of these come back 200. A credential is served a schema of only what its grants reach, so a field it can’t use isn’t there, and naming it fails validation before anything runs. That error has no extensions.code: compare the two keys’ grants (Cannot query field). A scope refusal refuses one field, not the request, so the response is 200 with that field null and the error coded PERMISSION_DENIED.

Reasoning about a query

To predict what a request returns, read it credential-first, not query-first:

  1. What credential? A publishable or a server key → published + public only. A dispatch token → the operation’s declared grants. An admin key → a machine’s view within its scopes.
  2. User token present? If so, that user’s owned/granted rows are added on top.
  3. What scopes? The operation must be in them, or it’s a call-level rejection.
  4. What owner plane and visibility do the touched models have? That, plus the identity above, decides the rows.

If a query returns fewer rows than you expected, it’s almost always step 4 (the credential can’t see them), not a bug in the query. Present a stronger credential (a user token, or, for your own back-office tooling, an admin key); don’t try to widen the query.

See also

Last updated on