I have the following code block to search for notes created on a specific date. Unfortunately, after paging through 10000 results, i get the following error:
Status code: 400, Message: {"status":"error","message":"There was a problem with the request.","correlationId":"83d14a7c-7a68-45f2-8f21-765f1a81508c"}
import requests
import time
import json
def search_hubspot_notes(api_key, start_date, end_date):
search_url = 'https://api.hubapi.com/crm/v3/objects/notes/search'
# Headers for the request
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}" # Use your API key or OAuth token
}
# The payload for the search request, with 'limit' set to 100 for maximum results per page
search_payload = {
"filterGroups": [{
"filters": [{
"propertyName": "hs_createdate",
"operator": "BETWEEN",
"value": start_date,
"highValue": end_date
}]
}],
"properties": ["hs_note_body", "hs_timestamp", "hs_createdate", "hs_lastmodifieddate", "id", "hs_createdby_user_id"],
"limit": 100 # Maximum limit allowed by HubSpot to maximize results per page
}
all_notes = []
has_more = True
after = 0 # Initial 'after' parameter for pagination, starts with 0
pagination_count = 0 # Counter for the number of paginations
while has_more:
search_payload["after"] = after
try:
response = requests.post(search_url, headers=headers, json=search_payload)
if response.status_code == 200:
pagination_count += 1
data = response.json()
all_notes.extend(data.get("results", []))
# Update 'has_more' based on the presence of 'paging.next.after'
has_more = data.get("paging", {}).get("next", {}).get("after") is not None
if has_more:
after = data["paging"]["next"]["after"]
else:
print(f"Failed to retrieve notes. Status code: {response.status_code}, Message: {response.text}")
print(f"Request details:\nURL: {response.request.url}\nMethod: {response.request.method}")
print(f"Headers: {response.request.headers}\nPayload: {json.dumps(search_payload, indent=4)}")
break
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
break
# Handle rate limiting by sleeping then retrying
if response.status_code == 429:
retry_after = int(response.headers.get("Retry-After", 10)) # Default retry after 10 seconds if header is missing
print(f"Rate limit hit, sleeping for {retry_after} seconds")
time.sleep(retry_after)
continue
print(f"Completed {pagination_count} paginations.")
return all_notes
# Use your actual API key or OAuth token here
api_key = 'Test'
start_date = "2024-03-06T00:00:00.000Z"
end_date = "2024-03-07T00:00:00.000Z"
# Fetch notes
notes = search_hubspot_notes(api_key, start_date, end_date)
# Print the outcome
print(f"Found {len(notes)} notes created between {start_date} and {end_date}")
It’s fairly ambiguous and I’m not sure what the correlationID has to do with it.