Skip to content

Context API

Every service handler receives a context object (ctx) that provides methods to access task data and control execution flow.

The input data for the current message. This is the payload that was routed to this service node based on the task template graph.

createService('1.0.0', (ctx) => {
const input = ctx.input
// input is the data routed to this service node
})

The static configuration set on the service node in the task template editor.

createService('1.0.0', (ctx) => {
const config = ctx.configuration
// config is the JSON set in the UI for this service node
})

The unique identifier of the current task execution.

A logger that sends messages to the Requence UI in real time. Available methods: log, info, warn, error.

createService('1.0.0', (ctx) => {
ctx.debug.log('Processing started')
ctx.debug.warn('Something looks off')
})

Instructs Requence to retry this service after an optional delay in milliseconds (minimum 100 ms). No code executes after this call.

createService('1.0.0', async (ctx) => {
const db = await getDbConnection()
if (!db.isConnected) {
ctx.retry(2000) // retry in 2 seconds
}
return db.query('SELECT ...')
})

Instructs Requence to abort this service immediately. If the on fail output is not connected, the entire task will fail.

createService('1.0.0', (ctx) => {
if (!ctx.input.requiredField) {
ctx.abort('Missing required field')
}
return processData(ctx.input)
})

Routes execution to a specific named output on the service node. This is used when your service has multiple outputs defined in the service definition.

createService('1.0.0', (ctx) => {
if (ctx.input.type === 'pdf') {
return ctx.toOutput('pdf', { url: '...' })
}
return ctx.toOutput('other', { raw: ctx.input })
})

Marks the current message as deferred. The service acknowledges the message but signals that the work will be completed later via the act() API. This is useful for long-running processes or external callbacks.

createService('1.0.0', (ctx) => {
const messageKey = ctx.defer('waiting for external process')
// Store messageKey to use later with service.act()
})