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

  1. Sign up for a Resend account to obtain your API key.
  2. In your project, you'll need to use this. I'm using an environment variable in the following code snippet.
  3. 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!

NodejsEmailResend
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.