Marketing emails api endpoint

Hello,

We are working with python and HubSpot API, using both the hubspot api library for python and the requests library depending on the endpoint we are using.

We have found a roadblock trying to retrieve the marketing emails and tie them to a contact and get the exact action that contact took with the email (either open, click, bounce, etc).Does anyone know if this is possible and if it is, how can make the call/calls?

Hi @LF_Ruiz and welcome, we are so glad to see you here! :hugs:
Great question, thanks for asking the HubSpot Community!
For information, here is the “Marketing Email API” documentation.
I found for you the solution from @Bortami on this post “Get marketing e-mails per Contact” that might be of interest.
I’d love to put you in touch with our Top Experts: Hi @Bortami, @sylvain_tirreau and @zach_threadint do you have suggestions to help @LF_Ruiz, please?
Have a great day and thanks so much in advance for your help! :heart:
Bérangère

Hello,

You should start there and see if you find a starting point to create your code (list the events and look for clicks and email opens, then use the Email Marketing API and Contacts API to refine the data you need):

Hi @LF_Ruiz
Yes, this is possible using HubSpot APIs. You’ll need to combine two APIs to achieve this:
Step 1

Use the Email Events API (Legacy) to find out what actions a contact took on marketing emails (e.g., opened, clicked, bounced).

You’ll need the contact’s email address.

import requests

TOKEN = 'your_private_app_token'
headers = {
 'Authorization': f'Bearer {TOKEN}',
 'Content-Type': 'application/json'
}

params = {
 'recipient': 'contact@example.com',
 'limit': 100
}

response = requests.get(
 'https://api.hubapi.com/email/public/v1/events',
 headers=headers,
 params=params
)

for event in response.json().get('events', []):
 print(event['type'], event['emailCampaignId'], event['timestamp'])

This will return event types like:

  • Open
  • Click
  • Bounce
  • Unsubscribe
    Step 2

Once you have the emailCampaign id from the events, use the Marketing Emails API v3 to get info about the email (like subject line and send time):

email_id = '123456' # use emailCampaignId from above
response = requests.get(
 f'https://api.hubapi.com/marketing/v3/emails/{email_id}',
 headers=headers
)

email_data = response.json()
print(email_data['subject'], email_data['publishDate'])

Hope this helps…

Hi @sylvain_tirreau and @Balaji_Mavlers1! Thanks for your answer! I’m testing it and I have one question that maybe you can solved, How should I write the request to get me all the events related to an specific campaign?
I already have the campaign ids, base on a list of campaigns ids (using a for loop for example) I would like to get all the events related to it.

The goal with this is not to query all the events everyday, but just to get the campaigns I’m interested in.

Hi @LF_Ruiz ,

You can try something like that:

https://api.hubapi.com/events/v3/events?objectType=CAMPAIGN&objectId=12345&limit=100

The response contains a paging.offset field to paginate; return it in your next call via &offset={offset}