Hello KitoodleCreators,
Thank you for your reply. The “Unauthorized” error response that you encountered would typically indicate that the access_token used in the authorization header of your request is expired or invalid. If you continue to encounter this issue, I would recommend generating a new access_token and ensuring that your application is properly saving and adding the new token value to the authorization header of subsequent requests.
Upon reviewing our logs for the most recent requests made for your account using the code you provided, it appears that authorization was successful but the requests failed with 409 error responses due to the contact already existing in your account.
Once a contact (email address) has been added into the system, even if it is deleted, the contact entry and details still remain on the back end of the system for reporting and compliance purposes. When using the API, there are a couple of different endpoint options to bypass the 409 response. Typically this error occurs when making a POST call to the /contacts endpoint, which can only add new contacts whose email addresses have not previously been added to the account. Essentially, deleting a contact never really deletes them. It just strips their list memberships, moves them into a “deleted” status, and hides them from being visible in the account.
Frequently Asked Questions for Contacts
https://www.constantcontact.com/legal/about-constant-contact-faqs
Additionally, when working with unsubscribed contacts via the API, you'll need to ensure that resubscribing the contact is permission-based (they have to confirm that they want to be resubscribed and resume receiving emails) to meet all applicable laws and regulations, as well as our terms of service.
Constant Contact's email permission policy
https://knowledgebase.constantcontact.com/articles/KnowledgeBase/5305-constant-contact-s-email-permission-policy?lang=en_US
That being said, in this case you are going to need to use the GET /contacts endpoint using the value "all" ("status":"all"). Using the "status" query parameter with the value "all" returns all contacts including existing, deleted, and unsubscribed contacts, which is needed with your use case.
Next, you'll want to use all of the fields for the "include" parameter, because if the contact exists or previously existed within the account, we'll have to use PUT (rather than POST), which overwrites any existing data (more on that further down)
Here's the flow you'll want to use:
Step 1: Check to see if the contact exists in the account using the email query parameter to search for a contact using a specific email address.
For the GET /contacts call, this is how we'll want to format the request URL to make sure we're checking for deleted or unsubscribed contacts, rather than just active ones:
GET
https://api.cc.email/v3/contacts?email=dlang@example.com&status=all&include=custom_fields,list_memberships,phone_numbers,street_addresses,taggings,notes
If you get a blank response body at this point, then you can use your POST call to create a new contact.
{
"contacts": []
}
However, if a contact entry is returned in the response body, you'll need to update that entry using PUT /contacts instead:
{
"contacts": [
{
"contact_id": "1618ae62-4752-11e9-9c8a-fa163e6b01c1",
"email_address": {
"address": "dlang@example.com",
"permission_to_send": "implicit",
[...]
If the contact comes back as already existing in the account. You need to append the returned data with the desired list memberships and send that back with PUT to update the contact.
When Updating a contact, we need to append the new information to the old information and send all of it back in the request, because when you update a contact using a PUT, all properties are updated, overwriting all existing property values. Any properties left blank or not included in the PUT will be overwritten with a null value.
So, from that response body, the "contact_id" is going to go in our PUT /contacts URL, and then you'll take the rest of the data from the response to build your request body like so:
PUT
https://api.cc.email/v3/contacts/1618ae62-4752-11e9-9c8a-fa163e6b01c1
{
"email_address": {
"address": "dlang@example.com",
"permission_to_send": "implicit",
[...]
Important things to note when updating the contact:
When updating {"permission_to_send": "unsubscribed"} to {"permission_to_send": "implicit"}, you need to also change the update source to Contact, because the permission can't be changed by the account, like so: {"update_source": "Contact"}.
Additionally, if the contact already exists as an active contact in the account (not deleted or unsubscribed), you should still do a PUT call in case they are not already on the desired list. You'll want to append to, rather than replace the existing lists (and other data) so that they are added to the correct list without being removed from other lists they are on:
"list_memberships": [
"07936f78-662a-11eb-af0a-fa163e56c9b0", "04fe9a-a579-43c5-bb1a-58ed29bf0a6a, {add desired list here and send back all three}"
]
Please have a look and let us know if you have any other questions!
Regards,
... View more