Official Node.js SDK for the ToSend email API
npm install tosendOfficial Node.js SDK for the ToSend email API.
``bash`
npm install tosend
`typescript
import { ToSend } from 'tosend';
const tosend = new ToSend('tsend_your_api_key');
const response = await tosend.send({
from: { email: 'hello@yourdomain.com', name: 'Your App' },
to: [{ email: 'user@example.com' }],
subject: 'Welcome!',
html: '
Thanks for signing up.
',console.log(response.message_id);
`
`typescript`
const response = await tosend.send({
from: { email: 'hello@yourdomain.com', name: 'Your App' },
to: [
{ email: 'user@example.com' },
{ email: 'another@example.com', name: 'John Doe' },
],
subject: 'Hello!',
html: 'Welcome
',
text: 'Welcome', // optional
});
` Hello Worldtypescript`
const response = await tosend.send({
from: { email: 'hello@yourdomain.com' },
to: [{ email: 'user@example.com' }],
cc: [{ email: 'cc@example.com' }],
bcc: [{ email: 'bcc@example.com' }],
reply_to: { email: 'support@yourdomain.com', name: 'Support' },
subject: 'Hello',
html: '
});
`typescript
import * as fs from 'fs';
const response = await tosend.send({
from: { email: 'hello@yourdomain.com' },
to: [{ email: 'user@example.com' }],
subject: 'Your Invoice',
html: '
Please find your invoice attached.
',$3
Send multiple emails in a single request:
`typescript
const response = await tosend.batch([
{
from: { email: 'hello@yourdomain.com' },
to: [{ email: 'user1@example.com' }],
subject: 'Hello User 1',
html: 'Welcome!
',
},
{
from: { email: 'hello@yourdomain.com' },
to: [{ email: 'user2@example.com' }],
subject: 'Hello User 2',
html: 'Welcome!
',
},
]);response.results.forEach((result) => {
if (result.status === 'success') {
console.log('Sent:', result.message_id);
} else {
console.log('Failed:', result.message);
}
});
`$3
`typescript
const info = await tosend.getAccountInfo();console.log(info.account.title);
console.log(info.account.emails_usage_this_month);
console.log(info.account.emails_sent_last_24hrs);
info.domains.forEach((domain) => {
console.log(domain.domain_name, domain.verification_status);
});
`Configuration
$3
`typescript
const tosend = new ToSend('tsend_your_api_key');
`$3
`typescript
const tosend = new ToSend({
apiKey: 'tsend_your_api_key',
baseUrl: 'https://api.tosend.com', // optional
timeout: 30000, // optional, in milliseconds
});
`Error Handling
`typescript
import { ToSend, ToSendError } from 'tosend';try {
const response = await tosend.send({
from: { email: 'hello@yourdomain.com' },
to: [{ email: 'user@example.com' }],
subject: 'Hello',
html: '
Hello
',
});
} catch (error) {
if (error instanceof ToSendError) {
console.log('Error:', error.message);
console.log('Code:', error.code);
console.log('Errors:', error.errors); if (error.isValidationError) {
// Handle validation error (422)
}
if (error.isAuthenticationError) {
// Handle auth error (401/403)
}
if (error.isRateLimitError) {
// Handle rate limit (429)
}
}
}
`TypeScript
Full TypeScript support with exported types:
`typescript
import {
ToSend,
ToSendError,
SendEmailParams,
SendEmailResponse,
BatchEmailResponse,
AccountInfo,
Address,
Attachment,
} from 'tosend';
``- Node.js 18 or higher
MIT