sendgrid / sendgrid-java

The Official Twilio SendGrid Led, Community Driven Java API Library
https://sendgrid.com
MIT License
485 stars 409 forks source link

Create a new Contact and add to List in one method call #598

Closed ashaffer1903 closed 4 years ago

ashaffer1903 commented 4 years ago

Issue Summary

When using the /marketing/contacts endpoint of the V3 API, I am not able to also add the contact to a list at the same time.

Steps to Reproduce

  1. Create a new list: POST https://api.sendgrid.com/v3/marketing/lists {"name": "test"}
  2. The API should return 200, with a response body similar to: { "name": "test", "id": "abc-123", "contact_count": 0 }
  3. Now, I want to create a new contact, and add them to the list. I should be able to add the contact like so: PUT https://api.sendgrid.com/v3/marketing/contacts { "email"="bob.butts@aol.com", "list_ids"=["abc-123"] } This does not work, and instead I have to create the contact, get the contact id, and then make a PUT request to the marketing/lists endpoint with the contact's id.

Technical details:

thinkingserious commented 4 years ago

Hello @ashaffer1903,

It sounds like you would like a helper method that would allow you to add a contact to a list in one method call. I've updated the title accordingly.

Pull requests to add this feature are welcome and will be reviewed based on priority, but Twilio SendGrid is not actively building new functionality for the library.

With best regards,

Elmer

childish-sambino commented 4 years ago

I'm not seeing this behaviour locally. Creating a contact with the returned list ID adds the contact to the list. Note that the payload in your example should be:

{ "contacts": [{ "email"="bob.butts@aol.com" }], "list_ids"=["abc-123"] }

Could you post sample code that illustrates this issue?

sauntimo commented 2 years ago

If anyone comes across this in future, please note that you can now create/update users and add them to a list in one request. The request body schema is on this docs page. You can write a request like this;

  const listId = "your-list-id";

  try {
    await sendGridClient.request({
      url: `/v3/marketing/contacts`,
      method: "PUT",
      body: {
        list_ids: [listId],
        contacts: [
          {
            email: user.email,
            first_name: user.firstName,
            last_name: user.lastName,
          },
        ],
      },
    });
    return { success: true, message: "Added user to mailing list" };
  } catch (error) {
    return { success: false, message: "Failed to add user to mailing list" };
  }