{"record":{"id":"17f5a7880a507dd9","repo":"jackwener/OpenCLI","slug":"instagram-story-publish-could-not-derive-current-u","errorCode":null,"errorMessage":"Instagram story publish could not derive current user id from browser session","messagePattern":"Instagram story publish could not derive current user id from browser session","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/instagram/_shared/private-publish.js","lineNumber":959,"sourceCode":"            })\n            : undefined,\n    });\n}\nexport async function publishStoryViaPrivateApi(input) {\n    const now = input.now ?? (() => Date.now());\n    const uploadId = String(now());\n    const fetcher = input.fetcher ?? ((url, init) => instagramPrivateApiFetch(input.page, url, init));\n    const prepareMediaAsset = input.prepareMediaAsset ?? (async (item) => item.type === 'video'\n        ? { type: 'video', asset: prepareVideoAssetForPrivateStoryUpload(item.filePath) }\n        : { type: 'image', asset: prepareImageAssetForPrivateStoryUpload(item.filePath) });\n    const prepared = await prepareMediaAsset(input.mediaItem);\n    const currentUserId = input.currentUserId\n        || ('getCookies' in input.page\n            ? String((await (input.page.getCookies?.({ domain: 'instagram.com' }) ?? Promise.resolve([])))\n                .find((cookie) => cookie.name === 'ds_user_id')?.value || '')\n            : '');\n    if (!currentUserId) {\n        throw new CommandExecutionError('Instagram story publish could not derive current user id from browser session');\n    }\n    const signedPayloadBase = {\n        _csrftoken: input.apiContext.csrfToken,\n        _uid: currentUserId,\n        _uuid: crypto.randomUUID(),\n        device: INSTAGRAM_STORY_DEVICE,\n    };\n    const buildSignedStoryPhotoBody = (width, height) => buildSignedBody({\n        ...buildConfigureToStoryPhotoPayload({\n            uploadId,\n            width,\n            height,\n            now,\n            jazoest: input.jazoest,\n        }),\n        ...signedPayloadBase,\n    });\n    const buildSignedStoryVideoBody = (width, height, durationMs) => buildSignedBody({","sourceCodeStart":941,"sourceCodeEnd":977,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/_shared/private-publish.js#L941-L977","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Log in to Instagram in the browser session before calling so ds_user_id is set.","Pass input.currentUserId explicitly instead of relying on cookie extraction.","Verify cookie extraction uses domain 'instagram.com' and the exact cookie name ds_user_id.","Ensure the page object exposes getCookies (Playwright/Puppeteer context) when not supplying currentUserId.","Re-establish the session if cookies expired — ds_user_id disappears on logout."],"exampleFix":"// before\nawait publishStoryViaPrivateApi({ page, mediaItem, apiContext });\n// after\nconst cookies = await page.context().cookies('https://www.instagram.com');\nconst currentUserId = cookies.find(c => c.name === 'ds_user_id')?.value;\nif (!currentUserId) throw new Error('Not logged in: ds_user_id cookie missing');\nawait publishStoryViaPrivateApi({ page, mediaItem, apiContext, currentUserId });","handlingStrategy":"validation","validationCode":"const cookies = await page.context().cookies('https://www.instagram.com');\nconst dsUserId = cookies.find(c => c.name === 'ds_user_id')?.value;\nif (!dsUserId) throw new Error('Not logged in to Instagram — story publish would fail');","typeGuard":"function hasCurrentUserId(input) {\n  return typeof input.currentUserId === 'string' && /^\\d+$/.test(input.currentUserId);\n}","tryCatchPattern":"try {\n  await publishStoryViaPrivateApi({ page, mediaItem, apiContext });\n} catch (e) {\n  if (/could not derive current user id/.test(e.message)) {\n    throw new Error('Instagram session missing ds_user_id — log in first');\n  }\n  throw e;\n}","preventionTips":["Log in and confirm the ds_user_id cookie exists before story publishes.","Pass currentUserId explicitly when using a non-cookie fetcher.","Re-establish sessions on a schedule; cookies expire.","Check the page object exposes getCookies when relying on the cookie fallback."],"tags":["instagram","session","authentication","cookies","story"],"backgroundTag":"missing-session-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}