Overview
The Transactional resource allows you to send transactional emails using your published Brew templates. Access it via client.send.transactional.
Parameter Names: Both the SDK and REST API use chatId for template IDs and variables for template data.
Methods
Method Description send.transactional.send()Send a transactional email send.transactional.documentation()Get API documentation info
Send Transactional Email
Send a transactional email using a published email template.
The template must be published (status: “live”) in Brew before it can be used via API.
Basic Usage
import BrewSDK from 'brew-sdk' ;
const client = new BrewSDK ();
const response = await client . send . transactional . send ({
chatId: 'your-template-id' ,
to: 'user@example.com' ,
variables: {
firstName: 'John' ,
orderNumber: 'ORD-12345' ,
},
});
console . log ( 'Emails sent:' , response . data . sent );
Send to Multiple Recipients
Send the same email to multiple recipients (up to 1000):
const response = await client . send . transactional . send ({
chatId: 'announcement-template' ,
to: [
'user1@example.com' ,
'user2@example.com' ,
'user3@example.com' ,
],
variables: {
announcement: 'New feature launched!' ,
ctaUrl: 'https://example.com/new-feature' ,
},
});
console . log ( `Sent: ${ response . data . sent } , Failed: ${ response . data . failed } ` );
Override Sender and Subject
const response = await client . send . transactional . send ({
chatId: 'order-confirmation' ,
to: 'customer@example.com' ,
from: 'orders@yourdomain.com' ,
replyTo: 'support@yourdomain.com' ,
subject: 'Your Order #12345 is Confirmed!' ,
variables: {
orderNumber: '12345' ,
orderTotal: '$99.99' ,
deliveryDate: 'January 25, 2024' ,
},
});
With Request Options
You can pass additional options like custom timeouts or retries:
const response = await client . send . transactional . send (
{
chatId: 'urgent-notification' ,
to: 'user@example.com' ,
variables: { message: 'Important update' },
},
{
timeout: 10000 , // 10 second timeout
maxRetries: 5 , // Retry up to 5 times
}
);
Parameters
The transactional email template ID from Brew.
to
string | string[]
required
Recipient email address(es). Can be a single email or an array of up to 1000 emails.
Template variables as key-value pairs. These populate dynamic content in your email template.
Override the default sender email address.
Set the reply-to email address.
Override the default subject line.
Response
interface TransactionalSendResponse {
success : true ;
data : {
sent ?: number ; // Number of emails sent successfully
failed ?: number ; // Number of emails that failed
results ?: Array <{
email ?: string ;
id ?: string ; // Email ID (if successful)
error ?: string ; // Error message (if failed)
}>;
};
meta ?: {
requestId ?: string ;
};
}
Get API Documentation
Retrieve API documentation and endpoint information:
const docs = await client . send . transactional . documentation ();
console . log ( 'API Version:' , docs . version );
console . log ( 'Documentation:' , docs . documentation );
docs . endpoints ?. forEach ( endpoint => {
console . log ( ` ${ endpoint . methods ?. join ( ', ' ) } ${ endpoint . path } ` );
console . log ( ` ${ endpoint . description } ` );
});
Response
interface TransactionalDocumentationResponse {
version ?: string ; // API version
documentation ?: string ; // Link to full docs
endpoints ?: Array <{
path ?: string ;
methods ?: string [];
description ?: string ;
}>;
}
Common Use Cases
Password Reset Email
const response = await client . send . transactional . send ({
chatId: 'password-reset-template' ,
to: user . email ,
variables: {
userName: user . firstName ,
resetLink: `https://app.example.com/reset?token= ${ token } ` ,
expiryHours: 24 ,
},
});
Order Confirmation
const response = await client . send . transactional . send ({
chatId: 'order-confirmation-template' ,
to: order . customerEmail ,
subject: `Order Confirmed - # ${ order . id } ` ,
variables: {
orderNumber: order . id ,
orderTotal: formatCurrency ( order . total ),
items: order . items . map ( item => ({
name: item . name ,
quantity: item . quantity ,
price: formatCurrency ( item . price ),
})),
shippingAddress: order . shippingAddress ,
estimatedDelivery: order . estimatedDelivery ,
},
});
Welcome Email
const response = await client . send . transactional . send ({
chatId: 'welcome-email-template' ,
to: newUser . email ,
variables: {
firstName: newUser . firstName ,
dashboardUrl: 'https://app.example.com/dashboard' ,
helpCenterUrl: 'https://help.example.com' ,
trialDays: 14 ,
},
});
Team Invitation
const response = await client . send . transactional . send ({
chatId: 'team-invitation-template' ,
to: invitee . email ,
replyTo: inviter . email ,
variables: {
inviterName: inviter . name ,
teamName: team . name ,
inviteLink: `https://app.example.com/invite/ ${ inviteToken } ` ,
expiryDays: 7 ,
},
});
Handling Results
Check Individual Results
When sending to multiple recipients, check each result:
const response = await client . send . transactional . send ({
chatId: 'newsletter-template' ,
to: [ 'user1@example.com' , 'user2@example.com' , 'invalid-email' ],
variables: { /* ... */ },
});
// Log successful sends
response . data . results ?. forEach ( result => {
if ( result . id ) {
console . log ( `Sent to ${ result . email } : ID ${ result . id } ` );
} else {
console . error ( `Failed for ${ result . email } : ${ result . error } ` );
}
});
// Summary
console . log ( `Total: ${ response . data . sent } sent, ${ response . data . failed } failed` );
Type Definitions
Import types for better TypeScript support:
import BrewSDK from 'brew-sdk' ;
// Parameter types
type TransactionalSendParams = BrewSDK . Send . TransactionalSendParams ;
// Response types
type TransactionalSendResponse = BrewSDK . Send . TransactionalSendResponse ;
type TransactionalDocumentationResponse = BrewSDK . Send . TransactionalDocumentationResponse ;
// Example with typed params
const params : TransactionalSendParams = {
chatId: 'template-id' ,
to: 'user@example.com' ,
variables: {
name: 'John' ,
},
};
const response : TransactionalSendResponse =
await client . send . transactional . send ( params );
Error Handling
import BrewSDK from 'brew-sdk' ;
const client = new BrewSDK ();
try {
await client . send . transactional . send ({
chatId: 'invalid-template-id' ,
to: 'user@example.com' ,
});
} catch ( error ) {
if ( error instanceof BrewSDK . NotFoundError ) {
console . error ( 'Template not found or not published' );
} else if ( error instanceof BrewSDK . BadRequestError ) {
console . error ( 'Invalid request:' , error . message );
} else if ( error instanceof BrewSDK . RateLimitError ) {
console . error ( 'Rate limited - try again later' );
} else {
throw error ;
}
}
Best Practices
Use meaningful template IDs
Name your templates descriptively in Brew so the IDs are easy to identify:
welcome-email
password-reset
order-confirmation
Validate variables before sending
Ensure all required template variables are present: function sendOrderConfirmation ( order : Order ) {
if ( ! order . id || ! order . customerEmail || ! order . total ) {
throw new Error ( 'Missing required order data' );
}
return client . send . transactional . send ({
chatId: 'order-confirmation' ,
to: order . customerEmail ,
variables: {
orderNumber: order . id ,
orderTotal: order . total ,
},
});
}
Handle failures gracefully
Always check results and handle failures: const response = await client . send . transactional . send ( params );
if ( response . data . failed && response . data . failed > 0 ) {
// Log failures for investigation
const failures = response . data . results ?. filter ( r => r . error );
console . error ( 'Email failures:' , failures );
}
Use reply-to for support emails
Set a reply-to address so customer replies go to the right place: await client . send . transactional . send ({
chatId: 'support-response' ,
to: customer . email ,
replyTo: 'support@yourcompany.com' ,
// ...
});
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: