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
That was to be expected – https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact#body says,
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 aDELETE
request, and pass a parametercontact_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
Understanding Contact Management in SendGrid