检索SendGrid事务模板列表

时间:2019-01-25 19:16:40

标签: sendgrid sendgrid-api-v3 sendgrid-templates

我一直在尝试使用API​​检索SendGrid事务模板的列表。我使用正确的API密钥并得到一个空数组,而我的SendGrid帐户中大约有5个事务模板。这是响应:

{
  "templates": []
}

有人猜怎么了?

2 个答案:

答案 0 :(得分:0)

  

有人猜怎么了?

是的,他们的文档可能是!

我也坚持住这个问题,并在打开devtools并看到它们如何从UI请求自己的API后终于设法解决了这个问题。长话短说-必须传递附加的generations=dynamic查询参数。这是我使用的C#代码:

            var client = new SendGridClient("key");
            var response = await client.RequestAsync(
                SendGridClient.Method.GET, 
                urlPath: "/templates",
                queryParams: "{\"generations\": \"dynamic\"}");

答案 1 :(得分:0)

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

    }