Schedules and other triggers
Schedules, credits running low, a run starting, record changes, and a call from your app that charges credits.
Schedules
{ "kind": "schedule", "every": "1d", "at": "09:00" }. every
is one of 15m, 30m, 1h, 6h,
12h, 1d or 1w, so no schedule runs more often than every
fifteen minutes. at is a UTC time (00:00 when unset) and pairs with
1d or 1w; 1w also needs on, the weekday,
in lowercase ("monday" to "sunday"). A weekly digest is one relay:
{ "kind": "schedule", "every": "1w", "on": "monday", "at": "09:00" }.
A schedule ticks once per period. A relay created mid-period first fires next period. After a gap, the most recent missed period runs late, and older missed periods land as dead events you can replay.
A schedule has no person, so grant_access, revoke_access,
email_person and to: "person" are refused on it when the file
is validated.
Credits running low
{ "kind": "credits_low", "below": 20 }. It runs once each time a person's
spendable credits fall from below or more to under it: a spend, a refund's
clawback, an expiry, or a reservation for a run. It never runs on a rise, so a person who
tops up and runs low again triggers it again. below is a whole number from 1
to 1,000,000,000. It records one event per crossing per relay.
The person is the event's person, so email_person and
grant_credits have someone to act on. The payload's event is
{ balance, before, below }, read in templates as
{{event.balance}}.
A grant_credits action on this trigger refills the person. This repeats
every time the person runs low.
A run starting
{ "kind": "run_started", "tool": "poster" }. It runs once when a person
starts a run on any tool (every one, or the one tool names), whatever
its provider. It reacts to the run and takes no part in delivering it. The person is the
run's person, so email_person has someone to write to. The payload's
event carries the run, read in templates as {{event.run.id}} and
{{event.run.tool}}. It records one event per run per relay, ever.
Person actions work on it: an email_person notice, or
start_run to chain the next tool for the same person. A tool on provider
external sends its own run to your pipeline without a relay (see
External tools), so this trigger needs no action beside it.
Record changes
{ "kind": "data_change", "collection": "bookings", "on": ["created"], "where": { "status": "won" } }.
on is any of created, updated and
deleted. where filters the record's own data fields: per field, a
literal (an exact match) or an object of operators, the list's own filter grammar (see
Data).
published filters on whether the record is public after the write:
"published": false fires only for drafts (on a collection whose posts wait for
approval, a post that is waiting for you), "published": true only once the
record is public. Leave it out to hear both. It is a separate key, not part of
where, because published is not one of the record's data fields.
{ "kind": "data_change", "collection": "posts", "on": ["created", "updated"], "published": false }
tells you each time a post starts waiting. Any value other than true or
false is refused when the relay is saved.
openField names one of the collection's open fields (a like, a vote, an
acknowledgement) and fires only when a reader changes that field; it needs
updated in on. direction narrows it to
"added", "taken back" or "set". A trigger with
openField never fires on content edits, and one without it never fires on
open-field changes.
changed fires once, exactly on the write that moves a declared
field to to and it was not already there — { "field":
"status", "to": "confirmed", "from": ["pending"] }. Leave out
from for "from any value". It needs updated in
on, and does not mix with openField (an open field
already has direction for this — name one or the other). Unlike a
plain where, which matches every write that leaves the field at
its target value, changed fires only on the crossing: a later
edit that leaves the field at to never fires it again, and a
write outside the declared from never fires it either. A
reader's own open-field change (a status they set themself) fires it too, the
same as any other write that moves the field.
It fires for writes from your app, from the dashboard's editor and from Stripe
receipts, at most once per write per relay. It never fires for a relay's own writes, and
never for an account erasure, so an erased person's data never fans out. A
call_url that writes back through the SDK does fire it, so avoid a loop
through your own host. A definition change applies within five seconds. The person is
the record's owner, otherwise its recipient.
Authorise on fields the person cannot set. A
where on a field the signed-in person can write is a self-service grant: they
set status to won and receive Pro. Filter on a field only the
owner or the dashboard writes, or grant from a receiver, where the provider's signature
is the proof.
A call from your app
{ "kind": "app_call", "who": "signed_in", "perMinute": 30 }. Your app calls
the relay by name, and the relay runs while the app waits for the answer. Use it when a
person's action in your app costs credits: booking a class, unlocking a post, sending a
message. who is "signed_in" (the default) or "anyone".
perMinute lowers how many calls one person may make each minute; it is 30 when
unset and cannot go higher.
The caller is the signed-in person from their session, never a field in the body.
{{event.input.<path>}} reads what the app sent. A relay with this trigger
never grants or takes what a person holds (access, plans, products or credits granted). Its
one money action is spend_credits,
which takes the caller's own credits. With "anyone" the call has no person, so
spend_credits and every action that acts on a person are refused when the file
is validated.
A studio that charges one credit per class booking is one file:
{
"name": "book-class",
"trigger": { "kind": "app_call" },
"actions": [
{ "type": "spend_credits", "amount": 1, "reason": "Class booking" },
{ "type": "write_record", "collection": "bookings", "data": { "class": "{{event.input.class}}" } },
{ "type": "email_person", "subject": "You're booked", "text": "See you at {{event.input.class}}." }
]
}
The app calls it before anything is written. The web, Expo and Swift SDKs all do it the same way, and the app holds no secret key:
const { records, balance } = await g.relays.call("book-class", { class: classId }, { key: tapId });
Make key once per tap (crypto.randomUUID()) and send the same
one on a retry. The same run answers with deduped: true: one booking, one
charge. The answer is 200 { ok, spent, balance, records, run }.
spent and balance are there when the relay charges.
What the charge pays for
spend_credits must be the first action, and it must be followed by an action
that creates something for the caller: a write_record. Until that action
succeeds, a failure puts the credits back once; the ledger line reads "refunded: <relay>
didn't finish". After it succeeds the charge stands, and every later action (the email, your
URL, more records) is a follow-up that retries and never refunds. A file where
spend_credits pays for nothing, or where another action sits between the charge
and what it pays for, is refused when it is saved.
What the app is told
| Code | Status | Meaning · what to do |
|---|---|---|
session_required | 401 | The relay’s who is "signed_in" (the default) and nobody is signed in. Sign in first |
credits_exhausted | 402 | The person has too few credits. The answer carries their balance, nothing was written, and the relay sent no alert. Show your top-up screen |
not_callable | 404 | No relay of that name has an app_call trigger |
call_in_progress | 202 | This key's call is still working. Ask again with the same key, never a new one |
call_not_finished | 409 | This key's call ended without charging, or was refunded. Send a new key |
invalid_body | 400 | The body is not a JSON object of input and key, both optional |
invalid_key | 400 | The key is over 200 characters, or, on a relay anyone may call, shorter than 16 random characters |
input_too_large | 413 | A call to an app_call relay sent an input over 16 KB, or a body over 32 KB |
too_many_calls | 429 | One person, or the app, passed the calls it may make in a minute. resetAt says when |
daily_cap | 429 | The app has taken all the calls it can today. Try again tomorrow |
relay_failed | 502 | What the charge paid for failed. refunded says the credits came back |
relay_paused | 503 | The relay is paused. Nothing was charged |
scope_denied | 403 | A secret key made the call. Your server uses the server methods instead |
A person gets 30 calls a minute per relay, and the app gets Development 60 a minute and 2,000 a day, Live 1,200 a minute and 200,000 a day. Calls from people who are not signed in get a quarter of the app’s limits. Every limit is checked before anything is written or charged. npx gemmein dev skips the app’s limits and keeps the 30 a minute per person.
What happens if…
- The connection drops and the app retries with the same key: the same run answers, and
nothing runs or is charged twice. If the first call is still working, the retry waits up
to 8 seconds, then answers
call_in_progress. - Gemmein restarts between the charge and the booking: the call is written down before the charge, so a recovery pass finds it within minutes. With no booking the credits come back once; with the booking written the charge stands and the follow-ups continue.
- A follow-up fails after the booking was saved: the person keeps what they paid for and
the app gets
200withfollowUps: "retrying"(or"failed"when a retry cannot help). It retries on the same ladder as every relay. - Two people use the same key: two runs. A key belongs to the person who sent it.
- You replay an app call from the dashboard: refused with
not_replayable. The app calls again. - The person deletes their account: their app-call runs, the records the relay wrote for them and their ledger lines go with them.
- The same relay in Development and Live: the same rules in each, and a promote carries the relay into Live like any other.
Thanks for telling us.
Thanks for telling us.
Last updated