matter.work
Building Apps / The SDK

Routines and setup

Trigger routines, create per-member routines in code, and build custom setup flows.

Your app can declare routines in matter.json—or create per-member routines dynamically in code—and trigger or inspect them through matter.routines. Member routines exist once the member adds the app.

The Routines page#

Members see your routines on the app’s Routines page, under the app in the sidebar. There they choose which of their computers runs them (Runs on), pick a model for each of your model slots under Models, and pause a routine or run it now. Everything else about a routine is yours, set in matter.json or in code.

Listing, running, and configuring routines#

Triggering and inspecting routines
// List the current member's routines for this app (including last 10 runs each)
const routines = await matter.routines.list()

// Trigger a run immediately with custom input and an idempotency key
const run = await matter.routines.run(
  'research',
  { campaign: campaignId },
  { requestKey: `research:${campaignId}:${day}` },
)
// -> { ok, runId, threadId, queued }

// Fetch recent runs across one or all routines
const runs = await matter.routines.runs({ key: 'research', limit: 20 })

// Update standing input or pause/resume a routine
await matter.routines.setInput('research', { campaigns: ['Q4'] })
await matter.routines.setEnabled('research', false)
Method Description
routines.list() Returns the member’s routines for this app, each with its id, key, title, enabled, deviceId, schedule, input, lastRunAt, nextRunAt, and last 10 runs.
routines.run(key, input?, { requestKey? }) Starts a run now. Passing a requestKey makes retries idempotent: calling run again with the same requestKey returns the existing run instead of starting a duplicate.
routines.runs({ key?, ids?, requestKeys?, limit? }) Returns recent runs (newest first, default limit 50).
routines.setInput(key, input) Sets the standing JSON input (up to 16 KB) passed to every run of that routine.
routines.setEnabled(key, on) Pauses or resumes scheduled runs for that routine.

Creating per-member routines in code#

When your app needs dynamic routines that can’t be listed statically in matter.json—for example, one routine per active outbound campaign—create and remove them with routines.upsert and routines.remove:

Dynamic routines in code
await matter.routines.upsert(`research-${campaignId}`, {
  title: `Research: ${campaign.name}`,
  prompt: 'Find new prospects for the campaign in your input.',
  schedule: '0 9 * * 1-5',
  input: { campaign: campaignId },
  thread: 'continue',
  slot: 'research',
})

// Later, when the campaign finishes:
await matter.routines.remove(`research-${campaignId}`)
  • Accepts the same properties as a manifest routine (title, prompt, schedule, needs, slot, thinking) plus thread ('continue' or 'new'), input, and enabled. The slot must be one that matter.json declares, and the routine runs on the member’s choice for it, the same as a manifest routine.
  • If you omit input or enabled when re-upserting an existing key, the routine preserves its current input and pause state.
  • An app can create up to 200 dynamic routines per member (the member must have added the app first). Dynamic routines persist across app deployments and are deleted if the member removes the app.

When a member adds your app#

A member adds your app with one click (Add), on the computer they’re using. That turns on their routines and starts your worker there. To run code once at that moment—seed their data, make their routines—register matter.onAdd in your worker:

src/worker.ts
matter.onAdd(async () => {
  // Safe to run again: it can, after a failure or if they add the app again.
  await matter.table('settings').upsert('key', { key: 'defaults', digestHour: 9 })
  await matter.routines.upsert('digest', {
    title: 'Morning digest',
    prompt: 'Summarize what changed yesterday.',
    schedule: '0 9 * * *',
  })
})
  • It runs in the worker on the computer where they clicked Add (for your own app, the first computer you open it on), once the worker is running, even if that’s after a restart.
  • It can run more than once: when it throws, Matter runs it again later (up to 10 tries), and a member who removes the app and adds it again gets it again. Make it safe to repeat.
  • Work every computer needs, like a local cache, belongs where your worker starts, not here: the worker starts on every computer it runs on.
  • If you need to ask the member something first, ask on your app’s own first page, and save their answers with matter.routines.setInput or your tables.

matter.setup tells your code where the member’s worker and routines run:

Method Description
setup.get() Returns the member’s setup { id, deviceId }, or null if they haven’t added the app. The setup’s computer runs the member’s routines and the app’s worker.
setup.create({ deviceId?, inputs? }) Moves the member’s routines and worker to another of their computers, or sets each routine’s standing input by routine key.
setup.remove() Removes the app for the member, as Remove for me does, deleting their routines.