Skip to main content

1. Install the SDK

npm install @threadify/sdk

2. Connect to Threadify

Need an API key? Generate one here to get started.
import { Threadify } from '@threadify/sdk';

// Connect with your API key and service name
const connection = await Threadify.connect('your-api-key', 'my-service');

3. Create Your First Thread

// Start tracking a workflow
const thread = await connection.start();

// Or start with a contract
const thread = await connection.start('order_fulfillment', 'merchant');

4. Add Steps to Your Process

// Create step first, then execute business logic
const orderStep = thread.step('order_received');
const order = await receiveOrder();
await orderStep.addContext({ orderId: 'ORD-123', total: 299.99 }).success();

const inventoryStep = thread.step('inventory_checked');
const inventory = await checkInventory(order.items);
await inventoryStep.addContext({ inStock: true, warehouse: 'US-EAST' }).success();

// Or chain them together
await thread.step('payment_processed')
  .addContext({ amount: 299.99, currency: 'USD' })
  .success();

5. Handle Failures

// Create step first to track execution time
const paymentStep = thread.step('payment_processed');
try {
  const payment = await processPayment(orderId);
  await paymentStep.addContext(payment).success();
} catch (error) {
  await paymentStep.failed({ 
    message: 'Payment gateway timeout',
    errorCode: 'TIMEOUT'
  });
}

6. Set Up Real-time Event Listeners (Optional)

Subscribe to WebSocket events to react to execution graph changes in real-time.
// Listen for execution events
connection.on('step.success', 'payment_processed', (event) => {
  console.log('Payment processed - update UI, send confirmation email');
  event.ack();
});

// Listen for validation violations
connection.on('rule.violated', 'inventory_check', (event) => {
  console.error('Inventory violation detected - escalate to ops');
  event.ack();
});

// Contract-specific events
connection.on('rule.violated', 'product_delivery@order_placed', (event) => {
  console.log('Product delivery rule violated - notify logistics');
  event.ack();
});

What’s Next?

Need Help?