Settings are what gets filled in when an app is installed, like an API token or the URL of a school's environment. Examplary shows them as a form on the app's card, validates them, and passes them to every hook of the app.
app.addSetting({
key: "environment",
label: { en: "Environment" },
type: "select",
options: [
{ value: "eu", label: { en: "Europe" } },
{ value: "us", label: { en: "United States" } },
],
default: "eu",
});Workspace and user settings
By default, apps are installed with workspace-level settings (org scope).
However, some apps might make more sense as an app an individual teacher
connects, using user-level settings. For such apps, you can define
scope: "user" in the defineApp call.
This will allow teachers to enable the app for their account after it has been installed for the workspace.
A setting without a scope of its own takes the app's scope. So in an app with
scope: "user", every setting is filled in by each teacher, unless you give it
scope: "org": do that for things like a school's API key, and leave the
teacher's own values, like their course ID, as they are.
Field types
| Type | Shown as |
|---|---|
text | A text input |
password | A text input that hides what's typed |
url | A URL input |
boolean | A switch |
select | A dropdown with the given options |
component | Your own React component, see below |
Other properties of a setting:
| Property | Description |
|---|---|
key | The key its value is stored under, like apiToken. Required. |
label | The field's label. A setting without a label isn't shown: use these for values your hooks set. |
help | Help text shown under the field. |
placeholder | Placeholder text. |
default | The initial value, a string or (for boolean) a boolean. |
schema | A JSON schema the value has to match, for anything its type doesn't check, like { type: "string", pattern: "^acme_" }. A zod schema works too. |
optional | Whether the field may be left empty. Fields are required by default. |
secret | Store the value, but never send it back to the browser. Use this for passwords and tokens. |
scope | org or user, see above. |
Checking and preparing settings
Before saving, Examplary checks each value against its setting's type (a
non-empty string, a valid URL, a boolean, or one of a select's options) and
its schema, if it has one. To check more, like whether an API token actually
works, add a validate step. It gets the settings and has to return true;
throw an error to show its message to the person filling in the form:
app.addSettingsValidateStep(async (settings) => {
const res = await fetch("https://api.acme.com/me", {
headers: { Authorization: `Bearer ${settings.apiToken}` },
});
if (!res.ok) {
throw new Error("Acme didn't accept this API token.");
}
return true;
});A prepare step runs before that, and can turn what was filled in into what
you'd like to store. It returns the settings to save, which are then checked
as usual. A common use is swapping a one-time sign-in code for a long-lived
token, which you then store in a secret setting without a label:
app.addSetting({
key: "refreshToken",
type: "text",
secret: true,
});
app.addSettingsPrepareStep(async (settings) => ({
refreshToken: await exchangeCode(settings.authorizationCode),
}));To clean up when a workspace removes your app, add an uninstall step. It gets the workspace's settings; if it throws, the app stays installed and the admin sees your message:
app.addUninstallStep(async (settings) => {
await disconnectWorkspace(settings.apiToken);
});Your other hooks read the saved settings from
settings.
Component settings
When a form field isn't enough, like for a "Sign in with…" button that opens a popup, a setting can render your own component:
app.addSetting({
key: "authorizationCode",
label: { en: "Acme account" },
type: "component",
component: {
path: "./components/connect-button.tsx",
translations: {
connect: { en: "Connect your Acme account" },
},
},
optional: true,
});The component gets setValue(key, value) to set the value of any of the app's
settings, and submit() to save them. A card with a component setting has no
save button of its own, so the component has to call submit():
import { Button, FrontendSettingsFieldComponent } from "@examplary/ui";
const ConnectButton: FrontendSettingsFieldComponent = ({
t,
setValue,
submit,
}) => (
<Button
onClick={async () => {
setValue("authorizationCode", await signInWithAcme());
await submit();
}}
>
{t("connect")}
</Button>
);
export default ConnectButton;