Skip to main content
New to contracts? Learn how to create a contract first.

Starting with a Contract

Contracts enforce workflow structure and validate steps automatically.
// Start with latest version
const thread = await connection.start('order_fulfillment', 'merchant');

// Or use specific version
const thread = await connection.start('order_fulfillment:2', 'merchant');

What Contracts Validate

ValidationDescription
Entry pointsFirst step must be a valid entry point
TransitionsSteps must follow allowed transitions
Required fieldsContext must include required fields
Terminal statesThread completes at terminal nodes

Recording Steps with Contracts

Contracts validate required fields automatically:
const thread = await connection.start('order_fulfillment', 'merchant');

// Contract validates this is a valid entry point
// and checks for required fields
await thread.step('order_placed')
  .addContext({ 
    order_id: order.id,        // required
    product_id: order.productId, // required
    quantity: order.quantity    // required
  })
  .success();

Handling Violations

Violations are reported via notifications but don’t block execution:
// Subscribe to violations
connection.on('rule.violated', 'order_placed', (notification) => {
  console.error('Validation violation:', notification.message);
  console.log('Severity:', notification.severity); // 'critical', 'warning', 'info'
  notification.ack();
});

// Your code continues executing
const thread = await connection.start('order_fulfillment', 'merchant');
await thread.step('invalid_step').success(); // Violation reported, but doesn't throw
Learn more about contract structure in Things to Know → Contracts

Next Steps