skip to Main Content

I’m utilizing the SendGrid API to manage contacts and their associated contact lists. Referring to the documentation here, I initially created an email address and assigned it to Contact Lists (XXXXX, YYYYY) using the following request:

curl -X PUT "https://api.sendgrid.com/v3/marketing/contacts" 
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" 
--header "Content-Type: application/json" 
--data '{"list_ids":["057204d4-755b-4364-a0d1-XXXXXX", "057204d4-755b-4364-a0d1-YYYYY"], "contacts": [{"email": "[email protected]"}]}'

Now, I want to update the contact’s associated Contact List to another one (ZZZZZ) using the same request. So, I issued the following command:

curl -X PUT "https://api.sendgrid.com/v3/marketing/contacts" 
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" 
--header "Content-Type: application/json" 
--data '{"list_ids":["057204d4-755b-4364-a0d1-ZZZZZ"], "contacts": [{"email": "[email protected]"}]}'

However, upon searching for the email address using the following request:

curl -X POST "https://api.sendgrid.com/v3/marketing/contacts/search/emails" 
--header "Authorization: Bearer <<YOUR_API_KEY_HERE>>" 
--header "Content-Type: application/json" 
--data '{"emails": ["[email protected]"]}'

I noticed that the email address’s contact list was not updated to ZZZZ; instead, all three lists are still present:

{
    "result": {
        "[email protected]": {
            "contact": {
                ...........
                "email": "[email protected]",
                "list_ids": [
                    "057204d4-755b-4364-a0d1-XXXXXX",
                    "057204d4-755b-4364-a0d1-YYYYYY",
                    "057204d4-755b-4364-a0d1-ZZZZZZ"
                ],
               ..........
            }
        }
    }
}

Could someone please guide me on how to properly update the Contact Lists associated with an email address?

2

Answers


  1. I noticed that the email address’s contact list was not updated to ZZZZ; instead, all three lists are still present

    That was to be expected – https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact#body says,

    list_ids– array[string] – An array of List ID strings that this contact will be added to.

    Doesn’t say there that the contact will get removed from other lists they are already on.

    To remove a contact from a list, you need to go via the lists endpoint, /v3/marketing/lists/{id}/contacts to be specific. You need to make a DELETE request, and pass a parameter contact_ids containing the comma-separated contact IDs you want to remove from the list.

    https://docs.sendgrid.com/api-reference/lists/remove-contacts-from-a-list

    Login or Signup to reply.
  2. // Update SendGrid contact's list association
    $apiKey = 'YOUR_API_KEY_HERE';
    $url = 'https://api.sendgrid.com/v3/marketing/contacts';
    $contactEmail = '[email protected]';
    $newListIds = ['057204d4-755b-4364-a0d1-ZZZZZ'];
    
    $data = [
      'list_ids' => $newListIds,
      'contacts' => [['email' => $contactEmail]]
    ];
    $payload = json_encode($data);
    $headers = [
      'Authorization: Bearer ' . $apiKey,
      'Content-Type: application/json',
      'Content-Length: ' . strlen($payload)
    ];
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    echo $response;
    

    Understanding Contact Management in SendGrid

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search