Overview
The Brew SDK provides methods to trigger automations and retrieve automation configuration. Automations are triggered via API with a typed payload that matches the automation’s configured schema.
New to automation triggers? Start with the Events & Automation Triggers guide to understand how triggers work, then return here for SDK-specific implementation details.
Trigger an Automation
Trigger an automation by ID with a payload:
import BrewSDK from 'brew-sdk' ;
const client = new BrewSDK ();
const result = await client . automations . trigger ( 'auto_abc123' , {
email: '[email protected] ' ,
firstName: 'John' ,
orderId: 'ORD-12345' ,
orderTotal: 99.99 ,
});
console . log ( 'Execution ID:' , result . executionId );
console . log ( 'Status:' , result . status ); // "running"
Parameters
Parameter Type Required Description automationIdstringYes The automation ID from Brew payloadobjectYes Data matching the automation’s payload schema options.metadataobjectNo Optional metadata to attach to the execution
Response
interface TriggerResponse {
success : boolean ;
executionId : string ; // Unique execution ID for tracking
automationId : string ; // The triggered automation
eventId : string ; // The event ID configured for this automation
status : 'running' ; // Initial status
contact : {
email : string ; // Contact email address
action : 'created' | 'updated' ; // Whether contact was created or updated
};
}
The contact field shows the result of the automatic contact upsert. When you trigger an automation, Brew automatically creates or updates the contact based on the email in your payload.
Get Automation Info
Retrieve an automation’s trigger configuration and payload schema:
const info = await client . automations . getTriggerInfo ( 'auto_abc123' );
console . log ( 'Automation:' , info . name );
console . log ( 'Event ID:' , info . eventDefinition ?. eventId );
console . log ( 'Schema:' , info . eventDefinition ?. payloadSchema );
Response
interface AutomationTriggerInfo {
automationId : string ;
name : string ;
status : 'draft' | 'active' | 'paused' | 'stopped' ;
isEnabled : boolean ;
isPublished : boolean ;
eventDefinition ?: {
eventId : string ;
eventName : string ;
payloadSchema ?: {
properties : Record < string , {
type : 'string' | 'number' | 'boolean' | 'object' | 'array' ;
description ?: string ;
required ?: boolean ;
}>;
required ?: string [];
};
samplePayload ?: Record < string , unknown >;
};
endpoint : string ;
}
Real-World Examples
For detailed guides on common automation scenarios (welcome emails, order confirmations, payment recovery, trial reminders), see Events & Automation Triggers .
E-commerce Order Confirmation
import BrewSDK from 'brew-sdk' ;
const client = new BrewSDK ();
async function sendOrderConfirmation ( order : Order ) {
try {
// Keep payload flat - avoid nested objects
const result = await client . automations . trigger ( 'auto_order_confirm' , {
email: order . customerEmail ,
firstName: order . customerName . split ( ' ' )[ 0 ],
orderId: order . id ,
orderTotal: order . total ,
itemCount: order . items . length ,
shippingCity: order . shippingAddress . city ,
shippingState: order . shippingAddress . state ,
});
console . log ( `Order confirmation triggered: ${ result . executionId } ` );
console . log ( `Contact ${ result . contact . action } : ${ result . contact . email } ` );
return result ;
} catch ( error ) {
if ( error instanceof BrewSDK . BadRequestError ) {
console . error ( 'Invalid payload:' , error . message );
}
throw error ;
}
}
User Onboarding Flow
async function triggerWelcomeFlow ( user : User ) {
await client . automations . trigger ( 'auto_welcome_sequence' , {
email: user . email ,
firstName: user . firstName ,
plan: user . plan ,
signupSource: user . referrer || 'direct' ,
trialEndsAt: user . trialEndDate ?. toISOString (),
});
}
Payment Failed Notification
async function notifyPaymentFailed ( subscription : Subscription ) {
await client . automations . trigger ( 'auto_payment_failed' , {
email: subscription . customerEmail ,
firstName: subscription . customerName ,
amount: subscription . amount ,
failureReason: subscription . lastFailureReason ,
retryUrl: `https://app.example.com/billing/retry/ ${ subscription . id } ` ,
attemptNumber: subscription . failedAttempts ,
});
}
Error Handling
The SDK throws typed errors for different failure scenarios:
import BrewSDK from 'brew-sdk' ;
const client = new BrewSDK ();
try {
await client . automations . trigger ( 'auto_abc123' , {
email: '[email protected] ' ,
// Missing required fields...
});
} catch ( error ) {
if ( error instanceof BrewSDK . BadRequestError ) {
// Payload validation failed
console . error ( 'Invalid payload:' , error . message );
// error.details may contain field-specific errors
} else if ( error instanceof BrewSDK . NotFoundError ) {
// Automation doesn't exist
console . error ( 'Automation not found' );
} else if ( error instanceof BrewSDK . AuthenticationError ) {
// Invalid API key
console . error ( 'Check your API key' );
} else if ( error instanceof BrewSDK . RateLimitError ) {
// Too many requests
console . error ( 'Rate limited, retry after:' , error . retryAfter );
}
}
TypeScript Types
Use the SDK’s built-in types for full type safety:
import BrewSDK from 'brew-sdk' ;
import type {
AutomationTriggerParams ,
AutomationTriggerResponse ,
AutomationTriggerInfo
} from 'brew-sdk' ;
const client = new BrewSDK ();
// Typed parameters
const params : AutomationTriggerParams = {
automationId: 'auto_abc123' ,
payload: {
email: '[email protected] ' ,
firstName: 'John' ,
},
};
// Typed response
const result : AutomationTriggerResponse =
await client . automations . trigger ( params . automationId , params . payload );
Best Practices
Use idempotency keys for critical triggers
For important automations (order confirmations, payment notifications), use idempotency to prevent duplicates: await client . automations . trigger ( 'auto_order_confirm' , payload , {
idempotencyKey: `order- ${ order . id } ` ,
});
Validate payloads before sending
Get the automation’s schema and validate locally before triggering: const info = await client . automations . getTriggerInfo ( 'auto_abc123' );
const schema = info . eventDefinition ?. payloadSchema ;
// Validate required fields
const required = schema ?. required || [];
for ( const field of required ) {
if ( ! ( field in payload )) {
throw new Error ( `Missing required field: ${ field } ` );
}
}
Always wrap trigger calls in try-catch and handle specific error types: try {
await client . automations . trigger ( automationId , payload );
} catch ( error ) {
if ( error instanceof BrewSDK . BadRequestError ) {
// Log validation errors but don't retry
logger . warn ( 'Payload validation failed' , { error });
} else {
// Retry transient errors
await retryWithBackoff (() =>
client . automations . trigger ( automationId , payload )
);
}
}
Next Steps
Need Help?
Our team is ready to support you at every step of your journey with Brew. Choose the option that works best for you: