Published on

How to send email using Resend API

Authors

Resend is a startup that is focused on helping developers to send transactional emails easily without them getting ended up in spam.

It has support for various ways to send an email like from serverless environments, REST API call and other supports almost all the major programming languages and frameworks.

And the best part is you can write an email using React itself (if you prefer)

For the sake of simplicity, let's send an email with attachments using their REST API.

The only thing that we need is the API key. You can easily get it from their dashboard

Curl

curl -X POST 'https://api.resend.com/emails' \
  -H 'Authorization: Bearer re_YOUR_RESEND_API_KEY_HERE' \
  -H 'Content-Type: application/json' \
  -d $'{
    "from": "onboarding@resend.dev",
    "to": "example@gmail.com",
    "subject": "Hello World from Curl",
    "text": "It works!"
  }'

Node.js using Fetch

npm install node-fetch
import fetch from "node-fetch";

const RESEND_AUTH_KEY = process.env.RESEND_AUTH_KEY

const payload = {
    "from": "onboarding@resend.dev",
    "to": "example@gmail.com",
    "subject": "Hello World",
    "html": "<p>Congrats on sending your <strong>first email</strong>!</p>"
  }
const requestOptions = {
  method: 'POST',
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${RESEND_AUTH_KEY}`
  },
  body: JSON.stringify(payload)
};

fetch("https://api.resend.com/emails", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Happy sending emails!