Pagination
Cursor pagination for list endpoints.
List endpoints return items newest first, a page at a time:
{
"data": [ { "id": 42, "...": "..." } ],
"nextCursor": "38"
}| Parameter | Description |
|---|---|
limit | Items per page, 1 to 100. Default 25. A number outside that range is rounded to the nearest limit (0 gives 1, 500 gives 100), a fraction is rounded down, and anything that isn't a number (including an empty limit=) gives the default. |
cursor | The nextCursor of the previous page. Leave it out for the first page. Any other value returns 400. |
nextCursor is null on the last page. Cursors stay valid: items created after you started don't shift your pages.
Read every page
async function* allLeads(campaignId) {
let cursor = null;
do {
const url = new URL(`https://app.alienmail.io/api/v1/campaigns/${campaignId}/leads`);
url.searchParams.set('limit', '100');
if (cursor) url.searchParams.set('cursor', cursor);
const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.ALIENMAIL_API_KEY}` } });
if (!res.ok) throw new Error(`Alienmail API: ${res.status}`);
const page = await res.json();
yield* page.data;
cursor = page.nextCursor;
} while (cursor);
}Reading everything uses one request per 100 items. Mind the rate limit.