jackwener/OpenCLI · error · CommandExecutionError

Instagram story publish could not derive current user id fro

Error message

Instagram story publish could not derive current user id from browser session

What it means

publishStoryViaPrivateApi needs the numeric Instagram user id of the logged-in account to sign the story configure payload (_uid). It takes input.currentUserId, or falls back to reading the ds_user_id cookie from the browser page session. If neither yields a non-empty value it throws this error, because story publishing cannot proceed without the uid.

Source

Thrown at clis/instagram/_shared/private-publish.js:959

            })
            : undefined,
    });
}
export async function publishStoryViaPrivateApi(input) {
    const now = input.now ?? (() => Date.now());
    const uploadId = String(now());
    const fetcher = input.fetcher ?? ((url, init) => instagramPrivateApiFetch(input.page, url, init));
    const prepareMediaAsset = input.prepareMediaAsset ?? (async (item) => item.type === 'video'
        ? { type: 'video', asset: prepareVideoAssetForPrivateStoryUpload(item.filePath) }
        : { type: 'image', asset: prepareImageAssetForPrivateStoryUpload(item.filePath) });
    const prepared = await prepareMediaAsset(input.mediaItem);
    const currentUserId = input.currentUserId
        || ('getCookies' in input.page
            ? String((await (input.page.getCookies?.({ domain: 'instagram.com' }) ?? Promise.resolve([])))
                .find((cookie) => cookie.name === 'ds_user_id')?.value || '')
            : '');
    if (!currentUserId) {
        throw new CommandExecutionError('Instagram story publish could not derive current user id from browser session');
    }
    const signedPayloadBase = {
        _csrftoken: input.apiContext.csrfToken,
        _uid: currentUserId,
        _uuid: crypto.randomUUID(),
        device: INSTAGRAM_STORY_DEVICE,
    };
    const buildSignedStoryPhotoBody = (width, height) => buildSignedBody({
        ...buildConfigureToStoryPhotoPayload({
            uploadId,
            width,
            height,
            now,
            jazoest: input.jazoest,
        }),
        ...signedPayloadBase,
    });
    const buildSignedStoryVideoBody = (width, height, durationMs) => buildSignedBody({

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log in to Instagram in the browser session before calling so ds_user_id is set.
  2. Pass input.currentUserId explicitly instead of relying on cookie extraction.
  3. Verify cookie extraction uses domain 'instagram.com' and the exact cookie name ds_user_id.
  4. Ensure the page object exposes getCookies (Playwright/Puppeteer context) when not supplying currentUserId.
  5. Re-establish the session if cookies expired — ds_user_id disappears on logout.

Example fix

// before
await publishStoryViaPrivateApi({ page, mediaItem, apiContext });
// after
const cookies = await page.context().cookies('https://www.instagram.com');
const currentUserId = cookies.find(c => c.name === 'ds_user_id')?.value;
if (!currentUserId) throw new Error('Not logged in: ds_user_id cookie missing');
await publishStoryViaPrivateApi({ page, mediaItem, apiContext, currentUserId });
Defensive patterns

Strategy: validation

Validate before calling

const cookies = await page.context().cookies('https://www.instagram.com');
const dsUserId = cookies.find(c => c.name === 'ds_user_id')?.value;
if (!dsUserId) throw new Error('Not logged in to Instagram — story publish would fail');

Type guard

function hasCurrentUserId(input) {
  return typeof input.currentUserId === 'string' && /^\d+$/.test(input.currentUserId);
}

Try / catch

try {
  await publishStoryViaPrivateApi({ page, mediaItem, apiContext });
} catch (e) {
  if (/could not derive current user id/.test(e.message)) {
    throw new Error('Instagram session missing ds_user_id — log in first');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling publishStoryViaPrivateApi where input.currentUserId is falsy AND the page object has no getCookies method (plain fetcher) or its instagram.com cookies contain no ds_user_id cookie.

Common situations: Not logged in to Instagram in the automation browser (no session cookies); passing a plain fetcher page object without getCookies; session expired/cleared so ds_user_id is gone; forgetting to pass currentUserId when not using a cookie-capable page.

Related errors


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