Fetching Lists

I want to fetch a list in hubspot. I used below program. It is giving error. Also is there any issue with v1 api?

Error occurred during API request: 401 {"status":"error","message":"This hapikey doesn't exist.","correlationId":"2c43fca5-6bf2-4f99-89d2-833282530054"}

When i use api key to fetch contacts,deals and companies it is working but not with lists. Below i provided program for reference. Please give suggestion.

import requests
from datetime import datetime, timedelta

api_key = “”
list_id = 1234

# Calculate the date range for the previous day
specific_date = datetime.now() - timedelta(days=1)
start_date = specific_date.replace(hour=0, minute=0, second=0, microsecond=0).isoformat()
end_date = specific_date.replace(hour=23, minute=59, second=59, microsecond=999999).isoformat()

# Set the API endpoint URL
url = f"https://api.hubapi.com/contacts/v1/lists/{list_id}/contacts/all"

# Set the request parameters
params = {
“hapikey”: api_key,
“start”: start_date,
“end”: end_date
}

# Send the API request
response = requests.get(url, params=params)

# Check if the request was successful
if response.status_code == 200:
# Parse the response data
data = response.json()

# Extract the list of contacts from the response
contacts = data.get(“contacts”, [])

# Print the list of contacts
for contact in contacts:
print(contact)
else:
print(f"Error occurred during API request: {response.status_code} {response.text}")

Hi @SubrahmanyBhat,

As of November 30th 2022 using an API key has been deprecated. Hence the error you are seeing. Instead you must either choose to use a private or public apps corresponding access token. More information here.

Once done it should resolve and authenticate successfully!

No. It is not working . Iam using private app access token only.

Hi @SubrahmanyBhat,

I can’t seem to replicate the issue when calling the same endpoint using an access token via private app. Also I’m confused as you are referencing what appears to be an API key using “hapikey” and not an access token in your code.

# Set the request parameters
params = { 
"hapikey": api_key, # REFERENCING HAPI KEY HERE?
"start": start_date,
"end": end_date
}

In addition to this you are not including your access token as a header but rather a query parameter. See your code below:

# Send the API request
response = requests.get(url, params=params) #PASSING AS QUERY PARAM HERE?

HubSpot expects the access token to be included as a header in your request. Below is a sample request using python to the same endpoint you are using and it’s working for me. The difference being I’m using an access token and I’m passing it as a header. Not using an API key and passing as a query parameter which your code seems to be doing:

url = "https://api.hubapi.com/contacts/v1/lists/****/contacts/all"
headers = {
 'Authorization': 'Bearer pat-***-******-****-****-****-************'
}
response = requests.get(url, headers = headers)
print(response.text)