Hello, you developer here, trying to figure out of I am handling one of the limitations in the filter search endpoint here. The limitation I am concernced for is. Context: we have almost 3 million records that this will filter through
- The search endpoints are limited to 10,000 total results for any given query. Attempting to page beyond 10,000 will result in a 400 error.
Am I correctly using the filter search and grabbign the contacts records? posting the code below since I do display any personal api info. The code works but I am jsut having trouble understnad the whole query, pagination, limit, and if I am doing it properly
const endpoint = "https://api.hubapi.com/crm/v3/objects/contacts/search";
const filterData = {
properties: ["big_commerce_customer_id", "email", "hs_object_id"],
filterGroups: [
{
filters: [
{
propertyName: "big_commerce_customer_id",
operator: "NOT_HAS_PROPERTY",
},
],
},
],
limit: 100,
after: 100,
};
const headers = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
};
// Define an asynchronous function to fetch and save emails
async function getContacts() {
try {
const allContacts = [];
let subArray = [];
let continueFetching = true;
let totalCalls = 0;
async function makeAPICall() {
if (totalCalls >= 3) {
continueFetching = false;
}
if (continueFetching) {
try {
totalCalls++;
// Make the GET request to fetch contacts
const response = await axios.post(endpoint, filterData, {
headers,
});
if (response.status !== 200) {
console.error(
`API call failed with status code: ${response.status}`
);
continueFetching = false;
return;
}
const contacts = response.data.results;
if (contacts.length === 0) {
continueFetching = false;
}
console.log(contacts);
const simplifiedContacts = contacts.map((contact) => ({
id: contact.id,
email: contact.properties.email,
bigCommerceId: contact.properties.big_commerce_cusotmer_id || null,
}));
subArray.push(...simplifiedContacts);
if (subArray.length >= 100 || contacts.length === 0) {
allContacts.push(subArray);
}
setTimeout(makeAPICall, 10000); // Wait for 10 seconds before making the next call
} catch (error) {
console.error("An error occurred:", error);
continueFetching = false;
}
}
// If you want to check for successful writing to JSON, you can do it here.
// Write the data to a JSON file
try {
await fs.writeFile(
"contacts.json",
JSON.stringify(allContacts, null, 2),
"utf-8"
);
console.log("Contacts successfully written to JSON.");
} catch (error) {
console.error("An error occurred while writing to JSON:", error);
}
}
// Start the API calls
makeAPICall();
} catch (error) {
console.error("An error occurred:", error);
}
}