{"record":{"id":"a44b07ad970e3d08","repo":"jackwener/OpenCLI","slug":"failed-to-create-collection-http-res-status-b","errorCode":null,"errorMessage":"Failed to create collection: HTTP ${res.status}${body ? ' - ' + body.slice(0, 200) : ''}","messagePattern":"Failed to create collection: HTTP (.+?)(.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/collection-create.js","lineNumber":43,"sourceCode":"  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();\n  if (d?.status && d.status !== 'ok') {\n    throw new Error('Instagram returned non-ok status: ' + JSON.stringify(d).slice(0, 300));\n  }\n  return [{\n    status: 'Created',\n    collectionId: String(d?.collection_id ?? ''),\n    collectionName: String(d?.collection_name ?? trimmed),\n    mediaCount: d?.collection_media_count ?? 0,\n  }];\n})()\n` },\n    ],\n});\n","sourceCodeStart":25,"sourceCodeEnd":59,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/collection-create.js#L25-L59","documentation":"Thrown when the POST to /api/v1/collections/create/ returns a non-2xx HTTP status. The error includes the status code and the first 200 characters of the response body to expose Instagram's reason (rate limit, login required, bad request). It is the generic transport-level failure for the create call.","triggerScenarios":"Calling the create pipeline with an invalid/expired CSRF token (403), unauthenticated session (401/403), rate limiting (429), Instagram changing the endpoint, or a malformed form payload rejected by the server.","commonSituations":"Session expired mid-run causing 403 'Please wait a few minutes' or 'login_required' bodies; hitting API rate limits after many rapid creates; Instagram renaming the private API version.","solutions":["Read the appended body slice to identify Instagram's error reason","Re-authenticate / refresh csrftoken if the body mentions login_required or 403","Wait and retry with backoff if status is 429 (rate limited)","Verify the endpoint is still valid (Instagram private API changes)"],"exampleFix":"// before\nthrow new Error('Failed to create collection: HTTP ' + res.status + ...);\n// after\nif (res.status === 429) { await sleep(60000); return createCollection(name); } // retry on rate limit\nthrow new Error('Failed to create collection: HTTP ' + res.status + ...);","handlingStrategy":"try-catch","validationCode":"// Pre-flight: confirm session is authenticated before calling\nconst authed = await page.evaluate(() => document.cookie.includes('csrftoken='));\nif (!authed) throw new Error('Login required before creating collections');","typeGuard":"function isAuthedContext(cookies: string): boolean {\n  return /(?:^|;\\s*)csrftoken=[^;]+/.test(cookies);\n}","tryCatchPattern":"try {\n  await createCollection(name);\n} catch (e) {\n  const m = String(e.message).match(/HTTP (\\d+)/);\n  if (m && (m[1] === '401' || m[1] === '403')) { await relogin(); return createCollection(name); }\n  if (m && m[1] === '429') { await sleep(60000); return createCollection(name); }\n  throw e;\n}","preventionTips":["Check the status code in the error message before choosing a fix","Throttle create calls to avoid 429 rate limits","Keep sessions fresh; re-login on 401/403","Log full response bodies for diagnosing Instagram API changes"],"tags":["http","network","instagram-api","rate-limit"],"backgroundTag":"http-request-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}