Skip to main content
Conditions are regular iii functions that the engine calls before a trigger’s handler runs. Attach a condition to any trigger by adding the condition function’s ID to the trigger config. If the condition returns false, the handler function is not called.
Condition functions are registered with registerFunction / register_function like any other function. They receive the same event data as the handler and must return a boolean.

HTTP trigger condition

The condition function receives the full API request. If it returns false, the engine responds with 422 Unprocessable Entity and the handler function is not called.
import { registerWorker, Logger, TriggerAction } from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

iii.registerFunction(
  { id: 'conditions::is_premium' },
  async (req: ApiRequest<{ amount: number }>) => {
    return (req.body?.amount ?? 0) > 1000
  },
)

iii.registerFunction(
  { id: 'orders::premium', description: 'Processes premium orders' },
  async (req: ApiRequest<{ amount: number; description: string }>) => {
    const logger = new Logger()
    logger.info('Processing premium order', { amount: req.body?.amount })
    return { status_code: 200, body: { message: 'Premium order processed' } }
  },
)

iii.registerTrigger({
  type: 'http',
  function_id: 'orders::premium',
  config: {
    api_path: 'orders/premium',
    http_method: 'POST',
    condition_function_id: 'conditions::is_premium',
  },
})

Queue trigger condition

The condition function receives the queue message data. If it returns false, the handler function is not called — no error is raised.
iii.registerFunction(
  { id: 'conditions::is_high_value' },
  async (data: { amount: number }) => {
    return data.amount > 1000
  },
)

iii.registerFunction(
  { id: 'orders::high_value', description: 'Processes high-value orders from queue' },
  async (data: { amount: number; description: string }) => {
    const logger = new Logger()
    logger.info('Processing high-value order', { amount: data.amount })

    await iii.trigger({
      function_id: 'enqueue',
      payload: { topic: 'order.processed', data: { ...data, processedAt: new Date().toISOString() } },
      action: TriggerAction.Void(),
    })
  },
)

iii.registerTrigger({
  type: 'queue',
  function_id: 'orders::high_value',
  config: { topic: 'order.created', condition_function_id: 'conditions::is_high_value' },
})

State trigger condition

The condition receives the full state event, including the event type (created, updated, deleted), scope, key, and both old and new values.
iii.registerFunction(
  { id: 'conditions::is_update' },
  async (event: { event_type: string; scope: string; key: string; old_value: unknown; new_value: unknown }) => {
    return event.event_type === 'updated'
  },
)

iii.registerFunction(
  { id: 'orders::on_update', description: 'Reacts to order state updates' },
  async (event) => {
    const logger = new Logger()
    logger.info('Order updated', { scope: event.scope, key: event.key })
  },
)

iii.registerTrigger({
  type: 'state',
  function_id: 'orders::on_update',
  config: { scope: 'orders', condition_function_id: 'conditions::is_update' },
})

Key concepts

  • Condition functions are registered with registerFunction / register_function like any other function. They receive the same event data as the handler and must return true or false.
  • Add condition_function_id to the trigger config with the condition function’s ID. This key is the same for all trigger types.
  • When a condition returns false:
    • HTTP — the engine responds with 422 Unprocessable Entity; the handler function is not called.
    • Queue / Cron — the handler function is not called; no error is surfaced.
    • State / Stream — the handler function is not called.
  • If a condition function errors, the engine logs the error and does not call the handler (HTTP returns 500 Internal Server Error).
  • A single condition function can be shared across multiple triggers.