Skip to main content
defineWorkflowBlockExecute wires an execute handler to a step block. The handler runs each time a workflow run reaches this step.

Parameters

block
WorkflowBlock
required
The block returned by defineWorkflowBlock in block.ts.
execute
function
required
The handler function. Receives a context object with config (typed from the block’s configSchema) and metadata.metadata provides context about the current run and workflow:
FieldTypeDescription
workflowIdstringID of the workflow this block belongs to.
workflowVersionIdstringID of the active workflow version.
workflowBlockIdstringID of this block instance within the workflow.
workflowRunIdstringID of the current workflow run.
uniqueExecutionIdstringUnique ID for this specific execution of the block. Use for idempotency.
workflowTitlestringHuman-readable title of the workflow.
workflowUrlstringLink to the workflow in the Attio UI.
runUrlstringLink to the current run in the Attio UI.
finishCallbackUrlstringURL to POST to when finishing a deferred execution. Only relevant when the handler returns {type: "defer"}. See Finishing a deferred step.

Example

execute.ts
import {Workflows} from "attio/server"
import block from "./block"

export default Workflows.defineWorkflowBlockExecute(block, async ({config, metadata}) => {
  const response = await fetch("https://api.example.com/jobs", {
    method: "POST",
    body: JSON.stringify({email: config.recipient_email}),
  })

  if (!response.ok) {
    return {
      type: "error",
      errorMessage: `Request failed: ${response.statusText}`,
      retryable: response.status >= 500,
    }
  }

  const {jobId, shouldSkip} = await response.json()

  if (shouldSkip) {
    return {type: "exit"}
  }

  if (jobId) {
    // Pause and wait for a POST to metadata.finishCallbackUrl
    return {type: "defer"}
  }

  return {type: "outcome", id: "success", data: null}
})

See also