APIs & Integrations

cschouten
Member

Unsubscribe Contact using API

Hello there!

I’m attempting to unsubscribe a contact from all emails using an API call. I’ve been trying to use the following request but it keeps giving me an error:

(Using the Requests library in Python)

url = http://api.hubapi.com/email/public/v1/subscriptions/email@address.com?portalId=XXXXXX&hapikey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
json_obj = {'unsubscribeFromAll': 'true'}

r = requests.put(url, data=json_obj)

I know I’m missing something as I keep getting a Response 415. Has anyone made this call before? Are there any headers I’m missing?

Thanks for reading!

0 Upvotes
2 Replies 2
cschouten
Member

Unsubscribe Contact using API

Thanks for the reply @dadams! I totally forgot about json.dumps for Requests. I appreciate the help.

I also added in {‘Content-Type’: ‘application/json’} to the header and it updated the desired test contact.

Thanks again for your help!!

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Unsubscribe Contact using API

Hi @cschouten

The Requests library defaults to form encoded data. If you need to send JSON, you’ll either need to manually set the content-type header and use data=json.dumps(json_obj), or use the json parameter.

r = requests.put(url, json=json_obj)