Manoj Goel
Manoj Goel

Reputation: 2120

Retrieving SendGrid Transactional Templates List

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?

Upvotes: 5

Views: 3405

Answers (3)

Sergio Morstabilini
Sergio Morstabilini

Reputation: 2055

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'})

Upvotes: 0

Santo
Santo

Reputation: 69

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().'"}';

    }

Upvotes: 1

Andrey Stukalin
Andrey Stukalin

Reputation: 5939

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\"}");

Upvotes: 7

Related Questions