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": "annahamilton@example.org"}]}'
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": "annahamilton@example.org"}]}'
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": ["annahamilton@example.org"]}'
I noticed that the email address's contact list was not updated to ZZZZ; instead, all three lists are still present:
{
"result": {
"annahamilton@example.org": {
"contact": {
...........
"email": "annahamilton@example.org",
"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?
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