Sample code for 30+ languages & platforms
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 C++ Downloads

C++
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    bool success = false;

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

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

            return;
        }

        if (resp.get_StatusCode() != 202) {
            std::cout << "HTTP " << resp.get_StatusCode() << ": " << resp.bodyStr() << "\r\n";
            return;
        }

        CkJsonObject queued;
        success = queued.Load(resp.bodyStr());
        if (success != true) {
            std::cout << "Unexpected response: expected JSON." << "\r\n";
            return;
        }

        if (queued.HasMember("job_id") != true) {
            std::cout << "No job ID returned." << "\r\n";
            return;
        }

        jobId = queued.stringOf("job_id");
        std::cout << "Save this import job ID: " << jobId << "\r\n";
    }

    http.SetUrlVar("job",jobId);

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

        return;
    }

    if (resp.get_StatusCode() != 200) {
        std::cout << "HTTP " << resp.get_StatusCode() << ": " << resp.bodyStr() << "\r\n";
        return;
    }

    CkJsonObject job;
    success = job.Load(resp.bodyStr());
    if (success != true) {
        std::cout << "Unexpected response: expected JSON." << "\r\n";
        return;
    }

    const char *state = job.stringOf("status");
    if (state == "pending") {
        std::cout << "Still pending. Check again later with jobId = " << jobId << "\r\n";
        return;
    }

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