sendgrid-nodejs: Unexpected Error when trying to send transactional email

Issue Summary

Getting a ‘Bad Request’ error with the following payload.

Request

cosnt msg = {
        "to": "ian.reis@gmail.com",
        "from": "invitations@paycheck.fyi",
        "subject": "Support Pay Transparency at PayCheck",
        "templateId": "24ae3147-4faa-4380-8613-c5be144f4542",
        "customArgs": {
            "ally_id": "cj6zlh7yd000001qir4r5suuk"
        }
};
sgMail.send(msg)

Response

{
        "message": "Bad Request",
        "code": 400,
        "response": {
            "headers": {
                "server": "nginx",
                "date": "Wed, 30 Aug 2017 22:30:41 GMT",
                "content-type": "application/json",
                "content-length": "365",
                "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"
            },
            "body": {
                "errors": [
                    {
                        "message": "Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.",
                        "field": "content",
                        "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
                    }
                ]
            }
        }
    }

Notes

In the documentation it says templateId but in the error message it says template_id. I’ve tried both and received the same error message. I attached a screenshot of my template screen as a sanity check to make sure I have it configured correctly in sendgrid.

Technical details:

  • sendgrid-nodejs Version: master (latest commit: [commit number])
  • Node.js Version: 6.10.3
  • Running in an AWS Lambda function

screen shot 2017-08-30 at 6 43 57 pm

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 24 (14 by maintainers)

Most upvoted comments

Thanks for the help peeps. I was literally losing my mind. I finally figured out a workaround late last night and was coming here to report more information, but it seems like you all are already on it 😃

Sorry about that @onesien,

The fix has just been pushed to npm (v6.1.2).

PR is up, this should fix the issue and not send the content field with the request if there is no content provided.

@thinkingserious I think you’re right. I read the API docs and thought the field was mandatory. It only has a well hidden comment (which is amazingly styled!) that I missed:

image

I will create a PR to address this!

I know this is an old issue, but I’m getting this same error. Using the suggestion by @cbilliau works… The catch here is that I’m not using Javascript… I’m using a Crystal adapter.

These docs say content is required Screen Shot 2021-11-16 at 3 29 02 PM

so in my case, I was sending an empty array along with the template id {"content": [], "template_id": "abc123"}. If anyone else lands here, {content: [{ type: "text/html", value: "0" }], template_id: "abc123"} works. Possibly also removing the content key if template_id exists, but I haven’t been able to test that theory.

kk - confirming its working now, especially using the content: [{ type: "text/html", value: "0" }] line that @cbilliau mentioned 👍

wow let me bow out for a sec @cbilliau 🤣 i just was rushing thru stuff and didn’t pay more attention. I realized i was grabbing the unique ID for a template i made under “marketing” but not under “transactional templates”. I don’t use the sendgrid UI much and just totally botched this one lol

Total face palm, fixing on my end and then hopefully expecting it to work.

I had to add the content block to get the template to be sent. I was getting a Bad Request (400) “Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block…” while using template created in the new html editor.

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: 'support@freshairsensor.com',
  templateId: 'xxxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)

If I add the content block, the template email is sent as formatted in the html editor.

sgMail.setApiKey(sgKey);
sgMail.setSubstitutionWrappers('-', '-');
var msg: any = {
  to: email,
  from: 'support@freshairsensor.com',
  content: [{"type":"text/html","value":"0"}],
  templateId: 'xxxxxx',
  substitutions: {
     verifyUrl: createAccountLink
  }
};
sgMail.send(msg)