Hi, I wonder if anyone tried to set up a contact form via SendGrid. This is the recommended method as per Sending Emails from an application on Vercel.
However, I can’t make it work. I keep getting Unauthorized. The code works perfectly from my localhost, but not from deployed application.
I thought the problem could be with SendGrid’s IP access management, but Vercel doesn’t provide static IP address for hobby accounts so that I could whitelist those. However, AI support on Vercel website told me, that this shouldn’t be an issue for the functionality to work.
On Vercel side:
I have correctly set up the environmental variable - its available on all environments (SENDGRID_API_KEY).
On SendGrid side:
API key has full access
Domains are verified
I also tried to enable CORS via vercel.json, but same result - I always get “Unauthorized”.
Anybody came across similar issue? Anybody integrated SendGrid successfully?
Let’s start by double-checking that all necessary values are set correctly and ensuring your token is still valid and hasn’t expired.
If you’re sure it’s not an issue with your environment variable, or token, I recommend checking these resources shared by the community in our previous platform:
Wow thank you so much for such quick response! Yeah, unfortunately I can’t see anything wrong with environment variable, as I stated in my original post. Its set.
As for problems with Delays, I’ve came across that too, but I already do Await when sending the form. This is my piece of code:
try {
await sendgrid.send({
to: recipient, // Your email where you’ll receive emails
from: recipient, // your website email address here
replyTo: email,
subject: req.body.subject || subject || ‘Contact form entry’,
html,
})
} catch (error) {
return res.status(error.statusCode || 500).json({ error: error.message })
}
Hi everyone, indeed it was caused by SendGrid IP Access Management.
You can’t whitelist your own IP (to access the admin account), because that will automatically block also all API requests - thus vercel.
If you don’t have static IP hosting, don’t use IP whitelisting even for basic auth for SendGrid’s access.