sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
620 stars 324 forks source link

How to add/remove contacts to a list via new marketing API? #456

Closed netwire88 closed 3 years ago

netwire88 commented 3 years ago

There doesn't seem to be any documentation on how to add/remove contacts to a list via the new marketing API? Is there a gem wrapper around this https://sendgrid.api-docs.io/v3.0/lists/remove-contacts-from-a-list?

rickychilcott commented 3 years ago

391 might be helpful to you. This does need better documentation

netwire88 commented 3 years ago

Thanks @rickychilcott , I read through that, but the example is how to create a contact & add it to a list at the same time. I'm trying to figure out how to add/remove contacts to/from lists without updating contact info. Any ideas?

Essentially, how to translate DELETE /marketing/lists/{id}/contacts. (https://sendgrid.api-docs.io/v3.0/lists/remove-contacts-from-a-list) to sg.client.marketing.... format?

So far, I can retrieve a list

sg.client.marketing.lists._(list_id).get

But delete a contact doesn't work, it deletes the entire list instead of a single contact in a list:

sg.client.marketing.lists._(list_id).delete(contact_ids: contact_id)
rickychilcott commented 3 years ago

Adding a user to a list without changing anything else about that should be something like:

contacts = [{ email: "abc@1234.com"}]
list_ids = ["abcd"]
response = sg.client.marketing.contacts.put(request_body: {list_ids: list_ids, contacts: contacts})

Note, if the email address does not exist, it will create a new contact.

Regarding deleting a contact from a list, I havent' done it myself.

The code you wrote does look close (e.g. sg.client.marketing.lists._(list_id).delete(contact_ids: contact_id)).

But since that URL you linked to is DELETE /marketing/lists/{id}/contacts does the following work:

sg.client.marketing.lists._(list_id).contacts.delete(contact_ids: contact_id)
netwire88 commented 3 years ago

Thanks @rickychilcott , great ideas!

It turns out the correct formatting for removing a contact from a list is:

sg.client.marketing.lists._(list_id).contacts.delete(query_params: {"contact_ids": contact_id})

I found some examples of similar API calls via https://github.com/sendgrid/sendgrid-ruby/blob/main/examples/suppression/suppression.rb

thinkingserious commented 3 years ago

Please be sure to vote on this issue to help move the priority up. Thanks!