autosend-sdk
v0.0.2
Published
Handfull utility to interact with the autosend api
Maintainers
Readme
AutoSend
A TypeScript SDK for the AutoSend API - a simple and powerful email sending service.
Installation
npm install autosend-sdkQuick Start
import { AutoSendClient } from 'autosend-sdk';
// Initialize the client
const client = new AutoSendClient({
apikey: 'your-api-key-here'
});
// Send an email
const result = await client.sendEmail({
to: { email: '[email protected]', name: 'John Doe' },
from: { email: '[email protected]', name: 'Your App' },
subject: 'Hello from AutoSend!',
html: '<h1>Welcome!</h1><p>This is a test email.</p>'
});
console.log(result); // { emailId: '...', status: 'sent' }Usage
Initialize Client
import { AutoSendClient } from 'autosend-sdk';
const client = new AutoSendClient({
apikey: 'your-api-key-here',
baseUrl: 'https://api.autosend.com/v1' // optional, defaults to this
});Send Single Email
// Basic email
await client.sendEmail({
to: { email: '[email protected]', name: 'User Name' },
from: { email: '[email protected]', name: 'Your App' },
subject: 'Welcome!',
html: '<p>Welcome to our service!</p>'
});
// With template
await client.sendEmail({
to: { email: '[email protected]' },
from: { email: '[email protected]' },
templateId: 'welcome-template',
dynamicData: {
userName: 'John',
activationLink: 'https://yourapp.com/activate'
}
});
// With additional options
await client.sendEmail({
to: { email: '[email protected]', name: 'User' },
from: { email: '[email protected]', name: 'Your App' },
subject: 'Newsletter',
html: '<p>Check out our latest updates!</p>',
text: 'Check out our latest updates!', // plain text version
replyTo: { email: '[email protected]', name: 'Support' },
unsubscribeGroupId: 'newsletter-group'
});Send Bulk Emails
await client.sendBulkEmail({
recipients: [
{ email: '[email protected]', name: 'User 1' },
{ email: '[email protected]', name: 'User 2' },
{ email: '[email protected]', name: 'User 3' }
],
from: { email: '[email protected]', name: 'Your App' },
subject: 'Important Update',
html: '<p>We have an important update for you!</p>',
templateId: 'bulk-template', // optional
dynamicData: { // optional, same data for all recipients
companyName: 'Your Company'
}
});
// Returns:
// {
// batchId: '...',
// totalRecipients: 3,
// successCount: 3,
// failedCount: 0
// }Manage Contacts
Create Contact
await client.createContact({
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
userId: 'user-123', // optional, your internal user ID
customFields: { // optional
plan: 'premium',
signupDate: '2024-01-01'
}
});Upsert Contact
Create a new contact or update if email already exists:
await client.upsertContact({
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
customFields: {
lastLogin: '2024-11-09'
}
});Remove Contacts
await client.removeContacts({
emails: ['[email protected]', '[email protected]']
});
// Returns: { success: true }Error Handling
All methods throw AutoSendError on failure:
import { AutoSendClient, AutoSendError } from 'autosend-sdk';
const client = new AutoSendClient({ apikey: 'your-key' });
try {
await client.sendEmail({
to: { email: '[email protected]' },
from: { email: '[email protected]' },
subject: 'Test',
html: '<p>Test</p>'
});
} catch (error) {
if (error instanceof AutoSendError) {
console.error('AutoSend Error:', error.message);
console.error('Status Code:', error.statusCode);
console.error('Response:', error.response);
}
}TypeScript Support
This package is written in TypeScript and includes full type definitions:
import {
AutoSendClient,
SendEmailParams,
SendEmailResponse,
CreateContactParams,
Contact,
AutoSendError
} from 'autosend-sdk';API Reference
AutoSendClient
Constructor Options
apikey(required): Your AutoSend API keybaseUrl(optional): API base URL, defaults tohttps://api.autosend.com/v1
Methods
sendEmail(params: SendEmailParams): Promise<SendEmailResponse>- Send a single email
sendBulkEmail(params: SendBulkEmailParams): Promise<SendBulkEmailResponse>- Send emails to multiple recipients
createContact(params: CreateContactParams): Promise<CreateContactResponse>- Create a new contact
upsertContact(params: CreateContactParams): Promise<CreateContactResponse>- Create or update a contact by email
removeContacts(params: RemoveContactsParams): Promise<RemoveContactsResponse>- Remove contacts by email addresses
License
MIT
Author
Sahil Khan
