Hooks are the functions in your app that run on the server: the settings steps, and the functions you give to a capability, like startExport. You don't call them yourself; Examplary does, when it needs them.
Hooks get their own input as arguments, like the test to export, and read everything else from @examplary/apps: the installation's settings, secrets, who it runs for, and storage.
Settings
settings holds the settings of the installation a hook runs for: the workspace's settings. For hooks a teacher runs themselves, like an export, it's merged with the teacher's own when the app has user settings. It's read-only, and only filled in while a hook runs, so read it inside your functions:
import { settings } from "@examplary/apps";
const listClasses = async () => {
const res = await fetch("https://api.acme.com/classes", {
headers: { Authorization: `Bearer ${settings.apiToken}` },
});
return res.json();
};The settings steps are the exception: they get the settings that are about to be saved, or removed, as their argument.
Where hooks run
When you publish your app, the CLI bundles app.ts, with everything it imports, into a single script. Examplary runs that script in a sandbox, a JavaScript environment of its own. That keeps apps apart from each other and from the platform, but it also means hooks can't use everything Node.js offers.
Available in hooks:
- Standard JavaScript, including
async/await,JSON,Date,MathandRegExp fetch(), withHeaders,Response,URLandURLSearchParamsTextEncodercrypto.randomUUID(), andcrypto.subtle.digest()withSHA-1,SHA-256,SHA-384orSHA-512consolefor logging
Not available:
- Node.js modules, like
fs,http,cryptoorBuffer, and npm packages that need them - Timers like
setTimeout AbortController, streaming response bodies,BlobandFormData
Requests made with fetch() have to use HTTPS and can only reach public addresses. A hook can run for up to 25 seconds, with 256 MB of memory; a cron job gets 299 seconds and 1 GB. For work that takes longer, like an export that a platform processes in the background, start it in your hook and let Examplary check back later; export options work that way.
Nothing is kept between two calls of a hook. Store what you need to remember with storage, or return it from your hook when the capability allows it, like the data of an export.
Errors
When a hook throws an error, Examplary shows its message to the person who caused the hook to run, like the admin filling in the settings or the teacher exporting a test. Write those messages for them:
if (res.status === 401) {
throw new Error(
"Your Acme API token has expired. Update it in the app's settings.",
);
}Secrets
Some values belong to your app rather than to a workspace, like the client secret of your OAuth app. Store those as secrets, which only your app's hooks can read:
npx @examplary/cli set-secret ACME_CLIENT_SECRET "..." --key YOUR_API_KEYThen read them from env inside a hook. Secret names are written in UPPER_SNAKE_CASE, and a secret that isn't set is undefined:
import { env } from "@examplary/apps";
const exchangeCode = async (code: string) => {
const res = await fetch("https://api.acme.com/oauth/token", {
method: "POST",
body: new URLSearchParams({
code,
client_id: "examplary",
client_secret: env.ACME_CLIENT_SECRET!,
}),
});
return (await res.json()).refresh_token;
};Secrets are only available while a hook runs. Read env inside your functions, not at the top of a file, where it would still be empty.
Who a hook runs for
context tells you the workspace a hook runs for, and the user who started it, if any. These are Examplary IDs, which you can use to tell installations apart on your side:
import { context } from "@examplary/apps";
const workspaceId = context.org; // "org_…"
const userId = context.user; // "user_…", or undefinedCalling the Examplary API
To call Examplary's own API from your hooks, list the scopes your app needs in defineApp():
const app = defineApp({
id: "acme.gradebook",
// ...
apiScopes: ["exams:read", "audit-logs:read"],
});Every hook then gets context.examplary, an Examplary SDK client limited to those scopes. When a user started the hook, like a dashboard page action, it acts as that user, with their permissions. When there's no user, like in a cron job, it acts as your app, with an admin's access to the workspace; the audit log then shows your app as the actor.
import { context } from "@examplary/apps";
const { url } = await context.examplary.auditLogs.export({
from: "2026-11-10",
to: "2026-11-10",
});Your code never sees the credentials behind it: the client only works while the hook runs, and only for this workspace. The scopes of a public app are part of the version the Examplary team reviews.