# Fetching Lists

**URL:** <https://community.hubspot.com/t/fetching-lists/86179>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [July 26, 2023, 1:58pm UTC](https://community.hubspot.com/t/fetching-lists/86179 "2023-07-26T13:58:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubrahmanyBhat](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@SubrahmanyBhat](https://community.hubspot.com/u/SubrahmanyBhat)\
**Post date:** [July 26, 2023, 1:58pm UTC](https://community.hubspot.com/t/fetching-lists/86179/1 "2023-07-26T13:58:45Z")

</div>

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](https://api.hubapi.com/contacts/v1/lists/%7Blist_id%7D/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}")

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [July 26, 2023, 3:49pm UTC](https://community.hubspot.com/t/fetching-lists/86179/2 "2023-07-26T15:49:03Z")

</div>

Hi @SubrahmanyBhat,

As of November 30th 2022 using an [API key has been deprecated](https://developers.hubspot.com/changelog/upcom). Hence the error you are seeing. Instead you must either choose to use a private or public apps corresponding access token. More information [here](https://developers.hubspot.com/docs/api/developer-tools-overview).

Once done it should resolve and authenticate successfully!

---

<div class="post-metadata">

**Author:** ![SubrahmanyBhat](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@SubrahmanyBhat](https://community.hubspot.com/u/SubrahmanyBhat)\
**Post date:** [July 28, 2023, 7:44am UTC](https://community.hubspot.com/t/fetching-lists/86179/3 "2023-07-28T07:44:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [July 28, 2023, 11:34am UTC](https://community.hubspot.com/t/fetching-lists/86179/4 "2023-07-28T11:34:50Z")

</div>

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.

```lia-code-sample
# 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:

```lia-code-sample
# 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:

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

```
