Send Emails in Vercel with Mailtrap
Step-by-step guide on how to integrate Mailtrap with your application hosted on Vercel.
Mailtrap is an email-sending solution for developer and product teams. Focused on fast delivery and high inboxing rates for transactional and promo emails. Provides highly customizable API and 24/7 tech support.
Prerequisites:
- Vercel account with a project for which you’ll add your Mailtrap API key.
- If you haven’t set up your sending domain already, you’ll need to do it before we start—it takes ~5 minutes, and you can use our step-by-step article as a guide.
Step 1. Find your Mailtrap API key
In your Mailtrap dashboard, click on Settings → API Tokens. There, you should be able to see all active tokens, their creators, and access level.
If you already don’t have an API key, click on Add Token and assign the desired permissions. In this case, you should check the API/SMTP permissions.
Hit Save and then store your API key safely since you won’t be able to see it again. For more information, you can read our guide on Mailtrap API Tokens.
Step 2. Add your key to Vercel
Open your Vercel dashboard and go to the Settings for the project you want to add Mailtrap to:
Then, in the Environment Variables section of Settings, find the Key section and add MAILTRAP_API_TOKEN
and your actual token, and click Save.
Important: Since Vercel environment variables only become available after you redeploy your project, make sure to either push a new commit or click Deploy again in the Vercel dashboard.
Step 3. Reference the key in your code
Finally, make sure to reference the Mailtrap API key in your code so that your app/project can reference it when making requests. To do this, you need to add your Mailtrap API key to your code, as well as the Mailtrap URL https://send.api.mailtrap.io/api/send.
For example, here’s what it would look like in your route.ts:
const MAILTRAP_API_TOKEN = process.env.MAILTRAP_API_TOKEN; const res = await fetch("https://send.api.mailtrap.io/api/send", { method: "POST", headers: { "Authorization": `Bearer ${MAILTRAP_API_TOKEN}`, "Content-Type": "application/json" }, body: JSON.stringify({ from: { email: "no-reply@yourdomain.com" }, to: [{ email: "support@yourdomain.com" }], subject: "Hello from Vercel + Mailtrap", text: "This is a test email sent via Mailtrap API." }) });