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

SendGrid: Add or update list contacts and check the import job

See more SendGrid Examples

Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.

Add or update contacts ยท Import job status

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    http.put_AuthToken(L"SENDGRID_API_KEY");
    CkHttpResponseW resp;

    const wchar_t *jobId = L"";
    if (jobId == L"") {
        CkJsonObjectW contacts;
        contacts.UpdateString(L"list_ids[0]",L"YOUR_LIST_UUID");
        contacts.UpdateString(L"contacts[0].email",L"avery@example.com");
        contacts.UpdateString(L"contacts[0].first_name",L"Avery");
        contacts.UpdateString(L"contacts[1].email",L"robin@example.com");
        contacts.UpdateString(L"contacts[1].first_name",L"Robin");
        success = http.HttpJson(L"PUT",L"https://api.sendgrid.com/v3/marketing/contacts",contacts,L"application/json",resp);
        if (success != true) {
            if (resp.get_StatusCode() == 0) {
                wprintf(L"%s\n",http.lastErrorText());
            }
            else {
                wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
            }

            return;
        }

        if (resp.get_StatusCode() != 202) {
            wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
            return;
        }

        CkJsonObjectW queued;
        success = queued.Load(resp.bodyStr());
        if (success != true) {
            wprintf(L"Unexpected response: expected JSON.\n");
            return;
        }

        if (queued.HasMember(L"job_id") != true) {
            wprintf(L"No job ID returned.\n");
            return;
        }

        jobId = queued.stringOf(L"job_id");
        wprintf(L"Save this import job ID: %s\n",jobId);
    }

    http.SetUrlVar(L"job",jobId);

    success = http.HttpNoBody(L"GET",L"https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp);
    if (success != true) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"%s\n",http.lastErrorText());
        }
        else {
            wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        }

        return;
    }

    if (resp.get_StatusCode() != 200) {
        wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
        return;
    }

    CkJsonObjectW job;
    success = job.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Unexpected response: expected JSON.\n");
        return;
    }

    const wchar_t *state = job.stringOf(L"status");
    if (state == L"pending") {
        wprintf(L"Still pending. Check again later with jobId = %s\n",jobId);
        return;
    }

    wprintf(L"Job status: %s\n",state);
    wprintf(L"Created: %d, updated: %d, errors: %d\n",job.IntOf(L"results.created_count"),job.IntOf(L"results.updated_count"),job.IntOf(L"results.errored_count"));
    if ((state == L"errored") || (state == L"failed")) {
        wprintf(L"Review the job before retrying. If present, results.errors_url identifies the private error report.\n");
    }
    }