Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

SendGrid: Send an order receipt with a Dynamic Template

See more SendGrid Examples

Activate a version of your receipt template and set its subject to Receipt for order {{order_number}}. Try this HTML in the template:

<p>Thank you, {{first_name}}.</p>
<ul>{{#each items}}<li>{{quantity}} × {{name}}</li>{{/each}}</ul>

Replace the template ID, verified sender, and recipient. Template data is visible to the recipient. The separate, opaque notification reference is for matching delivery events to an application record; keep personal data out of custom arguments.

Dynamic Templates

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW message;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
    resp = CkHttpResponseW_Create();

    message = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(message,L"from.email",L"sender@example.com");
    CkJsonObjectW_UpdateString(message,L"template_id",L"d-YOUR_ACTIVE_TEMPLATE_ID");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].to[0].email",L"recipient@example.com");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].dynamic_template_data.order_number",L"1042");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].dynamic_template_data.first_name",L"Avery");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].dynamic_template_data.items[0].name",L"Notebook");
    CkJsonObjectW_UpdateInt(message,L"personalizations[0].dynamic_template_data.items[0].quantity",2);
    CkJsonObjectW_UpdateString(message,L"personalizations[0].dynamic_template_data.items[1].name",L"Pen");
    CkJsonObjectW_UpdateInt(message,L"personalizations[0].dynamic_template_data.items[1].quantity",1);
    //  This reference is returned on Event Webhook events; it is not a template variable.
    CkJsonObjectW_UpdateString(message,L"personalizations[0].custom_args.notification_ref",L"ntf_8042");

    success = CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",resp);
    if (success != TRUE) {
        if (CkHttpResponseW_getStatusCode(resp) == 0) {
            wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        }
        else {
            wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
        }

        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(message);
        return;
    }

    if (CkHttpResponseW_getStatusCode(resp) != 202) {
        wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(message);
        return;
    }

    wprintf(L"Accepted for delivery (HTTP 202).\n");


    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(message);

    }