Build an app / SDK reference
SDK reference
Use one app context per page to select files, share content, store preferences, and receive desktop events.
JavaScript
import { createAppContext, AppCapabilityError } from './sdk/app-context.js';
const app = createAppContext('my-app');
The my-app argument namespaces browser preferences. The WorkOS installation supplies the app identity inside an eligible hosted window. The starter download includes both SDK modules.
Add the SDK to an existing app
Download the v1 SDK and copy the extracted sdk/ directory next to your app module. The JavaScript files run as ES modules, and the adjacent TypeScript declaration files provide editor completion. Load your app with <script type="module"> and use the local import above.
For an HTTPS prototype, you can import the hosted v1 module directly.
import { createAppContext } from 'https://creaton.dev/workos/sdk/v1/app-context.js';
const app = createAppContext('my-app');
Choose a capability
Add the declaration to workos.capabilities in your manifest. Call await app.supports(operation) before showing a control. For a permission decision, call await app.permissions.query(operation). Possible results are granted, denied, prompt, and unavailable. Start pickers and permission prompts from a user action.
| Declare | SDK methods and behavior |
|---|---|
files.read | files.pickOpen(options) returns an array of handles. files.read(handle) returns a File and version. Browsers support read-only import. |
files.write | files.pickSave(name) chooses a destination. files.write(handle, bytes, version) writes a Uint8Array and returns the next version. Requires an eligible WorkOS window. |
preferences | preferences.get(key), set(key, value), and remove(key) store JSON values. A missing key returns null. Browser fallback uses localStorage. |
share | share.present({ text, url, files }) opens a share sheet. Browser support depends on Web Share and the payload. |
camera | camera.capture({ facing: 'environment' }) returns a File. Check camera.capture. Browser capture requires camera access in a secure context. |
photos | photos.pick() returns one image File. Check photos.pick. WorkOS returns JPEG and ordinary browsers preserve the selected format. |
clipboard | clipboard.readText() and clipboard.writeText(text) use browser clipboard permissions. Check each method by its full operation name. |
notifications | notifications.permission(), requestPermission(), and show(title, options) use site notification settings. Request permission on a click and show after a grant. |
Save a file safely
Declare both file capabilities for an editor. Request edit: true when opening a file and check the returned handle’s writable flag. Keep the version from the read alongside the draft. Serialize saves so each write uses the last acknowledged version.
JavaScript · Save As from a button
async function saveCopy(text) {
const { file, version } = await app.files.pickSave('Draft.txt');
try {
const bytes = new TextEncoder().encode(text);
await app.files.write(file, bytes, version);
await app.setDirty(false);
} finally {
await app.files.release(file);
}
}
Mark the window dirty with app.setDirty(true) when editing begins. Keep the draft on cancellation, denial, or a version conflict. On E_CONFLICT, offer Save As. In a regular browser provide a Blob download for export. Native file transfers are limited to 8 MiB per file.
Remember a setting
JavaScript
await app.preferences.set('count-mode', 'words');
const mode = await app.preferences.get('count-mode') ?? 'words';
WorkOS scopes preferences to the installation and profile. A key can use up to 160 UTF-8 bytes. The host accepts up to 128 keys, 32 KiB per JSON value, and 256 KiB across all values. Removing the installation clears its WorkOS preferences.
Receive a desktop action
app.onActivation(listener) delivers launch, open, and command events. Inspect kind, files, and command. Await the subscription’s ready promise for the initial delivery and call remove() when tearing down the listener. The window command recipe shows how a manifest ID reaches your code.