I have a Next.js 14 project deployed on Vercel, where I've integrated the Mailgun API to send emails using this package. The integration works fine locally. However, after deploying to production on Vercel, I'm consistently encountering an "Unauthorized" error when attempting to send emails via the Mailgun API.
I've already added Vercel's IP, i.e. 76.76.21.21 to our domain's whitelist on Mailgun, but still no luck.
I've thoroughly checked and verified the API keys, environment variables and permissions and they're all fine. I've also included my code below for reference.
import { NextRequest, NextResponse } from "next/server";
import FormData from "form-data";
import Mailgun from "mailgun.js";
const { MAILGUN_EMAIL, MAILGUN_TO_EMAIL, MAILGUN_API, MAILGUN_DOMAIN } =
process.env;
export async function POST(request: NextRequest, res: NextResponse) {
const { name, email, ticketCount, attendingAs, exhibitorDetails } =
await request.json();
const mailgun = new Mailgun(FormData);
try {
const mg = mailgun.client({
username: "api",
key: MAILGUN_API as string,
});
const mailData = {
from: MAILGUN_EMAIL,
to: MAILGUN_TO_EMAIL,
subject: `${name} just signed up for the expo`,
text: `${name} just signed up for ${ticketCount} tickets as ${attendingAs} for the expo with email: ${email}.`,
html: `
<body>
<div style="background: #f7f7f7; padding: 20px 0; margin:0">
body content here
</div>
</body>`,
};
await new Promise((resolve, reject) => {
mg.messages
.create(MAILGUN_DOMAIN as string, mailData)
.then((message) => {
resolve(message);
})
.catch((error) => {
reject(error);
return NextResponse.json({ error: error }, { status: error.status });
});
});
return NextResponse.json(
{
message:
"Thank you for your interest in attending the expo. We hope to see you there.",
},
{ status: 200 }
);
} catch (error: any) {
return NextResponse.json({ error: error }, { status: error.status });
}
}
I've also attached a screenshot below for reference.

I have a Next.js 14 project deployed on Vercel, where I've integrated the Mailgun API to send emails using this package. The integration works fine locally. However, after deploying to production on Vercel, I'm consistently encountering an "Unauthorized" error when attempting to send emails via the Mailgun API.
I've already added Vercel's IP, i.e. 76.76.21.21 to our domain's whitelist on Mailgun, but still no luck.
I've thoroughly checked and verified the API keys, environment variables and permissions and they're all fine. I've also included my code below for reference.
I've also attached a screenshot below for reference.
