{"record":{"id":"b75614463d0ff962","repo":"jackwener/OpenCLI","slug":"csrftoken-cookie-missing-make-sure-you-are-logge","errorCode":null,"errorMessage":"csrftoken cookie missing - make sure you are logged in to Instagram","messagePattern":"csrftoken cookie missing - make sure you are logged in to Instagram","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/collection-create.js","lineNumber":27,"sourceCode":"        {\n            name: 'name',\n            required: true,\n            positional: true,\n            help: 'Name of the collection to create',\n        },\n    ],\n    columns: ['status', 'collectionId', 'collectionName', 'mediaCount'],\n    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const name = \\${{ args.name | json }};\n  if (!name || !String(name).trim()) {\n    throw new Error('Collection name cannot be empty');\n  }\n  const trimmed = String(name).trim();\n  const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';\n  if (!csrf) {\n    throw new Error('csrftoken cookie missing - make sure you are logged in to Instagram');\n  }\n  const fd = new FormData();\n  fd.append('name', trimmed);\n  fd.append('module_name', 'collection_create');\n  const res = await fetch('https://www.instagram.com/api/v1/collections/create/', {\n    method: 'POST',\n    credentials: 'include',\n    headers: {\n      'X-IG-App-ID': '936619743392459',\n      'X-CSRFToken': csrf,\n    },\n    body: fd,\n  });\n  if (!res.ok) {\n    const body = await res.text().catch(() => '');\n    throw new Error('Failed to create collection: HTTP ' + res.status + (body ? ' - ' + body.slice(0, 200) : ''));\n  }\n  const d = await res.json();","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/collection-create.js#L9-L45","documentation":"Thrown before the create-collection POST is made when the browser's document.cookie contains no csrftoken value. Instagram's private web API requires the CSRF token from this cookie in the X-CSRFToken header, so without it the tool refuses to send a doomed request. It is an early-guard error indicating the browsing session is not logged in or the cookie is inaccessible.","triggerScenarios":"Running the collection-create pipeline in a browser context where the csrftoken cookie is absent: not logged in, a freshly cleared/incognito profile, an expired session, or document.cookie not exposing the cookie (e.g. HttpOnly-set variants or wrong domain like a logged-out instagram.com landing page).","commonSituations":"Developer points the automation at instagram.com before authenticating; session expired overnight; using a fresh browser profile in CI; scraping a page where cookies were blocked or cleared by privacy extensions.","solutions":["Log in to Instagram in the browser profile used by the automation, then retry","Verify document.cookie contains csrftoken in that same context (open DevTools and check Application > Cookies)","Check you are on the instagram.com domain and not a login/redirect page","Clear stale state and re-authenticate if the session silently expired"],"exampleFix":"// before\nconst csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';\nif (!csrf) throw new Error('csrftoken cookie missing...');\n// after\nawait page.goto('https://www.instagram.com');\nawait loginIfNeeded(page); // ensure authenticated session first\nconst csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';\nif (!csrf) throw new Error('csrftoken cookie missing...');","handlingStrategy":"validation","validationCode":"const hasCsrf = () => /(?:^|;\\s*)csrftoken=/.test(document.cookie);\nif (!hasCsrf()) throw new Error('Not logged in: csrftoken cookie absent');","typeGuard":"function hasCsrfCookie(): boolean {\n  return document.cookie.split(';').some((c) => c.trim().startsWith('csrftoken='));\n}","tryCatchPattern":"try {\n  await createCollection(name);\n} catch (e) {\n  if (String(e.message).includes('csrftoken cookie missing')) {\n    await loginToInstagram();\n    return createCollection(name);\n  }\n  throw e;\n}","preventionTips":["Verify login state (csrftoken presence) before any Instagram API automation step","Use a persistent browser profile so sessions survive between runs","Add a session health-check step at the start of scripts","Avoid incognito/fresh profiles for authenticated flows"],"tags":["authentication","csrf","cookies","browser-automation"],"backgroundTag":"missing-csrf-token","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}