jackwener/OpenCLI · error

Post index ${(idx + 1)} not found

Error message

Post index ${(idx + 1)} not found

What it means

Thrown when the validated index (idx = index - 1) is greater than or equal to the number of items in the fetched feed, i.e. the user's profile has fewer posts than the requested position. The 1-based index is echoed back in the message.

Source

Thrown at clis/instagram/comment.js:39

        { evaluate: `(async () => {
  const username = \${{ args.username | json }};
  const commentText = \${{ args.text | json }};
  const idx = \${{ args.index }} - 1;
  if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');
  const headers = { 'X-IG-App-ID': '936619743392459' };
  const opts = { credentials: 'include', headers };
  async function readInstagramJson(response, label) {
    try {
      return await response.json();
    } catch {
      throw new Error(label + ' returned invalid JSON');
    }
  }
  function getPostFromFeed(feed, label) {
    if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {
      throw new Error(label + ' returned malformed items payload');
    }
    if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');
    const post = feed.items[idx];
    const pkRaw = post?.pk ?? post?.id;
    const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');
    if (!/^\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');
    return { pk };
  }
  function assertOkStatus(payload, label) {
    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {
      throw new Error(label + ' returned no success evidence');
    }
  }

  // web_profile_info answers HTTP 400 for business accounts; feed-by-username needs no user id. See #2234.
  const r1 = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=' + (idx + 1), opts);
  if (!r1.ok) throw new Error(r1.status === 404 ? 'User not found: ' + username : 'HTTP ' + r1.status + ' - make sure you are logged in to Instagram');
  const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');

  const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Check the target account's post count and choose an index within range (1..postCount)
  2. Handle the error gracefully in your automation: skip the account or clamp the index
  3. Verify you are querying the right username — a similar/renamed account may have fewer posts
  4. Log in with valid cookies so private accounts' items are actually returned

Example fix

// before
const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');
// after
const feed = await readInstagramJson(r1, 'Instagram feed-by-username');
if (!Array.isArray(feed?.items) || feed.items.length <= idx) {
  return [{ status: 'Skipped', user: username, reason: 'fewer than ' + (idx + 1) + ' posts available' }];
}
const { pk } = getPostFromFeed(feed, 'Instagram feed-by-username');
Defensive patterns

Strategy: validation

Validate before calling

const feed = await readInstagramJson(r1, 'feed');
const count = Array.isArray(feed?.items) ? feed.items.length : 0;
if (idx >= count) {
  console.warn(`Only ${count} posts available for ${username}; index ${idx + 1} skipped`);
}

Type guard

function hasPostAtIndex(feed, idx) {
  return Array.isArray(feed?.items) && idx < feed.items.length;
}

Try / catch

try {
  await runCommentPipeline(args);
} catch (e) {
  if (/Post index \d+ not found/.test(e.message)) {
    console.warn('Skipping account: not enough posts');
  } else throw e;
}

Prevention

When it happens

Trigger: Requesting e.g. index 10 when the account has only 4 posts; fetch with count=idx+1 returns fewer items than requested because the account has fewer posts, is private, or is empty.

Common situations: Automating 'comment on the Nth latest post' for accounts with low post counts; new or cleaned-up accounts; querying a private account where items come back empty.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/cf05a56c0f7dbf4e. Report an issue: GitHub.