How to retrieve details for individual marketing campaign IDs using HubSpot API GET requests

I’m working with HubSpot’s marketing API and successfully getting a list of marketing_ids from their endpoint. However, I’m struggling to figure out how to loop through each marketing_id and fetch detailed information for multiple campaigns, then save everything into a pandas dataframe.

Here’s the JSON response I get with the campaign list:

{
"marketing_campaigns": [
    {
        "campaign_id": 5678901234,
        "updatedAt": 16429283391399,
        "applicationId": 445566,
        "applicationName": "TestApp1"
    },
    {
        "campaign_id": 87654321098,
        "updatedAt": 1684283410207,
        "applicationId": 778899,
        "applicationName": "TestApp2"
    },
    {
        "campaign_id": 12345678901,
        "updatedAt": 16429283391399,
        "applicationId": 556677,
        "applicationName": "TestApp3"
    }
 ],
"moreResults": true,
 "nextOffset": "XYZ123ABC$DEFGH"
}

I’m trying to get detailed data for each campaign_id but keep getting a 404 error response. Here’s my Python approach:

auth_token = 'pat-na1-2023xyz-abc123-def456-7890-12345abcdef'
marketing_ids = ['5678901234', '87654321098', '12345678901']
api_url = 'https://api.hubapi.com/marketing/v3/campaigns/{marketing_ids}?hapikey=pat-na1-2023xyz-abc123-def456-7890-12345abcdef'
print("api_url", api_url)

request_headers = {
    'Authorization': 'Bearer pat-na1-2023xyz-abc123-def456-7890-12345abcdef'
}
all_campaigns = []

for marketing_id in marketing_ids:
    print("marketing_id", marketing_id)
    api_response = requests.get(api_url.format(marketing_ids=marketing_id), headers=request_headers)
    print("api_response", api_response)
    
    if api_response.status_code == 200:
        campaign_info = api_response.json()
        all_campaigns.append(campaign_info)
    else:
        print('Request failed for', marketing_id)

What am I doing wrong with the API call format?

You’re getting 404s because that endpoint doesn’t exist. HubSpot doesn’t have /marketing/v3/campaigns/{id} - I’ve worked with their marketing APIs a ton and the endpoints are totally different depending on what type of campaign you’re dealing with. Email campaigns use /email/public/v1/campaigns/{campaignId}. Marketing events use /marketing/v3/marketing-events/{eventId}. Social campaigns have their own thing entirely. Your auth is messed up too - you’re sending the API key as both a URL parameter AND a Bearer token. Drop the hapikey parameter and just use the Authorization header. Figure out what type of campaigns you’re actually working with first, then find the right endpoint in their docs. Test one campaign ID manually before you start looping through anything.

your placeholder doesn’t match - you’ve got {marketing_ids} in the url but you’re calling .format(marketing_ids=marketing_id). make it {marketing_id} (singular) to match. also drop the hapikey param since you’re already using bearer auth - they’re conflicting.

Your URL formatting is wrong. You’ve got {marketing_ids} as the placeholder but you’re passing marketing_ids=marketing_id to format() - that’s why you’re getting a KeyError or the placeholder stays unfilled. Just change your URL to use {marketing_id} instead.

I hit similar issues with HubSpot’s marketing data last month. That /marketing/v3/campaigns/ endpoint you’re using probably doesn’t exist for individual campaigns. HubSpot’s marketing endpoints are all over the place - some campaigns live under /marketing/v3/marketing-events/ while others use email-specific endpoints.

Before you mess with the loop, test if the endpoint actually works. Try a manual GET request to https://api.hubapi.com/marketing/v3/campaigns/5678901234 with your Bearer token. Those consistent 404s tell me the endpoint itself is wrong, not your auth.

Your code has two main issues causing those 404s. You’re mixing auth methods - using both the hapikey parameter and Bearer token. Pick one. The Bearer token in headers is better anyway, so ditch the hapikey from your URL.

Second issue is your endpoint path. https://api.hubapi.com/marketing/v3/campaigns/{marketing_ids} doesn’t look right. HubSpot’s marketing endpoints usually follow patterns like /marketing/v3/marketing-events/{eventId} or something similar.

I hit the same wall last year with their API. Here’s what fixed it: verified the exact endpoint in their docs first, then tested with one campaign ID in Postman before coding the loop. Also logged the full generated URL to see what was actually being sent.

Double-check their API docs for the correct path structure - that’s probably your main culprit.

That 404 error is happening because you’re using the wrong endpoint URL. HubSpot’s marketing campaigns API doesn’t use /marketing/v3/campaigns/{id} like you’ve got there. I’ve worked with HubSpot’s APIs before - campaign details are usually at /marketing/v3/marketing-events/{eventId} or something similar, depending on what campaign data you’re after. The exact endpoint changes based on whether you want email campaigns, social media campaigns, or other marketing stuff. Before you mess with the loop, check HubSpot’s API docs to find the right endpoint for campaign details. Test a single request first with curl or Postman using one campaign ID to make sure the URL structure actually works. Once you’ve got the right path, your loop should be fine - just stick with Bearer token auth and ditch the hapikey parameter from your URL.