{"record":{"id":"3aa96df02e560767","repo":"different-ai/openwork","slug":"failed-to-create-api-key-response-status","errorCode":null,"errorMessage":"Failed to create API key (${response.status}).","messagePattern":"Failed to create API key \\((.+?)\\)\\.","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ee/apps/den-web/app/(den)/dashboard/_components/api-keys-screen.tsx","lineNumber":176,"sourceCode":"        setError(null);\n        setCreatedKey(null);\n        setCreatedKeyName(null);\n        setCopied(false);\n        try {\n            await runReauthableAction(\"create-api-key\", async () => {\n                setCreating(true);\n                try {\n                    const { response, payload } = await requestJson(\n                        `/v1/api-keys`,\n                        {\n                            method: \"POST\",\n                            body: JSON.stringify({ name }),\n                        },\n                        12000,\n                    );\n\n                    if (!response.ok) {\n                        throw getRequestError(\n                            payload,\n                            response,\n                            `Failed to create API key (${response.status}).`,\n                        );\n                    }\n\n                    const nextKey = getCreatedKey(payload);\n                    if (!nextKey) {\n                        throw new Error(\n                            \"API key was created, but the secret was not returned.\",\n                        );\n                    }\n\n                    setCreatedKey(nextKey);\n                    setCreatedKeyName(name);\n                    setName(\"\");\n                    setShowCreateForm(false);\n                    await loadApiKeys();","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/ee/apps/den-web/app/(den)/dashboard/_components/api-keys-screen.tsx#L158-L194","documentation":"handleCreate POSTs { name } to /v1/api-keys with a 12s timeout and throws getRequestError(payload, response, 'Failed to create API key (<status>)') when the response is not ok. The server payload is passed through so validation or policy errors from Den appear in the thrown error. It exists to fail the creation UI explicitly instead of showing a success state with no key.","triggerScenarios":"POST /v1/api-keys returns non-ok: 401/403 (session lacks permission to create keys), 409/422 (duplicate or invalid key name), 429 (rate limit), or 5xx from the Den server.","commonSituations":"Creating a key with a name that already exists in the org; non-admin user attempting creation; expired session; org hitting a key-count or request quota; transient Den backend failure.","solutions":["Surface the payload's error detail to the user (e.g. duplicate name or validation message) and let them correct the input.","For 401/403, confirm the user's role allows key creation or have an admin perform it.","Retry with backoff on 429/5xx.","Re-authenticate if the status is 401 due to an expired session."],"exampleFix":"// before\nawait handleCreate(name);\n// after\ntry {\n  await handleCreate(name);\n} catch (e) {\n  if (/already exists|duplicate/i.test(e.message)) setNameError(\"That key name is already in use.\");\n  else if (e.status === 403) setNameError(\"You need admin permission to create API keys.\");\n  else setNameError(\"Key creation failed. Please try again.\");\n}","handlingStrategy":"validation","validationCode":"const trimmed = name.trim();\nif (!trimmed || trimmed.length > 128) throw new Error(\"API key name must be 1-128 characters\");\nif (existingKeys.some(k => k.name === trimmed)) throw new Error(`Key name \"${trimmed}\" already exists`);","typeGuard":"function isValidKeyName(name, existing) {\n  return typeof name === \"string\" && name.trim().length > 0 && !existing.some(k => k.name === name.trim());\n}","tryCatchPattern":"try {\n  await handleCreate(name);\n} catch (e) {\n  const status = e.status ?? Number(/\\((\\d{3})\\)/.exec(e.message)?.[1]);\n  if (status === 409 || status === 422) showFieldError(\"Name already in use or invalid\");\n  else if (status === 403) showFieldError(\"Admin permission required\");\n  else showFieldError(\"Creation failed; try again.\");\n}","preventionTips":["Validate and de-duplicate key names client-side before submitting.","Confirm the user's role permits key creation before showing the form.","Refresh the session on 401 responses instead of retrying blindly.","Disable submit while a create request is in flight to avoid duplicates."],"tags":["den","api","http","api-key"],"backgroundTag":"http-request-failed","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}