Pagination
How to page through posts, reels, comments and hashtag results with IGramAPI cursors, with loop examples in Python and Node.js.
Endpoints that return lists (posts, reels, comments, hashtag feeds) use cursor pagination.
How it works
A response includes a page object:
{
"items": [ ... ],
"page": { "has_next_page": true, "end_cursor": "QVFDX2V4YW1wbGVjdXJzb3I" }
}
To get the next page, send the same request with cursor set to the previous page.end_cursor. Stop when has_next_page is false.
| Endpoint | Page size | Cost per page |
|---|---|---|
/v1/profiles/{username}/posts |
Instagram’s page size | 1 credit |
/v1/profiles/{username}/reels |
Instagram’s page size | 1 credit |
/v1/media/{shortcode}/comments |
limit 1 to 50 (default 24) |
2 credits |
/v1/hashtags/{tag} |
limit 1 to 50 (default 12) |
3 credits |
Cursors are opaque strings. Do not parse or build them yourself, and treat them as valid only for the same request.
Python
import requests
def all_posts(username, key, max_pages=5):
cursor, items = None, []
for _ in range(max_pages):
params = {"cursor": cursor} if cursor else {}
r = requests.get(
f"https://api.igramapi.com/v1/profiles/{username}/posts",
headers={"Authorization": f"Bearer {key}"},
params=params,
timeout=30,
)
r.raise_for_status()
data = r.json()
items += data["items"]
if not data["page"]["has_next_page"]:
break
cursor = data["page"]["end_cursor"]
return items
Node.js
async function allComments(shortcode, key, maxPages = 5) {
let cursor = null;
const items = [];
for (let i = 0; i < maxPages; i++) {
const url = new URL(`https://api.igramapi.com/v1/media/${shortcode}/comments`);
url.searchParams.set("limit", "50");
if (cursor) url.searchParams.set("cursor", cursor);
const response = await fetch(url, { headers: { Authorization: `Bearer ${key}` } });
const data = await response.json();
items.push(...data.items);
if (!data.page.has_next_page) break;
cursor = data.page.end_cursor;
}
return items;
}
Tips
- Always cap the number of pages in a loop, so a long thread cannot burn through your credits.
- Store the last cursor if you want to resume a collection later.
- For monitoring, keep the newest
shortcodeyou have seen and stop paging as soon as you reach it.
Something unclear or missing? Tell us and we will fix the page.