Examples
Typescript
Python
- Python Integration Overview
- Examples
Javascript
- JavaScript Overview
- Examples
Go
- Go Integration Overview
- Examples
PHP
- PHP Integration Overview
- Examples
cURL
Examples
TypeScript Client Library
Using the Shoutbox TypeScript client library
TypeScript Client Library
Our TypeScript client library provides a fully typed interface for sending emails through Shoutbox.net.
Installation
Copy
npm install shoutboxnet
# or
yarn add shoutboxnet
# or
pnpm add shoutboxnet
Using the Client Library
Basic Usage
Copy
import Shoutbox from 'shoutboxnet';
import { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emailOptions: EmailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Hello from TypeScript!',
html: '<h1>Hello!</h1><p>This is a test email.</p>'
};
try {
await client.sendEmail(emailOptions);
console.log('Email sent successfully');
} catch (error) {
console.error('Failed to send email:', error);
}
With Multiple Recipients
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emailOptions: EmailOptions = {
from: '[email protected]',
to: ['[email protected]', '[email protected]'],
subject: 'Group Announcement',
html: '<h1>Hello Team!</h1><p>This is a group announcement.</p>'
};
await client.sendEmail(emailOptions);
With Attachments
Copy
import Shoutbox, { EmailOptions, Attachment } from 'shoutboxnet';
import { readFileSync } from 'fs';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const attachment: Attachment = {
filepath: './document.pdf',
filename: 'report.pdf',
contentType: 'application/pdf'
};
const emailOptions: EmailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Report Attached',
html: '<h1>Monthly Report</h1><p>Please find the report attached.</p>',
attachments: [attachment]
};
await client.sendEmail(emailOptions);
With Custom Headers
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emailOptions: EmailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Custom Headers Example',
html: '<h1>Hello!</h1>',
headers: {
'X-Custom-Header': 'custom-value',
'X-Campaign-ID': 'campaign-123'
}
};
await client.sendEmail(emailOptions);
With CC Recipients
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emailOptions: EmailOptions = {
from: '[email protected]',
to: '[email protected]',
cc: ['[email protected]', '[email protected]'],
subject: 'CC Example',
html: '<h1>Hello!</h1><p>This email has CC recipients.</p>'
};
await client.sendEmail(emailOptions);
Sending Multiple Emails
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emails: EmailOptions[] = [
{
from: '[email protected]',
to: '[email protected]',
subject: 'Email 1',
html: '<h1>First Email</h1>'
},
{
from: '[email protected]',
to: '[email protected]',
subject: 'Email 2',
html: '<h1>Second Email</h1>'
}
];
await client.sendEmails(emails);
With React Email Templates
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
import { WelcomeTemplate } from './emails/WelcomeTemplate';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
const emailOptions: EmailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome!',
react: <WelcomeTemplate name="John" />
};
await client.sendEmail(emailOptions);
Error Handling
Copy
import Shoutbox, { EmailOptions } from 'shoutboxnet';
const client = new Shoutbox(process.env.SHOUTBOX_API_KEY);
async function sendEmailWithRetry(options: EmailOptions, maxRetries: number = 3): Promise<void> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
await client.sendEmail(options);
return;
} catch (error) {
if (attempt === maxRetries) {
throw error;
}
console.log(`Attempt ${attempt} failed, retrying...`);
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
}
}
}
// Usage
try {
await sendEmailWithRetry({
from: '[email protected]',
to: '[email protected]',
subject: 'Test with Retry',
html: '<h1>Hello!</h1>'
});
} catch (error) {
console.error('All retry attempts failed:', error);
}
Type Definitions
EmailOptions Interface
Copy
interface EmailOptions {
from: string;
to: string | string[];
subject: string;
html?: string;
text?: string;
react?: React.ReactElement;
name?: string;
replyTo?: string;
attachments?: Attachment[];
headers?: Record<string, string>;
cc?: string | string[];
}
Attachment Interface
Copy
interface Attachment {
filename?: string;
filepath: string;
contentType?: string;
content?: string | Buffer;
}
Environment Variables
Copy
// .env
SHOUTBOX_API_KEY=your-api-key-here
// Usage
import { config } from 'dotenv';
import Shoutbox from 'shoutboxnet';
config();
const apiKey = process.env.SHOUTBOX_API_KEY;
if (!apiKey) {
throw new Error('SHOUTBOX_API_KEY is required');
}
const client = new Shoutbox(apiKey);
Rate Limits
Please contact support for information about rate limits for your API key.
Next Steps
Support
For additional support or questions, please contact our support team.
On this page
- TypeScript Client Library
- Installation
- Using the Client Library
- Basic Usage
- With Multiple Recipients
- With Attachments
- With Custom Headers
- With CC Recipients
- Sending Multiple Emails
- With React Email Templates
- Error Handling
- Type Definitions
- EmailOptions Interface
- Attachment Interface
- Environment Variables
- Rate Limits
- Next Steps
- Support
Assistant
Responses are generated using AI and may contain mistakes.