APIs & Integrations

AMuller1
Participant

How to fetch a collection of contacts using the associated list id

SOLVE

Hey I think this might have been asked before but I couldn't find a clear answer, how can query for a collection of contacts using a list id? I'm not sure which API is capable of doing this.

0 Upvotes
1 Accepted solution
EddBrisley
Solution
Contributor

How to fetch a collection of contacts using the associated list id

SOLVE

Hi @AMuller1 👋

 

Sorry if you've already tried it - but does the 'Fetch List Memberships Ordered by ID' endpoint work for what you need?

const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

const listId = "listId"; // might have to use the ILS list ID for this, 
const after = undefined;
const before = undefined;
const limit = 100;

try {
  const apiResponse = await hubspotClient.crm.lists.membershipsApi.getPage(listId, after, before, limit);
  console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
  e.message === 'HTTP request failed'
    ? console.error(JSON.stringify(e.response, null, 2))
    : console.error(e)
}


In theory this should return all the contacts in the specified list (as long as it's a contact-based list). Let me know how you get on 👍

Edd

View solution in original post

2 Replies 2
EddBrisley
Solution
Contributor

How to fetch a collection of contacts using the associated list id

SOLVE

Hi @AMuller1 👋

 

Sorry if you've already tried it - but does the 'Fetch List Memberships Ordered by ID' endpoint work for what you need?

const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

const listId = "listId"; // might have to use the ILS list ID for this, 
const after = undefined;
const before = undefined;
const limit = 100;

try {
  const apiResponse = await hubspotClient.crm.lists.membershipsApi.getPage(listId, after, before, limit);
  console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
  e.message === 'HTTP request failed'
    ? console.error(JSON.stringify(e.response, null, 2))
    : console.error(e)
}


In theory this should return all the contacts in the specified list (as long as it's a contact-based list). Let me know how you get on 👍

Edd

AMuller1
Participant

How to fetch a collection of contacts using the associated list id

SOLVE

Hi Edd, this is exactly what I was looking for thanks!