--- title: Authentication description: Open a document with a token from your backend, by id, or with a grant, and what a token carries. engines: [cloud] --- {/* Generated from docs/content — edit there, then `pnpm docs:sync`. */} import { Callout } from '@/components/docs/callout'; # Authentication Every request the engine makes carries a token. Your backend creates the token and signs it with your secret key; the engine only passes it along, and the server checks it. The token says which document the user may open, what they may do with it, and who they are. ## Three ways to open a document Pick the one that matches where the credential comes from. ### With a document token Your backend creates a token for one user and one document. This is the most common way: ```ts const doc = await engine.open({ kind: 'token', token: () => fetchDocumentToken(documentId), }); ``` The token names the document, so you don't pass an id. Pass a function rather than a string: the engine calls it when it needs a token, keeps that token, and calls it again in the background a minute before it expires. A short-lived token never runs out mid-session, and nothing waits for the renewal. When CloudPDF refuses a kept token (it was revoked), the engine asks the function once more and tries again. One engine can hold many documents opened this way, each with its own token. ### By id When your app already has a token for your whole account (an admin tool, a backend job), open any of its documents by id: ```ts const engine = cloudEngine({ token: () => fetchAccountToken() }); const doc = await engine.open({ kind: 'id', id: 'doc_proposal_2026' }); ``` The engine's `token` is used for every document opened by id. To use a different one for a single document, pass `token` in the input as well. ### With a grant A grant (`grt_…`) is public access to one document, made in the CloudPDF dashboard. It needs no backend at all, which makes it right for public embeds: ```ts const doc = await engine.open({ kind: 'grant', grant: 'grt_9f2k…' }); ``` A grant isn't a credential itself: the engine exchanges it for a short-lived token, and renews that token before it expires. Editing or ending the grant in the dashboard changes every embed at the next renewal. The server only accepts the exchange from the websites the grant allows. If the grant has a passphrase, pass it as `passphrase`. Without it, `open()` fails with `GrantPassphraseRequired`, so you can ask the user and try again: ```ts const doc = await engine.open({ kind: 'grant', grant, passphrase: 'open-sesame' }); ``` A grant gives everyone who opens it one shared layer, or each visitor a layer of their own, made at their first change. `doc.grant` says which: ```ts doc.grant?.layerMode; // 'per-visitor' doc.grant?.keep; // true ``` The engine remembers the visitor in the browser's `localStorage`, so the next visit is the same visitor: their answers stay, what they added stays theirs, and on a per-visitor grant the same layer opens. The exception is a per-visitor grant that doesn't keep visitors' layers: there every visit is a new visitor with a new layer, and `keep` is `false`. To start over, open with `restart`: a new visitor begins, and on a per-visitor grant the old layer is deleted. ```ts const doc = await engine.open({ kind: 'grant', grant, restart: true }); ``` Outside a browser the engine remembers no visitors, since a server opens grants for many people. To keep them somewhere else, pass a `visitorStore` to `cloudEngine()`. ### Who the visitor is A grant can ask its visitors who they are: their name, email, job title, organization or department, each required or optional. It asks when they open the document, or before their first change. The answers are given once. They become the visitor's identity in every session, so their name shows on what they add, and they can't be changed later: a visitor who made a typo starts over with `restart`. A grant that asks on open makes `open()` fail with `VisitorIdentityRequired` until the answers come with it. `error.details` says what the grant asks (`fields`), what's still `missing`, which answers were `invalid`, and the owner's `purpose` and `privacyUrl` to show with the questions: ```ts try { doc = await engine.open({ kind: 'grant', grant }); } catch (error) { if (!EngineError.is(error, EngineErrorCode.VisitorIdentityRequired)) throw error; const answers = await askTheVisitor(error.details); // your form doc = await engine.open({ kind: 'grant', grant }, { identity: answers }); } ``` When your page already knows who the visitor is, pass it as `identity`, as you would to the local engine. It counts the same as the visitor typing it: the grant takes only the fields it asks (never `userId` or `groupId`, which the server gives each visitor), and an answer once given stays. A site that knows its users for certain gives them tokens from its backend instead. ```ts const doc = await engine.open( { kind: 'grant', grant }, { identity: { displayName: 'Dana Smith' } }, ); doc.security.identity; // { userId: 'visitor_…', displayName: 'Dana Smith' }, if the grant asks for a name ``` `doc.security.identity` says who the session acts for. In development the engine warns when that isn't the `identity` you passed, and says why. A grant made for one person can say who that is: its own `identity`, set when the grant is made. Every session it gives acts for that person, whoever opens it. The grant doesn't ask what it says, and no answer changes it, so anyone with the grant acts as that person. A grant that asks before the first change opens at once, and the visitor reads freely. `doc.grant.identityRequest` says what it asks. Their changes wait for the answers: listen for a change that waits, ask, and answer with `identify()`, or let the waiting changes fail with `decline()`. ```ts doc.grant?.onIdentityRequested(async (request) => { const answers = await askTheVisitor(request); if (answers) await doc.grant!.identify(answers); else doc.grant!.decline(); }); ``` A change made while nothing listens doesn't wait: it fails with `VisitorIdentityRequired`. ### When the grant changes while it's open A passphrase, or questions asked on open, that the grant gets while a document is open apply to that document too: its session isn't renewed without them. `doc.grant.openRequest` says what the grant asks: `{ kind: 'passphrase', passphraseProvided }`, or `{ kind: 'identity', request }` with the questions. The document reads on until its session runs out, then its requests wait. Listen for it, ask, and go on with `resume()` as the same visitor, or close the document: ```ts doc.grant?.onOpenRequested(async (request) => { const given = await askTheVisitor(request); // { passphrase } or { identity } if (given) await doc.grant!.resume(given); else await doc.close(); }); ``` `resume()` rejects with `GrantPassphraseRequired` or `VisitorIdentityRequired` while the grant still asks, and `openRequest` says what. With nothing listening, a request fails with the same error once the session runs out. ## Password-protected PDFs A PDF can have its own password, separate from any token or grant passphrase. Pass it in the options, whichever way you open: ```ts const doc = await engine.open({ kind: 'token', token }, { password: 'pdf-password' }); ``` [Opening documents](/docs/engine/documents/opening#password-protected-files) shows how to ask for it only when a file needs one. ## What a token carries A token carries the user's **permissions** (for example `doc.render`, `doc.text.copy`, `annotations:update:self`) and their **identity** (a user id, a display name, a group). The server enforces the permissions on every request; the identity becomes the author of the annotations the user creates. Both come from the token: the `scope` and `identity` you pass to `open()` don't change them, and in development the engine warns when you pass `scope`, or an `identity` the session doesn't act for. (On a grant, `identity` is what your page knows about the visitor: see [Who the visitor is](#who-the-visitor-is).) Read what the user was given from `doc.security`: ```ts doc.security.allows('doc.download'); // may this user download the file? doc.security.identity?.displayName; // who they are ``` [Permissions](/docs/engine/concepts/permissions) lists every permission and how to build UI that matches them. Create tokens on your server, never in the browser, and keep them short-lived. A document token opens one document only, so a leaked token can't be used for any other. ## When something is wrong A missing or expired token fails with `Unauthenticated`; a token without the permission a call needs fails with `Forbidden`. See [Errors & cancelling](/docs/engine/concepts/errors-and-cancelling).