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

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonArrayW rows;
    HCkJsonObjectW row;
    int i;

    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();

    //  SetUrlVar encodes the raw address as a URL component, including the plus sign.
    CkHttpW_SetUrlVar(http,L"address",L"avery+orders@example.com");

    success = CkHttpW_HttpNoBody(http,L"GET",L"https://api.sendgrid.com/v3/suppression/bounces/{$address}",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);
        return;
    }

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

    rows = CkJsonArrayW_Create();
    success = CkJsonArrayW_Load(rows,CkHttpResponseW_bodyStr(resp));
    if (success != TRUE) {
        wprintf(L"Unexpected response: expected JSON.\n");
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonArrayW_Dispose(rows);
        return;
    }

    if (CkJsonArrayW_getSize(rows) == 0) {
        wprintf(L"No bounce record for this address.\n");
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonArrayW_Dispose(rows);
        return;
    }

    row = CkJsonObjectW_Create();
    i = 0;
    while ((i < CkJsonArrayW_getSize(rows))) {
        CkJsonArrayW_ObjectAt2(rows,i,row);
        wprintf(L"%s: %s %s\n",CkJsonObjectW_stringOf(row,L"email"),CkJsonObjectW_stringOf(row,L"status"),CkJsonObjectW_stringOf(row,L"reason"));
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonArrayW_Dispose(rows);
    CkJsonObjectW_Dispose(row);

    }