SendGrid

Send an email to a user.

SendGrid
import sendgrid from '@sendgrid/mail';

sendgrid.setApiKey(process.env.SENDGRID_API_KEY);

export default async (req, res) => {
  try {
    await sendgrid.send({
      to: 'your@email.com',
      from: 'your@email.com',
      subject: 'Serverless Functions',
      text: 'Hello, world!'
    });
  } catch (error) {
    return res.status(error.statusCode || 500).json({ error: error.message });
  }

  return res.status(200).json({ error: '' });
};

Usage

First, create a SendGrid account. Then, generate an API key.

To protect against spam, SendGrid requires customers to verify their Sender Identities.

  1. Navigate to Sender Authentication.
  2. Click "Create New Sender".
  3. Fill out the form and click "Create".
  4. Verify your email address.

To securely access the API, we need to include the secret with each request. We also do not want to commit secrets to git. Thus, we should use an environment variable. Learn how to add environment variables in Vercel.