1
votes

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?

2

2 Answers

1
votes

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

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

    }