Skip to content

The person behind a page

Markdown

A page, and the server behind it, both need to know which person they serve.

Who is behind a request: x-apigami-user-ref

Section titled “Who is behind a request: x-apigami-user-ref”

Every route at your app’s own address knows the person in front of the browser, as req.user would in another framework: a page, a route of your own API, /oauth/callback, a file, an action. Apigami keeps their session in two cookies of your address, which no script reads, and hands your code their user_ref in a header, the same user_ref your actions read from GET /v1/me:

export default {
fetch(request: Request) {
// Null for a visitor, in the side panel, and on any request another site sent.
const userRef = request.headers.get('x-apigami-user-ref')
return new Response(userRef ? 'Hello again' : 'Hello, visitor')
},
}
  • The session starts once. The first time a page calls apigami.ready(), or the visitor apigami.connect(), the browser goes through /__apigami/start on your address, then Apigami’s connect page, and comes back with the session: without a click when the person already approved your application, after « Continue » otherwise. It lasts until 30 days go by without a visit, and ends sooner when the person disconnects from your application, signs out of Apigami’s dashboard, or their account is disabled.
  • It counts for your own pages’ requests and for a top-level navigation by GET or HEAD: a bookmark, a link, a service sending the person back. Never for a request another site or application sends: a form, a fetch, a WebSocket from another origin, or a page of yours in someone else’s frame. A link from anywhere still reaches you with the person, so keep every GET free of side effects.
  • The header names the person, never their name nor email. A page or a route that needs them, under identity, reads identity from GET /v1/me with the page’s token, or from GET /v1/users/{user_ref} with your key; apigami.ready() never hands them to the page.
  • Nobody forges it. Every x-apigami-* header and X-Forwarded-Host a request carries is taken out before your code, and Apigami’s cookies are taken out of Cookie.
  • It is absent on the request that loads a side panel page, framed by the dashboard, whose page gets its token by message, though the actions it calls with that token carry it; for a visitor; and once the session expired while Apigami’s API could not renew it for an hour, served as a visitor.
  • An action carries it too, on any call for a person: from a page, a sub-app’s call, a key with X-Apigami-User-Ref, beside X-Apigami-Run. Tell an action from a page by x-apigami-action, never by this header (What your code receives). A run Apigami starts itself for a person carries it as well, beside X-Apigami-Run: a task of a queue at your own path or a subscription naming them, or app_run for them. It is the user_ref GET /v1/me answers. A run that serves nobody, a cron’s, carries only X-Apigami-Run.
  • It is absent too on an address another application gives your public pages, which serves no session: there, x-apigami-root-app-id names that application (An address of theirs for your public pages), and GET /v1/me of a person’s run answers it as origin_under_root.
  • /__apigami/ is Apigami’s. Those paths never reach your code, and a version holding a file under __apigami/ is refused (reserved_envelope_path). Your entrypoint exports default { fetch(request, env, ctx) }: the validation warns about a missing one, or a node:http or Express server (no_default_fetch), and refuses Deno.serve (deno_api_refused).

Your neighbours under apigami.tech. Until browsers treat each application there as a site of its own, the other applications count as the same site as yours. Apigami ignores your session for their forms, fetch, WebSockets and frames, never runs an action of yours from them, and sets your pages’ frame-ancestors itself (Who may frame your pages). Two risks remain, accepted for now:

  • a link from another application reaches a page of yours with the person, as on any site;
  • another application’s page can set hundreds of cookies on apigami.tech from its script, pushing your session out of that browser, or making your address unusable there.

A domain of your own is a site of its own, and none of them applies there.

How a page knows who is viewing and calls its actions

Section titled “How a page knows who is viewing and calls its actions”

A page is shown in one of three ways, and apigami.js handles each:

  • Full page, in a tab of its own. At your app’s own address, apigami.js holds no token: the person’s session lives in cookies only the address reads, apigami.js asks it who is viewing at POST /__apigami/session and calls your actions at the same origin, and nothing is ever in the page’s address. A browser without a session there is a visitor’s (A visitor on your page): apigami.session() says whether a person is connected, and apigami.connect({ next }) gives the visitor a session and brings them back to next.
  • In the side panel, a side_panel page, or your settings page as the panel’s last tab, in a frame whose parent is the Apigami dashboard, https://app.apigami.com, inside the site’s own page. The panel sends the page its token by message once it is shown, and apigami.connect() does nothing there.
  • In a frame on another site your version lists as one that may frame it: the page has a session of its own in that frame, which the person starts with one click (A page of yours on another site).

In the side panel, the request that loads the page identifies neither the user nor the browser. Full page, your server reads the person in x-apigami-user-ref, never the browser. Load apigami.js and let it find out:

<script src="https://api.eu.apigami.com/apigami.js"></script>
<script type="module">
const session = await apigami.session()
if (!session.connected) {
// A visitor, full page: back here with a session once connected.
document.querySelector('#sign-in').onclick = () => apigami.connect({ next: location.pathname })
} else {
render(session.user_ref, apigami.state.url)
const cart = await apigami.call('getCart', {})
apigami.onChange((state) => render(session.user_ref, state.url))
}
</script>

A page that is only for connected people calls await apigami.ready() instead, which connects a visitor first. The second script is type="module": an await outside a function is a SyntaxError in a plain <script>, and the page stays blank. Without modules, write apigami.ready().then(({ user_ref }) => …). Load it by this absolute address, never a relative one (Load apigami.js).

Member What it does
apigami.ready() A promise of { url, install_ref, user_ref, app_id, page_id, page_kind }, once GET /v1/me has said whom the page serves: full page, from the person’s session at your address; in the side panel, from the panel’s token. url is the address of the site the side panel is open on, null full page; user_ref is the person, the one to key their data on. If GET /v1/me refuses, as once the session ended, it rejects with an apigami.ApigamiError carrying that status; in the side panel, the next token tries again. Full page, it waits for a person: without a session, it first sends the visitor through apigami.connect(), which brings them back to this page with a session. Call it only where the page needs to know who is viewing.
apigami.session() A promise that sends nobody anywhere: { connected: true, … } with what apigami.ready() gives, once GET /v1/me has accepted the session or the token, else { connected: false, app_id } for a visitor. In the side panel, it waits for the first token.
apigami.connect({ next }) Full page: leaves for /__apigami/start on your address, then Apigami’s connect page, which gives the visitor a session, without a click when they already approved your application, and brings them back to next, a path of this page’s address, the current one by default. Work not saved by an action is lost. In a frame on another site, call it in a click handler: it opens Apigami’s window, then reloads the frame with the person (A page of yours on another site). In the side panel it does nothing.
apigami.state The latest { url, install_ref, tab_id, settings_needed }; in the side panel, null before the first message. Never the token. install_ref is the browser your actions drive, as your group knows it: the one showing the side panel, or, opened full page, the one the user just connected, once apigami.ready() resolves; null when the page drives none. settings_needed is what GET /v1/me lists (Ask a person to set up or reconnect), [] until then. tab_id is the tab the side panel is open in, to pass to your actions (The site the side panel is open on); null full page, framed elsewhere, or before Apigami 0.17.6.
apigami.onChange(fn) Calls fn(state) at each later change of url, install_ref or tab_id: added after await apigami.ready(), it is not called for the state the page already has, which apigami.state holds. Not when the session or the token is renewed: apigami.js keeps that to itself. Returns the function that removes fn.
apigami.call(action, input, { app, params }) Calls the action at its method and path, at this page’s own address, credentials: 'same-origin': the session cookie does the rest, and in the side panel the page token goes in Authorization. params fills a path template ({ grant_id: 'abc' }); input goes in the query string for a get, head or delete, as a JSON body for the others. With app, the app_id of an application yours calls, a sub-app’s settings actions for instance, it calls that application’s action instead, at /__apigami/apps/{app_id}/… of this same address, for the same person, when the version serving your page declares it in x-apigami.subapps, or a sub-app it declares had it for mandatory when the version was written: your page never holds the sub-app’s session. For a visitor, only an action your manifest lists in open_actions answers; any other answers 401 bad client credentials and apigami.js then sends the visitor through apigami.connect(), on a refusal of Apigami’s only, the one carrying X-Apigami-Platform: your own action’s 401 does not. Resolves to the JSON body, or the text. Any other status than 2xx rejects with an apigami.ApigamiError carrying status, body and request_id, from X-Apigami-Request-Id. The answer streams for as long as the action runs: there is no 202 and nothing to read later, so answer quickly: the person is looking at the page.
apigami.me() GET /v1/me for the person of the page, through /__apigami/v1/me of your address full page, with the page token in the side panel.
apigami.open(appId, path) On a click: opens a window on path at the address of an application yours calls, where it gets its own session for this person, without a click since they approved yours (Show the settings of the sub-apps you call). Call it in the click handler itself: it opens the window before it awaits anything. Resolves when the person is back, on that window’s { type: 'apigami:settings-changed' } message or when this window gets the focus again. Rejects when the browser blocked the window, for a visitor, and when your application may not call that one. For an optional sub-app the person has not accepted, the window shows Apigami’s screen first, with what the sub-app adds that they never decided, a site through the browser or a permission; it is the one way to accept it, since the window carries a request_uri only your page obtains, claimed once within 90 seconds.
apigami.done({ result }) Full page, the end of a step of a sub-app’s page, a sign-in for instance, result being connected, cancelled or failed. In a window apigami.open() opened, it posts { type: 'apigami:settings-changed', result } to the page that opened it, at the address of the application that called yours, then closes the window; when the browser no longer lets it close, because a page on the way cut its opener, it shows « Done, you can close this tab. ». In a tab, it replaces the page with root_home_url, from GET /v1/me, plus ?apigami_result={result}: the home of the application the person connected to, else its settings page, production’s or --staging’s as they came, its own domain first, computed by Apigami and never read from the page’s address. Without one, it shows « Done, you can close this tab. ». Rejects in the side panel and in a frame on another site, and for any other result.
apigami.subapps() GET /v1/apps/{app_id}/subapps for the person of the page, as apigami.me() reaches Apigami: a promise of [{ app_id, name, is_optional, is_accepted, needs_browser, domains, logo_url, brand_color, metadata }], every sub-app the version serving your page declares, with whether the person accepted it under the application they connected to, is_accepted false for an optional one they have not accepted and reach no other way, always true for a mandatory one; logo_url and brand_color are the sub-app’s own or null, metadata the pairs its production declares in x-apigami.metadata. apigami.call knows the actions of these sub-apps only, and of the mandatory ones under them: an optional one the person’s team may not call has no action for them. Rejects for a visitor, and with 400 person required for a page that serves nobody.

apigami.js accepts a message only from the dashboard’s origin and from the window above the page. Full page at your app’s address it holds no token at all. In the side panel it keeps the page token in memory, never in localStorage, sessionStorage or a cookie of its own. The one thing it writes to sessionStorage is when it last sent that page to be connected, or, in a frame on another site, when that frame last came back from Apigami’s window. It never navigates the side panel’s frame; a frame on another site reloads once the person connected. For a person, it sends the page’s uncaught errors to app_logs with source: 'pages', ten a minute at most, including up to ten raised before the person was known. In a frame your version does not allow, Apigami gives it no session; under Firefox, which does not tell a page who frames it, a frame waits as the side panel would and says so in the console after five seconds. A page in a tab of its own is a full page, open to visitors.

The call needs no key, no user_ref and no install_ref: the session already names the person and the browser, and X-Apigami-User-Ref beside it is a 400. It runs the version serving this address: production’s at your address, the --staging version at --staging, where only a member of your group has a session. A page left open across a publication calls the new version: reload it, or keep the old action names for a while. The call is of the same origin, so every header of the answer is readable. A page token is accepted only from the origin of its page: the same token sent from another site, or once expired, is a 401 bad page token.

Who may frame your pages: x-apigami.embeddable_on

Section titled “Who may frame your pages: x-apigami.embeddable_on”

Apigami sets the frame-ancestors of every answer of your address itself, in place of yours:

  • a side_panel page gets frame-ancestors https: chrome-extension:, since the side panel sits under the site of the tab, Apigami’s own frame and the dashboard; the page token, bound to its page’s origin, protects it;
  • every other page gets the origins your version lists in x-apigami.embeddable_on, exact https:// origins (The manifest), and 'none' without one;
  • when that list holds the reserved word "apigami", a page opened under an application that declares yours also gets what that application allows (Inside the applications that declare yours).

A page of yours on another site. List the site in x-apigami.embeddable_on, and that site puts <iframe src="https://{slug}.apigami.tech/widget"> in its page: nothing else, no key and no server on its side.

  • A page that needs nobody works as it does anywhere: its open actions answer a visitor.
  • A page that needs the person loads apigami.js as usual. Without a session in that frame, apigami.ready() covers the whole frame with Apigami’s sign-in card, « Sign in to continue » and a « Continue with Apigami » button, which reads « Finish in the window that opened… » while that window is open; a click there starts over. apigami.connect(), called in a click handler of your own, opens the window directly, and outside a click the browser blocks it and it rejects. The click opens a small window on Apigami’s connect page, which closes at once for someone already signed in to Apigami in that browser and already connected to your application; otherwise it asks for a code by email or for « Continue », as any first visit does. The frame then reloads with the person.
  • The session is the frame’s own, in a cookie partitioned by the framing site: another site framing the same page sees none of it, and neither does a tab of your own address. It slides like any page session, 30 days after the last visit, so the person clicks once per browser and per framing site.
  • The frame calls its actions with apigami.call(), at the same origin, like any page of yours: only what apigami.js sends, and the frame’s own page load, carry the person. A fetch your code sends itself from the frame is a visitor’s.
  • Every site in the chain is checked, on every request: a page framed by a site your version does not list, another subdomain of a listed site included, never has the person there, nor does a side panel page, which any site may frame.
  • Browsers. Chrome 114 and later, Safari 26.2 and later. Where the browser keeps no cookie in a frame (Safari 18.5 to 26.1), the frame offers a link that opens the page in a tab instead. Firefox does not tell a page who frames it: there the frame waits as in a side panel, with no button; offer your own link to open the page in a tab.
  • The framing site must let the frame open a window: with sandbox, add allow-popups allow-popups-to-escape-sandbox.

Inside the applications that declare yours: "apigami"

Section titled “Inside the applications that declare yours: "apigami"”

Your application is a sub-app that others reuse, a connectors page for instance, and each of them wants your page inside its own settings. You cannot list addresses you do not know. Write the reserved word once, alone or beside your origins:

"x-apigami": { "embeddable_on": ["apigami"] }

It says: every Apigami application that declares mine as a mandatory sub-app may show my pages in a frame, wherever that application is itself shown.

  • What you write, as the sub-app: the word, lowercase, in the list. "Apigami", "*" and https://*.apigami.tech are embeddable_origin_invalid: there is no wildcard.

  • What the other application writes, the root: an alias to your page, and a frame on that alias in a page of its own:

    "subapps": [{ "app_id": "THE_SUBAPP_APP_ID" }],
    "pages": [{ "kind": "page", "id": "connectors", "page_url": "/connectors",
    "alias": { "app_id": "THE_SUBAPP_APP_ID", "page": "home" } }]
    <iframe src="/connectors" title="Connectors" style="width:100%;height:720px;border:0"></iframe>
    <a href="/connectors">Open in a tab</a>

    Its own embeddable_on stays empty for a frame at its own address. The frame loads the alias, never your address directly: the alias is what tells Apigami which application frames you.

  • Where your page then shows: at that application’s own addresses, {slug}.apigami.tech, its --staging address for its --staging version, and its own hostnames; and on the sites that application lists in its own embeddable_on, for a product such as https://www.contoso.com that frames the alias of its application. You no longer list that site yourself. The origins you list keep working as before.

  • What the person sees: your page inside the other application’s page. The first time, the sign-in card described above and one click; the window closes at once for someone already connected to that application. Then nothing for 30 days.

  • Whose session: the person’s, at your address, under that application, and only for someone connected to it. A session opened under one application never serves in the frame of another, every apigami.tech address sharing one cookie partition: the person clicks once per application, and going from one to another in the same browser signs the frame in again. A frame left open under the first one goes blank at its next page load, until its page is reloaded. A --staging version of that application frames you with the session its production opened: same team, same person.

  • Call your actions with apigami.call() in these pages, never with a fetch of your own. At another application’s apigami.tech address the frame is on the same site as your address: there a fetch your code sends itself carries the session of the person’s tab at your address when they have one, whichever application frames you, and an unreviewed --staging version of any team may declare yours. What apigami.js sends never does.

  • Never framed by the word: an application that declares yours with is_optional: true; an application that does not declare yours; a site that no application brought, which you still list yourself.

  • Browsers: the same limits as any frame, above, and one more. Where the browser keeps no cookie in a frame (Safari 18.5 to 26.1), a frame on another site than apigami.tech, the application’s own hostname or a site it lists, stays empty instead of offering its link, unless you list that site yourself. Keep the « Open in a tab » link beside the frame: Firefox shows no button in it.

The messages apigami.js exchanges, for a page that cannot load it

The dashboard sends the page, once it is shown, and again whenever the tab’s address or the token changes:

{ type: 'STATE_CHANGE', state: { url, install_ref, token, tab_id } }
  • url is the address of the tab the side panel is open in.
  • install_ref is this browser as your group knows it, the same value your key reads elsewhere.
  • token is a page token of your application for this person, bound to this page’s origin. It lives five minutes and the panel renews it at half-life (How long a page keeps the person). Each renewal arrives as a new STATE_CHANGE, so always use the latest token. Both install_ref and token are null when the panel could not obtain one, and the side panel then shows why above your page.
  • tab_id is the tab the side panel is open in, null or absent when the panel names none.

Answer each message with STATE_CHANGE_ACKNOWLEDGE carrying the state you received; until you do, the same state is sent again every 250 ms. Install the listener first, then announce the page with APIGAMI_PANEL_READY. Check the sender’s origin and window on every message. Never navigate the frame in response to STATE_CHANGE: each load starts the exchange again, and a page that reloads itself in a loop is suspended by the side panel. Call an action at its path on your own address, the token in Authorization: Bearer.

<!doctype html>
<meta charset="utf-8">
<button id="search" disabled>Search</button>
<pre id="out"></pre>
<script>
const DASHBOARD = 'https://app.apigami.com';
const ACTION = '/search'; // searchProducts, at this page's own address
let latest = null; // { url, install_ref, token, tab_id }, in memory only
window.addEventListener('message', (event) => {
if (event.origin !== DASHBOARD || event.source !== window.parent) return;
if (event.data?.type !== 'STATE_CHANGE') return;
const state = event.data.state;
if (typeof state?.url !== 'string') return;
latest = state; // a renewed token replaces the old one, which no longer works
document.getElementById('search').disabled = !state.token;
window.parent.postMessage({ type: 'STATE_CHANGE_ACKNOWLEDGE', state }, DASHBOARD);
});
window.parent.postMessage({ type: 'APIGAMI_PANEL_READY' }, DASHBOARD);
document.getElementById('search').addEventListener('click', async () => {
const response = await fetch(ACTION, {
method: 'POST',
headers: { Authorization: `Bearer ${latest.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ query: 'coffee' }),
});
document.getElementById('out').textContent = `${response.status} ${await response.text()}`;
});
</script>

On a tab where a side_panel page of theirs matches, the person sees the app bar at the site’s right edge, one icon per application. A click on yours opens your side panel inside the page: one tab per matching page, named by its title, your settings page last. One application at a time: another icon swaps in that application’s panel, and yours leaves the page. The panel remembers, per host, whether it was open, which application and which tab. The Apigami icon in Chrome’s toolbar opens Apigami’s settings, not the panel. Your page takes no click until it has been in full view for half a second, nor while the site covers it. Before Apigami 0.17.0, the side panel is Chrome’s own, opened from the Apigami icon in its toolbar.

Your page knows that tab: apigami.state.tab_id, and apigami.state.url its address, with apigami.onChange when it changes. Pass tab_id to your action, which passes it as tabId to tabs.read, tabs.parse, tabs.storage or a gesture on that tab’s domain: without a tabId, an operation acts on the first open tab of the domain, which need not be the one beside your panel.

const correspondent = await apigami.call('detectCorrespondent', { tabId: apigami.state.tab_id })

tab_id is null opened full page, framed on another site, and before Apigami 0.17.6. Passing that null as tabId is the same as omitting it, so on an older Apigami pass apigami.state.url as well, and let the action pick, in tabs.list, the tab with that url, the active one if several have it.

Load apigami.js by its absolute address, never a relative path

Section titled “Load apigami.js by its absolute address, never a relative path”
<script src="https://api.eu.apigami.com/apigami.js"></script>

The same address for every application and every page: a relative path reaches your own code, which does not serve the script. At your application’s own address, a page loading /apigami.js, or the older /__apigami/panel.js, never gets window.apigami: the validation refuses it (page_script_on_app_host) and so does publication (page_failed). Any other relative path is warned about (page_script_relative). A Content-Security-Policy needs https://api.eu.apigami.com in script-src, for the script, and in connect-src, for the calls it makes.

A visitor on your page: apigami.session() and apigami.connect()

Section titled “A visitor on your page: apigami.session() and apigami.connect()”

A page opened full page without a session is a visitor’s: the person’s first visit in this browser, a session that ended, an address someone shared. The page loads and shows what it shows anyone, and apigami.js navigates nowhere on its own. apigami.session() answers { connected: false, app_id } and sends nobody anywhere; apigami.call() reaches only the actions your manifest lists in open_actions. apigami.ready(), apigami.connect({ next }) and an action Apigami refuses for want of a person send the visitor to /__apigami/start?next=… on your own address, then to Apigami’s connect page, which brings them back to next, the current path by default, with their session: at once when they already approved your application, after « Continue », which connects them, otherwise. Your app’s bare address is therefore an install link too. An open action’s own 401, a wrong password, sends nobody. In the side panel, apigami.session() waits for the panel’s first token, and apigami.connect() does nothing.

apigami.js learns which application it is from the page’s address (GET /v1/pages/app): a page of any other origin than your app’s addresses serves nobody, and apigami.ready() and apigami.connect() reject with a 404. If the page comes back without a session within a minute, apigami.js shows a short message with the link instead of going round again. The errors of a visitor’s page never reach app_logs, which needs a person.

How long a page keeps the person: the page token’s lifetime

Section titled “How long a page keeps the person: the page token’s lifetime”

Nothing a page holds stops on its own: Apigami renews it while the page is used.

Where the page is What holds the person Renewed
Full page, at your app’s address two cookies of your address: an access token of 5 minutes and the session, 30 days By Apigami, in the answer itself, past half the access token’s life, on a page load, on a request the page makes to your own routes (fetch, XHR, a video or an audio it plays) or on an action for the person, never on an image, a script, a style sheet or a font. The session slides 30 more days each time, and ends after 30 days without a visit.
Side panel a page token of 5 minutes, sent by message By the side panel, at half-life, for as long as it shows the page.
Frame on a site you list two cookies of your address, kept for that site alone As full page: each site that frames the page has its own session, which slides 30 days.

When Apigami’s API cannot be reached, an expired access token still serves for an hour, never once Apigami has refused the session. The session ends at once when the person disconnects from your application, signs out of Apigami’s dashboard, or their account is disabled; removing them from your application with app_users_write reaches their pages within five minutes. A page left open then stays as it is, and the next apigami.ready(), or the next action Apigami refuses for want of a person, 401 session expired: sign in again, sends them to be connected again, which brings them back to the same path: without a click at your app’s address, while they are still connected to your application.