Node.js Emails: Integrating with Resend API
Resend offers a little library that makes sending emails a breeze, which you can find here.
But, I always like to see if I need a library before I install it to try and have less dependencies in my project.
With Resend, sending emails via the API is very simple and in this article we will learn how:
How to Send Emails with the Resend API
Configuring Resend
- Sign up for a Resend account to obtain your API key.
- In your project, you'll need to use this. I'm using an environment variable in the following code snippet.
- Do the thing:
const RESEND_API_KEY = process.env.RESEND_API_KEY;
const sendMail = async () => {
const res = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${RESEND_API_KEY}`
},
body: JSON.stringify({
from: 'Niall <testmaill@codu.co>',
to: ['person@codu.co'],
subject: 'yo, hello world',
html: '<b>Important message...</b>',
})
});
const data = await res.json();
return data;
// Don't forget to check status and error handling
};
sendMail();
You can include additional parameters like from, cc, bcc, and attachments as needed.
Additional Notes
- It's important to handle errors and confirm successful delivery.
- You can check the “Emails” page on Resend's dashboard to view the status of emails sent.
- Always secure your API key and test your email-sending functionality thoroughly.
Happy coding!
