App architecture
A Foir app has no server of its own standing between it and Foir. Your app calls Foir directly, and the logic that is yours runs in an operation, which Foir dispatches to an endpoint you host. This page is the rule; every other page that mentions a credential follows it.
Two actors reach a user’s data
Exactly two things call the data API for a user.
1. The user, as themselves. Your app (a browser, a mobile app, anything) holds a publishable key (foir_pub_). When someone is signed in, it adds that user’s own bearer, from Foir auth or hosted sign-in. Every read and write is gated by the user’s role, and the user-owned rows they reach are their own.
2. An operation, under its declared grants. When your app needs logic of its own (a checkout, a booking, a call to another system), the user runs an operation. Foir dispatches it to your endpoint with a dispatch token in X-Foir-Token. The token carries the grants the operation declared, and the identity of the user who triggered the run, for attribution, row confinement and audit. The endpoint builds its Foir client from that token and holds no key.
1. The user, as themselves
Your app ── its key + the user's own bearer ──► Foir data API
gated by the user's role
2. An operation, under its declared grants
Your app ── its key (+ bearer): run the operation ─► Foir
│
│ dispatches, signed.
│ X-Foir-Token carries the
│ declared grants and
│ the user who ran it
▼
Your operation endpoint
(verifies the dispatch, holds no key)
│
│ client built from the token
▼
Foir data API
gated by the operation's grantsApp logic may live anywhere, including a route in the same server that renders your pages: a server route is a fine operation endpoint. What matters is how it is reached. Your frontend reaches it only by running the operation, and it calls Foir only with the dispatch token it is handed. See Writing an Operation Endpoint.
A server that renders your pages is fine too. Rendering with the user’s own bearer, from a session your server holds, is the user acting as themselves (First-Party Sessions). That server holds a server key (foir_srv_): it gives a visitor what your publishable key gives, and it holds visitors:forward so it can name the visitor each call is for (A server rendering your pages). It never becomes a proxy for app logic, and it is not the pattern below: its key gives no more than a visitor may have, and it names no user to act as.
The pattern Foir refuses
Browser ────► your server, holding an admin key ────► Foir, "as" the userThis is the backend-for-frontend (BFF): a server of yours sits between the browser and Foir, holds an admin key, and makes calls for the signed-in user by naming them. It is the wrong shape on Foir:
- The authority is the server’s, not the user’s. An admin key reaches far more than any user, so every bug in that route is a bug in every user’s authorization.
- The logic is invisible to Foir. Nothing about it is declared, rate limited, quota’d or recorded as a run.
- It is a server whose whole job is holding a key, which you deploy, secure and pay for.
Foir refuses it. The header that used to let a key name a user to act as is retired, and a request that still carries it, on any credential, is refused with HTTP 400 (Connect InvalidArgument). The refusal says that a server never acts as a user, and points here. Nobody signs in through an admin key either: a sign-in, a token refresh, or a user’s token sent beside one is refused.
What to do instead: whatever the user may do themselves goes straight from your app, on your publishable key with their bearer (or your server key, from the server that renders your pages). Anything else your app does for them is an operation.
Who may run an operation from the browser
Two gates decide it, and both are configuration rather than code:
- Anyone, signed in or not. Name
operations:execute:<op>in what the publishable key gives, under What anyone can do without signing in in the console. Those runs count against the publishable key’s rate limit, and the operation’s own quotas and preconditions still apply. - A signed-in user. A role that grants
operations:execute:<op>runs that operation, and one that grantsoperations:executeruns every operation the key exposes.
See per-operation scopes for the scope forms.
Worked example: guest checkout
A buyer checks out without an account. The storefront runs one operation, checkout, on its publishable key, which names operations:execute:checkout in what it gives. The operation declares users:create, so it can create the buyer, and records:attribute:order, so the order it writes belongs to them. Its endpoint creates the user and then the order owned by that user, with the client it built from the dispatch token.
No server holds a key, and the buyer holds no role: the order is theirs because the operation said so. When they sign in later, their own bearer reads it. The configuration and the endpoint code are in Writing for a user from an operation.
Guarded fields: the operation holds what it declares
A guarded field is read and written only by a holder of the grant that names it. An operation holds the grants it declares, even when its token carries the user who triggered the run, while the user’s own request holds what their roles give them. The user the token carries still decides ownership, row confinement, the audit actor and quotas (an operation that names a different owner writes as itself). So a field the operation is given and no role is, a cancel token for example, is read and written by the operation and never by the user’s browser. See Guarded fields.
When an admin key is right
An admin key (foir_adm_) is for a machine of your own writing to Foir (integration sync, CI seeds, config push, back-office tooling) and never for your app, because the operations mechanism already gives your app’s logic the permissions it needs. It expires, and is renewed with the same secret: see Credentials.
Related
- Authentication: the three credential classes and what each may hold
- Writing an Operation Endpoint: the dispatch token, in three calls
- Writing for a user from an operation: guest checkout end to end
- You Don’t Need a Backend: why the endpoint is shaped this way
- First-Party Sessions: a cookie session, with or without a server