{"record":{"id":"7ca17bd5bfa10c33","repo":"n8n-io/n8n","slug":"failed-to-fetch-template-id-response-status","errorCode":null,"errorMessage":"Failed to fetch template ${id}: ${response.status} ${response.statusText}","messagePattern":"Failed to fetch template (.+?): (.+?) (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/ai-workflow-builder.ee/src/tools/web/templates.ts","lineNumber":106,"sourceCode":"\treturn data;\n}\n\n/**\n * Fetch a specific workflow template by ID from n8n API\n */\nexport async function fetchTemplateByID(id: number): Promise<TemplateFetchResponse> {\n\tconst url = `${N8N_API_BASE_URL}/workflows/templates/${id}`;\n\n\tconst response = await fetch(url, {\n\t\tmethod: 'GET',\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\tAccept: 'application/json',\n\t\t},\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to fetch template ${id}: ${response.status} ${response.statusText}`);\n\t}\n\n\tconst data: unknown = await response.json();\n\tif (!isTemplateFetchResponse(data)) {\n\t\tthrow new Error(`Invalid response format from template ${id} API`);\n\t}\n\treturn data;\n}\n\n/**\n * Result of fetching workflows from templates\n */\nexport interface FetchWorkflowsResult {\n\tworkflows: WorkflowMetadata[];\n\ttotalFound: number;\n\ttemplateIds: number[];\n}\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/ai-workflow-builder.ee/src/tools/web/templates.ts#L88-L124","documentation":"Thrown by fetchTemplateByID when the GET to https://api.n8n.io/api/workflows/templates/{id} returns a non-2xx status (response.ok is false). The message embeds the template id and the HTTP status/statusText for diagnosis. This is the upstream counterpart to [242]; it fires before any body parsing.","triggerScenarios":"Requesting a template id that does not exist (404), api.n8n.io is degraded (5xx), rate-limited (429), or the id passed in is NaN/garbage producing an unexpected path. Any non-2xx from the templates endpoint reaches this throw.","commonSituations":"The AI workflow builder resolves a template id from search results that has since been deleted; a stale bookmark/template id is hardcoded; api.n8n.io has a partial outage; a network middlebox returns a 502.","solutions":["Read the embedded status code: 404 means the id is gone, 5xx means retry later, 429 means back off.","If the id came from a previous search call, re-run the search to get a fresh id.","For transient 5xx/429, retry with exponential backoff.","Confirm the id is a valid number and the template still exists on https://n8n.io/workflows."],"exampleFix":"// before\nif (!response.ok) {\n  throw new Error(`Failed to fetch template ${id}: ${response.status} ${response.statusText}`);\n}\n\n// after - retry once on transient failures\nif (!response.ok) {\n  if (response.status >= 500 || response.status === 429) {\n    throw new OperationalError(`Template ${id} unavailable (${response.status}), retry later`);\n  }\n  throw new Error(`Failed to fetch template ${id}: ${response.status} ${response.statusText}`);\n}","handlingStrategy":"try-catch","validationCode":"// Validate the id is a positive integer before calling\nfunction assertTemplateId(id: number) {\n  if (!Number.isInteger(id) || id <= 0) {\n    throw new Error(`Invalid template id: ${id}`);\n  }\n}","typeGuard":"const isOkStatus = (status: number) => status >= 200 && status < 300;","tryCatchPattern":"try {\n  const tpl = await fetchTemplateByID(id);\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e);\n  if (/Failed to fetch template .*: 404/.test(msg)) {\n    // template gone — pick another from search results\n  } else if (/: 5\\d\\d/.test(msg) || /: 429/.test(msg)) {\n    // transient — retry with backoff\n  } else {\n    throw e;\n  }\n}","preventionTips":["Re-resolve template ids from a fresh search call before deep-linking.","Treat 404 as permanent and 5xx/429 as transient when classifying retries.","Cache template payloads briefly so a single upstream blip does not cascade."],"tags":["api","templates","http-status","network","ai-workflow-builder"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}