3

I have been trying to retrieve list of SendGrid transactional templates using API. I'm using correct API key and getting an empty array while there are about 5 transactional templates existing in my SendGrid account. Here is the response:

{
  "templates": []
}

Any guesses what could be wrong?

Manoj Goel
  • 2,034
  • 4
  • 12
  • 24

3 Answers3

6

Any guesses what could be wrong?

Yep, their documentation could be!

I also stuck with the problem and finally managed to solve it once I opened the devtools and saw how they request their own API from the UI. Long story short - one has to pass additional generations=dynamic query parameter. Here is the C# code I use:

            var client = new SendGridClient("key");
            var response = await client.RequestAsync(
                SendGridClient.Method.GET, 
                urlPath: "/templates",
                queryParams: "{\"generations\": \"dynamic\"}");
Andrey Stukalin
  • 5,328
  • 2
  • 31
  • 50
1

Using Api 7.3.0 PHP

require("../../sendgrid-php.php"); 
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

#Comma-delimited list specifying which generations of templates to return. Options are legacy, dynamic or legacy,dynamic
    $query_params = json_decode('{"generations": "legacy,dynamic"}');
    try {
        #$response = $sg->client->templates()->get();
        $response = $sg->client->templates()->get(null, $query_params);

        echo $response->body();
        exit;
    } catch (Exception $e) {
        echo '{"error":"Caught exception: '. $e->getMessage().'"}';

    }
Santo
  • 71
  • 3
0

I had the same problem using the python wrapper provided by Sendgrid. My code was similar to this:

response = SendGridAPIClient(<your api key>).client.templates.get({'generations': 'legacy,dynamic'})

This returned an empty array.

To fix you have to name the param or to pass None before the dict:

response = SendGridAPIClient(<your api key>).client.templates.get(None, {'generations': 'legacy,dynamic'})

or

response = SendGridAPIClient(<your api key>).client.templates.get(query_params={'generations': 'legacy,dynamic'})
Sergio Morstabilini
  • 2,035
  • 21
  • 28