Send emails with Node.js
Learn how to send your first email using the YourSend Node.js SDK.
Prerequisites
To get the most out of this guide, you'll need to:
1. Install
Get the YourSend Node.js SDK.
npm install yoursend2. Send email using HTML
The easiest way to send an email is by using the html parameter.
index.ts
import { YourSend } from 'yoursend';
const ys = new YourSend('ys_live_...');
const { data, error } = await ys.send({
channel: 'email',
from: 'hello@yourdomain.com',
to: 'user@example.com',
subject: 'Hello from YourSend',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
});
if (error) {
console.error('Failed to send:', error);
} else {
console.log('Message sent:', data.message_id);
}3. Send using a template
Use pre-built templates with merge tags for dynamic content.
send-template.ts
const { data, error } = await ys.send({
channel: 'email',
to: 'user@example.com',
template_id: 'tmpl_welcome_email',
tags: {
name: 'Alex',
company: 'Acme Inc',
},
});4. Try it yourself
You can also test the API directly using cURL:
curl -X POST https://api.yoursend.dev/v1/send \
-H "Authorization: Bearer ys_live_..." \
-H "Content-Type: application/json" \
-d '{
"channel": "email",
"to": "user@example.com",
"subject": "Test email",
"html": "<h1>It works!</h1>"
}'