1. Install the SDK
- JavaScript
- Go
npm install @threadify/sdk
go get github.com/ThreadifyDev/go-sdk
2. Connect to Threadify
Need an API key? Generate one here to get started.
- JavaScript
- Go
import { Threadify } from '@threadify/sdk';
// Connect with your API key and service name
const connection = await Threadify.connect('your-api-key', 'my-service');
ctx := context.Background()
conn, err := threadify.Connect(ctx, "your-api-key",
threadify.WithServiceName("my-service"),
)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
3. Create Your First Thread
- JavaScript
- Go
// Start tracking a workflow
const thread = await connection.start();
// Or start with a contract
const thread = await connection.start('order_fulfillment', 'merchant');
// Start tracking a workflow
thread, err := conn.Start(ctx)
if err != nil {
log.Fatal(err)
}
// Or start with a contract
thread, err := conn.Start(ctx,
threadify.WithContract("order_fulfillment"),
threadify.WithRole("merchant"),
)
if err != nil {
log.Fatal(err)
}
4. Add Steps to Your Process
- JavaScript
- Go
// 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();
// Create step first, then execute business logic
orderStep := thread.Step("order_received")
order, err := receiveOrder()
if err != nil {
log.Fatal(err)
}
_, err = orderStep.AddContext(map[string]any{"orderId": "ORD-123", "total": 299.99}).Success(ctx)
if err != nil {
log.Fatal(err)
}
inventoryStep := thread.Step("inventory_checked")
inventory, err := checkInventory(order.Items)
if err != nil {
log.Fatal(err)
}
_, err = inventoryStep.AddContext(map[string]any{"inStock": true, "warehouse": "US-EAST"}).Success(ctx)
if err != nil {
log.Fatal(err)
}
// Or chain them together
_, err = thread.Step("payment_processed").
AddContext(map[string]any{"amount": 299.99, "currency": "USD"}).
Success(ctx)
if err != nil {
log.Fatal(err)
}
5. Handle Failures
- JavaScript
- Go
// 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'
});
}
// Create step first to track execution time
paymentStep := thread.Step("payment_processed")
payment, err := processPayment(orderId)
if err != nil {
_, stepErr := paymentStep.Failed(ctx, map[string]any{
"message": "Payment gateway timeout",
"errorCode": "TIMEOUT",
})
if stepErr != nil {
log.Fatal(stepErr)
}
return
}
_, err = paymentStep.AddContext(payment).Success(ctx)
if err != nil {
log.Fatal(err)
}
6. Set Up Real-time Event Listeners (Optional)
Subscribe to WebSocket events to react to execution graph changes in real-time.- JavaScript
- Go
// 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();
});
// Listen for execution events
err := conn.Subscribe(ctx, "step.success", "payment_processed", func(n *threadify.Notification) {
fmt.Println("Payment processed - update UI, send confirmation email")
n.Ack()
})
if err != nil {
log.Fatal(err)
}
// Listen for validation violations
err = conn.Subscribe(ctx, "rule.violated", "inventory_check", func(n *threadify.Notification) {
fmt.Println("Inventory violation detected - escalate to ops")
n.Ack()
})
if err != nil {
log.Fatal(err)
}
// Contract-specific events
err = conn.Subscribe(ctx, "rule.violated", "product_delivery@order_placed", func(n *threadify.Notification) {
fmt.Println("Product delivery rule violated - notify logistics")
n.Ack()
})
if err != nil {
log.Fatal(err)
}
What’s Next?
- Installation Guide - Detailed setup for production
- API Reference - Complete API documentation
- SDK Guide - Advanced SDK features
- Creating Contracts - Define business rules
Need Help?
- Check our troubleshooting guide
- Visit our GitHub discussions
- Contact support at support@threadify.com