Lists and pagination
Iterate every item with for await, or work page by page.
List methods return one page at a time. To read a whole list, iterate it with
for await. The SDK requests the next page when you reach the end of the
current one.
for await (const lead of swarmhit.leads.list({ search: 'acme' })) {
console.log(lead.firstName, lead.company);
}You can stop at any time with break. The SDK then requests no further page.
Working with one page
await the call to get a single page:
const page = await swarmhit.leads.list({ search: 'acme', limit: 100 });
page.data; // the leads on this page
page.hasNextPage(); // true when the page is full
if (page.hasNextPage()) {
const next = await page.getNextPage();
}To go through whole pages rather than single items, use iterPages():
for await (const p of page.iterPages()) {
await saveBatch(p.data); // up to 100 at a time
}Page size and the end of a list
limitsets the page size: 1 to 100, 50 by default. A larger value is treated as 100, andpage.paging.limitsays how many the API applied.- The API pages by position (
offset) and doesn't say whether more items follow. SohasNextPage()is true when a page is full, and the read ends on the first shorter or empty page: a list whose size is an exact multiple oflimitends with one empty request. - Pages are read by position. A list that changes while you read it (a chat that gets a new message moves to the top, a record deleted or added) can make one item appear twice or be skipped. Deduplicate by id when that matters.
Database searches and post comments
People and company searches in the B2B database, and a post's comments, page
with a cursor instead of an offset. You use them the same way, with
for await or getNextPage(). The SDK passes the cursor along and sends the
same filters for every page.
Every database result costs credits. Set a limit, and break out of the
loop once you have enough instead of iterating a broad search to the end.
const people = [];
for await (const person of swarmhit.database.people.search({ filters: { titles: ['CTO'] }, limit: 25 })) {
people.push(person);
if (people.length === 100) break;
}Lists that are not paged
A few short lists return everything at once, without pages:
- The reference data:
swarmhit.countries.list(),swarmhit.timezones.list()andswarmhit.voices.list(). - An organization's workspaces (
swarmhit.workspaces.list()) and a workspace's members (swarmhit.workspaces.members.list(workspaceId)), and your managed domains (swarmhit.managedDomains.list()).