Starting Tasks
Once a task template is planned in the UI and services are implemented, you can start tasks programmatically.
Installation
Section titled “Installation”bash npm install @requence/task bash pip install requence Basic Usage
Section titled “Basic Usage”import { createTask } from '@requence/task'
const result = await createTask({ taskTemplate: 'my-template', input: { name: 'World' },})
console.log(result.result) // The final output of the taskfrom requence.task import Task
task = Task( task_template="my-template", input={"name": "World"},)
result = task.sync.result # Blocks until the task completesprint(result.result) # The final output of the taskTask Handle
Section titled “Task Handle”createTask / Task returns a task handle with methods to access task metadata without waiting for the task to finish:
const task = createTask({ taskTemplate: 'my-template', input: {} })
const taskId = await task.getTaskId() // Resolves as soon as the task is createdconst taskUrl = await task.getTaskUrl() // URL to view the task in the Requence UIawait task.abort('No longer needed') // Abort the running taskawait task.protect() // Protect the task from auto cleanup
// Await the final result only when neededconst result = await taskThe task handle is also an AsyncIterable — see Streaming Updates.
task = Task(task_template="my-template", input={})
# Synchronous accesstask_id = task.sync.id # Blocks until the task is createdtask_url = task.sync.url # URL to view the task in the Requence UItask.abort("No longer needed")task.protect() # Protect the task from auto cleanup
# Async accesstask_id = await task.task_idtask_url = await task.task_urlresult = await task.resultAwaiting Finalization / Validation (Cheap Monitoring)
Section titled “Awaiting Finalization / Validation (Cheap Monitoring)”By default, awaiting the task handle (await task / await task.result) or using onUpdate will start active task monitoring. Active monitoring connects to the Server-Sent Events (SSE) stream to track the progress of every node, which is expensive and often unnecessary if you only want to start the task and catch initial validation errors.
If you want to ensure the task has successfully passed input schema validation and successfully started on the backend—without incurring the overhead of monitoring the entire execution—you can await task.finalized():
const task = createTask({ taskTemplate: 'my-template', input: { /* ... */ },})
try { // Resolves when input validation passes and the task starts on the backend. // Rejects immediately with a TaskError if validation fails. const taskId = await task.finalized() console.log(`Task ${taskId} is running in the background!`)} catch (error) { console.error('Validation failed:', error.message)}task = Task(task_template="my-template", input={})
# Asynctry: task_id = await task.finalized() print(f"Task {task_id} is running in the background!")except TaskException as error: print("Validation failed:", error.message)
# Synchronoustry: task_id = task.sync.finalized() print(f"Task {task_id} is running in the background!")except TaskException as error: print("Validation failed:", error.message)Options
Section titled “Options”const result = await createTask({ taskTemplate: 'my-template', // Required — name of the task template input: { /* ... */ }, // Required if the template defines an input schema name: 'Daily Import', // Optional — human-readable task name priority: 5, // Optional — 0 (low) to 10 (high), default: 5 requireAck: true, // Optional — see Delivery Guarantee below})task = Task( task_template="my-template", # Required — name of the task template input={"key": "value"}, # Required if the template defines an input schema name="Daily Import", # Optional — human-readable task name priority=5, # Optional — 0 (low) to 10 (high), default: 5 require_ack=True, # Optional — see Delivery Guarantee below)Result
Section titled “Result”The resolved result contains:
{ input: unknown // The input you provided result: unknown // The final task output taskId: string // Unique task identifier taskUrl: string // URL to view the task in the Requence UI getNodeData(alias) // Get output from a specific node by alias getNodeError(alias) // Get error from a specific node by alias}result.input # The input you providedresult.result # The final task outputresult.task_id # Unique task identifierresult.task_url # URL to view the task in the Requence UIresult.get_node_data(alias) # Get output from a specific node by aliasresult.get_node_error(alias) # Get error from a specific node by aliasAborting a Task
Section titled “Aborting a Task”const task = createTask({ taskTemplate: 'my-template', input: {} })
// Abort after some conditionawait task.abort('No longer needed')Or use the standalone abortTask function:
import { abortTask } from '@requence/task'
await abortTask({ taskId: 'some-task-id', reason: 'Cancelled by user',})task = Task(task_template="my-template", input={})
# Abort after some conditiontask.abort("No longer needed")Or use the standalone abort_task function:
from requence.task import abort_task
abort_task(task_id="some-task-id", reason="Cancelled by user")Protecting a Task
Section titled “Protecting a Task”Protected tasks are excluded from automatic cleanup. This is useful for important task results you want to keep indefinitely.
const task = createTask({ taskTemplate: 'my-template', input: {} })
// Protect after the task is createdawait task.protect()Or use the standalone protectTask function:
import { protectTask } from '@requence/task'
await protectTask({ taskId: 'some-task-id',})task = Task(task_template="my-template", input={})
# Protect after the task is createdtask.protect()Or use the standalone protect_task function:
from requence.task import protect_task
protect_task(task_id="some-task-id")Delivery Guarantee
Section titled “Delivery Guarantee”By default a task transitions directly from RUNNING to SUCCESSFUL / FAILED / STOPPED when it finishes. If the process that started the task crashes or disconnects at the exact moment the terminal event is emitted, the event can be lost.
Setting requireAck: true (require_ack=True in Python) enables an explicit acknowledgement step:
- When the task reaches its terminal state the backend holds it in
AWAITING_DELIVERYinstead of immediately finalising it. - The SDK receives the terminal event over the SSE stream and automatically sends an ACK back to the backend.
- Only after the ACK is received does the task move to its real final status (
SUCCESSFUL,FAILED, orSTOPPED), and the exact delivery timestamp is recorded.
Because the ACK is tied to the same access token that created the task, the backend can guarantee the correct client received the result.
// The SDK handles the ACK automatically — await the result as normal.const result = await createTask({ taskTemplate: 'my-template', input: { /* ... */ }, requireAck: true,})
console.log(result.result) // Only resolves after the ACK has been confirmedtask = Task( task_template="my-template", input={}, require_ack=True,)
result = task.sync.result # Only resolves after the ACK has been confirmedrequireAck also works with watchTasks / TaskWatcher — when a task with requireAck reaches its terminal state, the watcher automatically sends the ACK on behalf of the access token that created the task.
Next Steps
Section titled “Next Steps”Learn about Streaming Updates to monitor task progress in real-time.