Managing Automations
Your application can manage automations with an access token, the same way it starts tasks. Use this when your application keeps one automation per user, for example one mailbox receiver per customer.
The SDK reads the access token as Authentication describes. The token needs the task permission on the automation’s scope:
| Operation | Permission |
|---|---|
| Create, start | Create |
| Read | Read |
| Stop | Update |
| Ack an action run | Update |
| Delete | Delete |
Automations run on the live branch only. A token made for a branch is refused with
LIVE_BRANCH_ONLY.
Creating an automation
Section titled “Creating an automation”You give the scope, a name, the trigger and the action template, and the trigger’s input. An
optional mappingFunction adapts the trigger’s output to the action’s input, as in the
wizard. A new automation is Idle: start it
when you are ready.
import { createAutomation } from '@requence/task'
const automation = await createAutomation({ id: automationId, // see "Creating safely more than once" below scope: 'project:<project-id>', name: 'Mailbox of customer 42', triggerTemplateId: '<trigger-template-id>', actionTemplateId: '<action-template-id>', input: { host: 'imap.example.com', user: 'jane', password: '…' },})from requence.task import create_automation
automation = create_automation( id=automation_id, # see "Creating safely more than once" below scope="project:<project-id>", name="Mailbox of customer 42", trigger_template_id="<trigger-template-id>", action_template_id="<action-template-id>", input={"host": "imap.example.com", "user": "jane", "password": "…"},)Before it saves anything, Requence checks that the automation can run:
- the trigger template has the Trigger role and exactly one exit node, and the action template has the Action role;
- both templates are enabled and have no errors;
- the action can take what the trigger yields — directly, or through your
mappingFunction. This is the same type check the wizard makes.
If a check fails, the create fails with AUTOMATION_INVALID_INPUT, and the message says why.
An update makes the same checks.
A secret input field is stored encrypted, exactly as when you use the wizard. The API never returns the input.
Waiting for delivery
Section titled “Waiting for delivery”Each trigger event runs the action as its own task. With requireAck, a finished action run waits
in Awaiting Delivery until your app acks it, so your app gets every result even when it was
down. It takes the same values as the delivery guarantee
of a task. The default is false: an automation that nobody watches would pile up waiting
results.
const automation = await createAutomation({ // … requireAck: 'manual', // your app acks each result after processing})automation = create_automation( # … require_ack="manual", # your app acks each result after processing)Only the action runs wait. A task that a node inside the trigger starts, or that an action run
starts, never waits. A change to requireAck reaches the action runs that start after the
change.
When you stop the automation, a running action run ends in Awaiting Delivery with the stop reason,
and your app receives it as taskAborted. Ack it like a result. An action run that already waits
keeps waiting.
Who may ack. An action run’s creator is whoever started the automation — often a user in the
Requence UI, not your app. So every access token with write access (the task Update
permission) to the automation’s scope may ack an action run. watchTasks, update.ack() and
ackTask work with your app’s token. For every other task, only the token that created it may
ack.
Receive the results. Watch the action runs with watchTasks:
each update of an action run has the automation’s task as its rootTaskId. Store your own
since and replay from it after a restart, as
After a restart explains. Requence does not send a
waiting result again by itself.
How long a result waits. A result waits until it is acked, or until the scope’s max wait for delivery has passed (30 days by default). Then it is deleted. When results wait, the automation’s run history in the Requence UI shows a warning such as “20 results waiting for delivery · the oldest expires on 18 Dec”.
What Requence cleans up
Section titled “What Requence cleans up”An automation runs as one long task, and each trigger event adds an action run and history to it. Task retention keeps both bounded:
- A finished action run is deleted after the scope’s max tasks age, and only the newest automation run history limit (1 000 by default) are kept per automation.
- An action run that waits for delivery is not deleted until it is acked or its deadline has passed. Neither is the automation’s task while it holds one.
- When an action run is deleted, the automation’s task deletes the trigger output, node history and files of that event too.
Read a result before you ack it: after the ack, retention can delete the action run and its files.
Creating safely more than once
Section titled “Creating safely more than once”A create can run twice: a retry after a timeout, a cleanup job beside your sign-up, two instances of your application for one user. Two automations on one mailbox run every action twice. To prevent this, you choose the automation’s id:
- Mint a new random uuid for each new automation, and save it in your own store first.
- Call create with that id. If the automation exists, treat it as success: an earlier attempt created it.
- Never derive the id from your own data, for example a uuid v5 of your user id. After that user is removed and added again, the derived id points at the deleted automation, and every create fails.
Requence lets exactly one create for an id through, also when two arrive at the same time.
Without an id, Requence mints one.
A create with an id fails in two ways:
| Error | Code | Meaning |
|---|---|---|
| Exists | AUTOMATION_EXISTS |
An automation has this id. For a retried create, this means it is done. |
| Deleted | AUTOMATION_DELETED |
The automation with this id was deleted. An id can never be used again: mint a new one. |
import { AutomationExistsError, createAutomation } from '@requence/task'
try { await createAutomation({ id: automationId, ...definition })} catch (error) { if (!(error instanceof AutomationExistsError)) { throw error } // created by an earlier attempt}AutomationDeletedError is the deleted case. Both extend AutomationError.
from requence.task import AutomationExistsException, create_automation
try: create_automation(id=automation_id, **definition)except AutomationExistsException: pass # created by an earlier attemptAutomationDeletedException is the deleted case. Both extend AutomationException.
Starting and stopping
Section titled “Starting and stopping”Start makes a new task for the automation and returns the automation, now Running. Stop
stops that task. Starting a running automation, or stopping one that is not running, fails
with AUTOMATION_STATE_CONFLICT.
import { startAutomation, stopAutomation } from '@requence/task'
await startAutomation(automationId)await stopAutomation(automationId)// or: startAutomation({ id: automationId, accessToken: '...' })from requence.task import start_automation, stop_automation
start_automation(automation_id)stop_automation(automation_id)# or: start_automation(automation_id, access_token="...")The task a start makes shows your access token as its creator.
Reading an automation
Section titled “Reading an automation”import { getAutomation } from '@requence/task'
const automation = await getAutomation(automationId)
automation.status // 'IDLE' | 'RUNNING' | 'STOPPED' | 'FAILED'automation.activeTaskId // the running task, or nullautomation.previousTaskIds // the tasks of earlier runs, oldest firstautomation.failureReason // why it failed, or nullautomation.outdatedReason // why it no longer matches its templates, or nullautomation.requireAck // true | false | 'manual'from requence.task import get_automation
automation = get_automation(automation_id)
automation["status"] # 'IDLE' | 'RUNNING' | 'STOPPED' | 'FAILED'automation["activeTaskId"] # the running task, or Noneautomation["previousTaskIds"] # the tasks of earlier runs, oldest firstautomation["failureReason"] # why it failed, or Noneautomation["outdatedReason"] # why it no longer matches its templates, or Noneautomation["requireAck"] # True | False | 'manual'failureReason is set only when the status is Failed. A failed automation
stays failed: start it again
yourself. outdatedReason is set when a template the automation was built from has
changed. To adopt the new templates,
delete the automation and create it again with a new id.
Use the activeTaskId with Fetching & Recreating to read the running task.
Deleting an automation
Section titled “Deleting an automation”Deleting a running automation stops its task first. What the automation kept in the store is deleted with it. Its id can never be used again.
import { deleteAutomation } from '@requence/task'
await deleteAutomation(automationId)from requence.task import delete_automation
delete_automation(automation_id)Errors
Section titled “Errors”The SDK raises a refusal as AutomationError (TypeScript) or AutomationException
(Python), with status and code:
| Status | Code | Cause |
|---|---|---|
400 |
AUTOMATION_INVALID_INPUT |
Create: the body is wrong, the id is not a uuid, a template does not exist or cannot be used, or the action cannot take what the trigger yields. Start: the input does not match the trigger’s input schema. |
400 |
LIVE_BRANCH_ONLY |
The token was made for a branch. |
403 |
FORBIDDEN |
The token does not have the permission on the scope. |
403 |
UNKNOWN |
The token is missing or not valid. |
404 |
AUTOMATION_NOT_FOUND |
No automation has this id, or it was deleted. |
409 |
AUTOMATION_EXISTS |
Create: an automation has this id. |
409 |
AUTOMATION_STATE_CONFLICT |
Start of a running automation, or stop of one that is not running. |
410 |
AUTOMATION_DELETED |
Create: the automation with this id was deleted. |
500 |
AUTOMATION_CHECK_UNAVAILABLE |
Create: Requence cannot check the types right now. Nothing was saved. Try again later. |
When Requence cannot be reached, the SDK raises the same unreachable error as for tasks:
RequenceUnreachableError (TypeScript) or RequenceUnreachableException (Python). Try again
later.