Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Instagram

The package has three Instagram entry points. resolve returns media for one post. listInstagramPosts returns a page of an account’s posts. getInstagramViewer checks a session. The first two use Instagram’s logged-out web endpoint and require no login. With a sessionid cookie they sign in instead; see Signed-in requests.

Resolve a post

resolve accepts /p/<code>/, /reel/<code>/, and /tv/<code>/ URLs.

import { resolve } from "@totallynotdavid/downloader";

const result = await resolve("https://www.instagram.com/p/DcepzLhTqxC/");

The result contains the post’s media at the first available full-resolution candidate. A carousel has one item per child. result.headers contains the User-Agent and Referer needed when fetching Instagram media.

An account URL or a URL without one of the supported post paths rejects with a ParseError.

List an account’s posts

import { listInstagramPosts } from "@totallynotdavid/downloader";

const first = await listInstagramPosts("uni_oficial");
for (const post of first.posts) {
  console.log(post.shortcode, post.type, post.author);
}

if (first.cursor) {
  const next = await listInstagramPosts("uni_oficial", {
    cursor: first.cursor,
  });
}

listInstagramPosts(username, options?) returns up to 12 posts in grid order. The result type is exported from the package root:

type ListPostsOptions = ResolveOptions & {
  cursor?: string;
  detail?: boolean;
};

type Post = {
  shortcode: string;
  url: string;
  author: string;
  caption?: string;
  type: "image" | "video" | "carousel";
  thumbnail: string;
  media?: MediaItem[];
  timestamp?: number;
  likes?: number;
  comments?: number;
  views?: number;
  resolved?: MediaResult;
};

type PostPage = {
  posts: Post[];
  cursor?: string;
};

Username and cursor

A leading @ is optional. The username that remains must match [A-Za-z0-9._]{1,30}. Invalid usernames and malformed cursors reject before a request.

Pass cursor unchanged to fetch the next page. It is absent on the last page. A cursor works only in the mode that returned it: a signed-out cursor with a sessionid, or a signed-in cursor without one, rejects with a ParseError. An unknown account rejects with a NetworkError whose statusCode is 404. An account with no posts returns { posts: [] }.

url is an Instagram reel URL for reels and a post URL for other items. author is the first author returned for that grid item, so a collab post can belong to another account. caption is absent when the post has no caption. thumbnail is the cover image URL. likes, comments and views are present when the listing returns them; views is the play count. resolved is the MediaResult that resolve(url) would return, set when the listing already holds the full media, so a caller can download the post without resolving it again.

Detail

The regular account response contains no media URLs or timestamp. Set detail: true to fill media with the same items returned by resolve and to fill timestamp with Unix seconds. This adds one request per post. A failure rejects the whole page.

List saved posts

const page = await listInstagramSaved("me", { cookies, cursor, detail });

listInstagramSaved lists the posts the session’s account saved, newest save first, 21 to a page, through GET /api/v1/feed/saved/posts/?max_id=<cursor>. It returns the same PostPage as listInstagramPosts. Only the account itself can see its saved posts, so a sessionid is required: without one it rejects with a BlockedError before any request. The first page checks the username against the session’s viewer and rejects with a NetworkError whose statusCode is 401 when they differ. The feed returns full media nodes, so every post with media carries resolved and detail: true costs no extra request. The feed gives no comment count, so comments is absent.

const page = await listInstagramSaved("me", { cookies, collection: "179" });

collection lists one named collection instead, through GET /api/v1/feed/collection/<id>/posts/?max_id=<cursor>, 12 to a page, with the same checks and post shape. The ID is the number that ends the collection’s URL, instagram.com/<you>/saved/<name>/<id>/; an ID that is not a number rejects with a ParseError before any request.

List comments

const page = await listInstagramComments(url, { cookies, cursor });
const replies = await listInstagramReplies(url, comment.id, {
  cookies,
  cursor,
});

listInstagramComments lists a post’s top-level comments, 15 to a page, through GET /api/v1/media/<id>/comments/?min_id=<cursor>. Instagram answers this endpoint only for a signed-in viewer, so a sessionid is required: without one it rejects with a BlockedError before any request. A URL without a post shortcode rejects with a ParseError. A page holds comments and, while more remain, the cursor for the next page.

Each Comment holds id, author (the username), text, timestamp in Unix seconds, likes and replies, the reply count. listInstagramReplies lists the replies to one comment through GET /api/v1/media/<id>/comments/<comment id>/child_comments/?min_id=<cursor>, about 9 to a page, with the same checks. A reply carries parent, the ID of the comment it answers.

Anonymous access

Instagram can block anonymous requests from datacenter IPs. A block is reported as BlockedError; retrying from the same IP rarely helps.

The library uses the runtime’s global fetch. Configure a residential proxy in the runtime when it supports proxy environment variables. Node 24 or later requires NODE_USE_ENV_PROXY=1 for that configuration.

See Errors for all Instagram block cases.

Signed-in requests

A blocked IP or a private account needs a session. Pass the browser’s Instagram cookies as cookies; the sessionid cookie alone is enough. The other cookie the API needs, ds_user_id, is read from the start of the sessionid.

import {
  getInstagramViewer,
  listInstagramPosts,
  resolve,
} from "@totallynotdavid/downloader";

const cookies = { sessionid: process.env.IG_SESSIONID ?? "" };

const viewer = await getInstagramViewer({ cookies });
console.log(`signed in as @${viewer.username}`);

const page = await listInstagramPosts("natgeo", { cookies, detail: true });
const post = await resolve(page.posts[0].url, { cookies });

getInstagramViewer(options) returns { id, username } for the session. It makes one request and is the cheapest way to check that a session still works.

Signed in, listInstagramPosts reads the account’s grid the way the web app does, and detail: true adds no request because the signed-in grid already carries the media. resolve reads the post from the v1 media API.

A session that is missing, signed out or expired rejects with a BlockedError. Its statusCode is 401, or the redirect status when Instagram sends the request to the login page. Treat a session as a password: anyone who holds the sessionid can act as the account.