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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    String sJobId
    Variant vContacts
    Handle hoContacts
    Handle hoQueued
    Handle hoJob
    String sState
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Integer iTemp3
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    Set ComAuthToken Of hoHttp To "SENDGRID_API_KEY"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

    Move "" To sJobId
    If (sJobId = "") Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoContacts
        If (Not(IsComObjectCreated(hoContacts))) Begin
            Send CreateComObject of hoContacts
        End
        Get ComUpdateString Of hoContacts "list_ids[0]" "YOUR_LIST_UUID" To iSuccess
        Get ComUpdateString Of hoContacts "contacts[0].email" "avery@example.com" To iSuccess
        Get ComUpdateString Of hoContacts "contacts[0].first_name" "Avery" To iSuccess
        Get ComUpdateString Of hoContacts "contacts[1].email" "robin@example.com" To iSuccess
        Get ComUpdateString Of hoContacts "contacts[1].first_name" "Robin" To iSuccess
        Get pvComObject of hoContacts to vContacts
        Get pvComObject of hoResp to vResp
        Get ComHttpJson Of hoHttp "PUT" "https://api.sendgrid.com/v3/marketing/contacts" vContacts "application/json" vResp To iSuccess
        If (iSuccess <> True) Begin
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 0) Begin
                Get ComLastErrorText Of hoHttp To sTemp1
                Showln sTemp1
            End
            Else Begin
                Get ComStatusCode Of hoResp To iTemp1
                Get ComBodyStr Of hoResp To sTemp1
                Showln "HTTP " iTemp1 ": " sTemp1
            End

            Procedure_Return
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 <> 202) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Get ComBodyStr Of hoResp To sTemp1
            Showln "HTTP " iTemp1 ": " sTemp1
            Procedure_Return
        End

        Get Create (RefClass(cComChilkatJsonObject)) To hoQueued
        If (Not(IsComObjectCreated(hoQueued))) Begin
            Send CreateComObject of hoQueued
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoQueued sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Unexpected response: expected JSON."
            Procedure_Return
        End

        Get ComHasMember Of hoQueued "job_id" To bTemp1
        If (bTemp1 <> True) Begin
            Showln "No job ID returned."
            Procedure_Return
        End

        Get ComStringOf Of hoQueued "job_id" To sJobId
        Showln "Save this import job ID: " sJobId
    End

    Get ComSetUrlVar Of hoHttp "job" sJobId To iSuccess

    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" "https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}" vResp To iSuccess
    If (iSuccess <> True) Begin
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 0) Begin
            Get ComLastErrorText Of hoHttp To sTemp1
            Showln sTemp1
        End
        Else Begin
            Get ComStatusCode Of hoResp To iTemp1
            Get ComBodyStr Of hoResp To sTemp1
            Showln "HTTP " iTemp1 ": " sTemp1
        End

        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Get ComBodyStr Of hoResp To sTemp1
        Showln "HTTP " iTemp1 ": " sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJob
    If (Not(IsComObjectCreated(hoJob))) Begin
        Send CreateComObject of hoJob
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJob sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Unexpected response: expected JSON."
        Procedure_Return
    End

    Get ComStringOf Of hoJob "status" To sState
    If (sState = "pending") Begin
        Showln "Still pending. Check again later with jobId = " sJobId
        Procedure_Return
    End

    Showln "Job status: " sState
    Get ComIntOf Of hoJob "results.created_count" To iTemp1
    Get ComIntOf Of hoJob "results.updated_count" To iTemp2
    Get ComIntOf Of hoJob "results.errored_count" To iTemp3
    Showln "Created: " iTemp1 ", updated: " iTemp2 ", errors: " iTemp3
    If ((sState = "errored") Or (sState = "failed")) Begin
        Showln "Review the job before retrying. If present, results.errors_url identifies the private error report."
    End



End_Procedure