I’m trying to follow SendGrid’s integration here with no luck. Currently I’m trying to send an email on login (just so I have a quick way to test it), and I keep getting 400 Bad Request
as a response. I’ve console logged out process.env.SENDGRID_API_KEY
to make sure that the key is correct, and I know that’s not the problem. It’s also been set to full access.
The full error message:
ResponseError: Bad Request
at C:CodeRecipe [email protected]:146:29
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 400,
response: {
headers: {
server: 'nginx',
date: 'Wed, 28 Jul 2021 17:52:10 GMT',
'content-type': 'application/json',
'content-length': '185',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html',
'strict-transport-security': 'max-age=600; includeSubDomains'
},
body: { errors: [Array] }
}
}
I don’t think it’s a classic CORS
error, as in my server.js
I have this: (unless I’m missing something here)
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
I’ve tried adding in On-behalf-of, x-sg-elas-acl
to the allowed headers, which didn’t change the outcome. Should those be included?
It also seems to arbitrarily switch between 400
and 401
and I honestly don’t know why. Here’s the 401
message:
ResponseError: Unauthorized
at C:CodeRecipe [email protected]:146:29
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 401,
response: {
headers: {
server: 'nginx',
date: 'Wed, 28 Jul 2021 18:04:59 GMT',
'content-type': 'application/json',
'content-length': '97',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html',
'strict-transport-security': 'max-age=600; includeSubDomains'
},
body: { errors: [Array] }
}
}
When I set up sender authentication, I authenticated a domain instead of a single sender, although I’m still testing this in the dev environment and am not on my domain when attempting to send an email. Could that be causing an issue as well?
Could the issue also be that I’m testing on localhost and the sendgrid IP access management is allowing my current IP but not localhost access? Though I’m not so sure adding localhost as an allowed IP is such a good idea.
2
Answers
Turns out the issue was I was setting the email's
from
field to something that didn't match my domain authenticated sender. Changing that to the right@
fixed the issue, and the email sent without a problem.April 2023. I’cant comment my reputation is to low.
But if anybody wil enocunter 400 from sendgrid check if proerty ‘text’ of your message object is not empty string. Changing it to space character helpt me.