Release hooks and the release webhook
When a new release of your app goes out, two things can happen on top of the release itself:
- Release hooks: code from your app's own files that changes the app's stored data to the shape the new release expects.
- The release webhook: one signed HTTP call from the platform to an address you choose, so your own systems hear about every release.
Both apply to apps whose source is kept as project files, in environments where release hooks are switched on.
Release hooks
Declare the hooks in widget.json:
{
"name": "Leaderboard",
"version": "2.0.0",
"lifecycle": {
"dataVersion": 2,
"migrate": {
"up": "hooks/migrate.js#up",
"down": "hooks/migrate.js#down",
"post": "hooks/migrate.js#post"
}
}
}| Field | Meaning |
|---|---|
dataVersion | A whole number, 0 or more. The shape of stored data this release expects. |
migrate.up | Runs before the release goes live, when the stored data is at a lower version. |
migrate.down | Runs before an earlier release is restored, to take the data back. |
migrate.post | Runs 15 minutes after the release went live, or when you start it yourself. Use it to remove what the previous release still needed. |
Each hook is "<file>#<export>": a .js or .mjs file inside your app and the export to call. Without #<export> the default export is called. All three are optional.
What a hook receives and returns
A hook is a function like any other function of your app. It runs in the same sandbox, with the same per-call time limit (5 seconds), and it is called in batches:
// hooks/migrate.js
export async function up(ctx) {
const { cursor, toVersion } = ctx.toolCall.params;
const board = ctx.kv.at('app');
const page = await board.list('score:', { limit: 50, cursor: cursor ?? undefined, values: true });
for (const { key, value } of page.items ?? []) {
if (value.v === toVersion) continue; // already done: a batch can run twice
await board.put(key, { ...value, v: toVersion, points: value.score });
}
return page.cursor ? { done: false, cursor: page.cursor } : { done: true };
}ctx.toolCall.paramscarrieshook("up","down"or"post"),cursor(nullon the first batch, then whatever you returned),fromVersion,toVersionandbatch(a counter).- Return
{ done: true }when there is nothing left, or{ done: false, cursor }to be called again. The cursor must be JSON and at most 4 KB. - A run is at most 1,000 batches or 10 minutes in total. Past that the release does not go out, your place is kept, and publishing the same files again continues from the last cursor.
Write every hook so a batch can run twice. The platform stores the cursor after each batch; if something fails in between, that batch runs again.
What a hook can reach
A hook can read and write your app's own function storage (ctx.kv):
- the hook's own scope (the default
ctx.kv), - the agent scope, which all functions of the app share (scope
agent), - the
appscope of the app's own catalog entry (ctx.kv.at('app')).
Data a function stored per mounted copy, per project or per person (instance, project and visitor scopes) is not reachable from a hook. Keep data that must change shape with a release in the scopes above, or update the other scopes when a function next reads them.
Nothing else is available inside a hook: no ctx.fetch, no secrets, no files, no messages to people, no calls to other agents, no payments. A hook cannot read your app's source or change which release is live.
Order, and what happens on failure
- You publish. Every check a publish makes runs first.
- If the new release declares
upand the stored data is behind itsdataVersion,upruns todone. - Only then does the release go live.
If up fails, times out, or returns something other than { done, cursor }, the release does not go out. The release that was live stays live and you get a notification that says why.
The release itself is never half-moved, but the data can be: up runs before the release, so by the time it fails it may already have added to what the app has stored. That is one more reason up must only add, and must be safe to run again.
Because the previous release keeps serving people while up runs, and for a few minutes after, up must only add: new keys, new fields. Remove the old ones in post.
Restoring an earlier release
Restoring an earlier release runs the live release's down first, because only the newer code knows the newer shape. If the live release declares no down, or down fails, the restore stops and tells you the stored data would stay as it is. A down that failed part way may have taken some of the data back and left the rest in the newer shape. You can then confirm and restore anyway; the data stays as it is at that moment, and the platform keeps the newer version on record for the next release.
Batches of a hook count against the app owner's function usage, like any other function call of the app.
Data in the browser
Hooks change data stored on the platform. Data your app keeps in the browser (IndexedDB, localStorage) is the app's to update when it opens.
The release webhook
The owner of an app can register one https address per app. After every release (a publish or a restore), the platform sends one POST to it.
Setting it up
All four calls need the session of the app's owner. <folderId> is the id of the app's source folder.
| Call | Does |
|---|---|
PUT /api/folders/<folderId>/release-webhook with { "url": "https://..." } | Sets the address. Answers { webhook, secret }. |
POST /api/folders/<folderId>/release-webhook with { "action": "rotate" } | New secret, same address. Answers { webhook, secret }. The old secret stops working at once. |
GET /api/folders/<folderId>/release-webhook | The address and the result of the latest delivery. Never the secret. |
DELETE /api/folders/<folderId>/release-webhook | Removes it. |
The secret is shown once, in the answer to the call that created or rotated it. Store it then. If you lose it, rotate.
The address must be public https. Addresses on private networks, on this platform itself, or with a user name and password in them are refused. The app needs at least one function of its own before a webhook can be set.
What is sent
POST /your/path HTTP/1.1
Content-Type: application/json
User-Agent: Robutler-Release-Webhook/1
X-Robutler-Event: release
X-Robutler-Delivery: 6f1c0d9e-2b1a-4f7e-9c55-0d3c1c1a7e10
X-Robutler-Timestamp: 1763894400
X-Robutler-Signature: v1=5f2b...c9
{"app":"<folderId>","release":12,"root":"<64 hex>","commit":"<commit id>","environment":"production"}The body has exactly these five fields and nothing else:
| Field | Meaning |
|---|---|
app | The id of the app's source folder. |
release | The release number. |
root | The content hash of the released files. |
commit | The id of the release commit. |
environment | production, staging, dev or local. |
Verifying the signature
X-Robutler-Signature is v1= followed by the hex HMAC-SHA256, keyed with your secret, of:
<X-Robutler-Timestamp> + "." + <the raw request body>Use the body bytes exactly as received, before any JSON parsing. Refuse a timestamp more than five minutes from your clock, so a captured request cannot be replayed later.
import { createHmac, timingSafeEqual } from 'node:crypto';
export function isFromRobutler(secret, headers, rawBody) {
const ts = headers['x-robutler-timestamp'];
const got = Buffer.from(String(headers['x-robutler-signature'] ?? ''));
if (!/^\d+$/.test(ts) || Math.abs(Date.now() / 1000 - Number(ts)) > 300) return false;
const want = Buffer.from('v1=' + createHmac('sha256', secret).update(`${ts}.${rawBody}`).digest('hex'));
return want.length === got.length && timingSafeEqual(want, got);
}Delivery
- Answer with any
2xxwithin 5 seconds. Do slow work after you have answered. - A timeout, a network error,
5xx,408or429is tried again after 1, 5, 15 and 60 minutes, then dropped. Every retry carries the same body and the sameX-Robutler-Delivery, with a fresh timestamp and signature. Use the delivery id to ignore a repeat. - Any other
4xxis not retried. - Redirects are not followed. A
3xxcounts as a failed delivery. Register the final address. - A delivery never delays or undoes a release: it is sent after the release is live.