Formatting requests with the SendGrid integration

Does anybody have a working example of the POST /mail/send query working? I keep getting "bad request" with no other info about the error, but it's also a little wonky trying to create the query from within Retool.

Hi Jed,
You should be able to get more information from the preview of the query builder(bottom window).
it’d be super helpful if you could send a screenshot of your query and what the preview is saying!

Thanks @terence - not sure why I didn’t think to try the preview. That helped me track down the error, and I’m now able to get a 202 response using a hard-coded to: address.

I up this subject as i’m struggling to fill all the required fields.

Especially “body” : “personnalisation”. I dont understand what is expected here. Same with “content”.

I read the manual about the api : https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
which is quite straight forward but cant manage to make it work with the retool integration.

Also, how to incorporate data into the variables i want to use ?

Thanks a lot !

Hey @benoitba and welcome to the community! I’m guessing you’ve seen these already, but here are the Sendgrid docs on personalizations. The TL;DR is that it’s where you put in who you want to send the email to (including CC and BCC). Here’s an example of a personalizations array from the docs:

  "personalizations": [
    {
      "to": [
        {
          "email": "john@example.com"
        }
      ],
      "send_at": 1600188812
    },
    {
      "to": [
        {
          "email": "jane@example.com"
        }
      ],
      "send_at": 1600275471
    }
  ]

(In Retool, you’d just put in the array, not the “personalizations” key)

Content is the contents of your email. According to the docs, they’re expecting an array of objects, like so:

  "content": [
    {
      "type": "text/plain",
      "value": "Hello, World!"
    }
  ]

(Again, since Retool provides a GUI, you just put in the value, not the “content” key)

To use variables, just reference any data in your Retool app via {{ }} like {{ textinput.value }} or the like.

Hope this helps!

Thanks a lot for the quick answer. I did see that example, but my question was about how it translated to retool. I dont know if I make an error of input form. Lets look at the example you took from the api, for "personalization" key :

In the example above, i would then need to fill the box with : "[
{
"to": [
{
"email": "john@example.com"
}
],
"send_at": 1600188812
},
{
"to": [
{
"email": "jane@example.com"
}
],
"send_at": 1600275471
}
]"
Thats right ? Using JS I presume ? I dont understand what "Use dropdown" is used for.

Many thanks !

That’s correct @benoitba! I honestly have no idea what the dropdown is for (it seems to be empty on my end).

Ok. So far all i get is a “method not allowed” error, with “query ran successfuly” meanwhile. But nothing is sent.

Error is :

end_mail: {“status”:422,“message”:“Method Not Allowed”,“statusCode”:422,“error”:“Unprocessable Entity”,“data”:null}

I am sending from a gmail adress.

If you have any clue, but i’ll keep digging !

Are you sure you have your resource configured properly? Do any other methods work?

Well, I can do the post with postman, but not inside retool. That is weird.

Got it to work :slight_smile: Thanks a lot !

1 Like

@benoitba nice! What was the issue?

Hey guys!
Could you please explain a bit more what was the issue @benoitba ? I’m running into the same matter actually. The api call works fine in Postman but not in retool.
I’m having a 422 HTTP error

      {
          "statusCode": 422,
          "error": "Unprocessable Entity",
          "message": "Bad Request",
          "data": null,
          "source": "resource"
        }

Well, it seems that the issue comes from the content field.
The tooltip message says that we should specify at least one MIME type, as an object containing type and value parameters. And the request only succeed when the content field contains at least 1 object.
For example :

[
  {
    type: "application/json",
    value: "test"
  }
]

Yet, I'm trying to send a dynamic template so there is no value to specify here, only the content type which should be set to application/json.
And putting the value attribute to someValue (so that the api call doesn't fail), overrides the dynamic template and the email is sent with a noname.txt containing someValue.

Here's an example of the query, after setting the content field : (otherwise it fails with 422 statuts code error)

That's a weird behaviour actually.. :thinking:

1 Like

I finally found out what was the issue.
The api req only succeeds when the content field uses Dropdown and not JS.
So basically, if you want to send a dynamic template, without having to fill the content field, you just have to make sure that you have clicked on Use dropdown under content input.

To be honest I don’t understand what Use dropdown means actually. Maybe one of the Retool Team could explain its usefulness ?
Thanks !

It doesn't look like that Use dropdown provides any functionality outside of an attempt for a better UI to input array items (which it doesn't appear to provide :grimacing:). Both should have the same output. I believe the option to switch between JS and the Dropdown is provided for every field which expects an array input from the Sendgrid spec.

I'm not overly familiar with the Sendgrid API, but the docs appear to say that the content field requires both a type and a value field, are you generally able to send a request without providing both a content-type and content-value? Is it an option to define the content within the template itself? It seems that if the content type is provided in the template, that it shouldn't need to be defined at the personalizations or the message level.

1 Like

Indeed, the content requires both a type and a value field. But not in the case of a dynamic template, because the content type and content value are already known (provided in the template) and shouldn’t be specified on the content field.

And I yes, I was able (after few hours :sweat_smile:) to send the request without specifying both content-type and content-value. But ONLY when the content field is set to Use Dropdown. Otherwise, if it’s set to Use JS it fails with 422 status code when the content field is empty.

1 Like