different-ai/openwork · error · Error

Failed to create API key (${response.status}).

Error message

Failed to create API key (${response.status}).

What it means

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.

Source

Thrown at ee/apps/den-web/app/(den)/dashboard/_components/api-keys-screen.tsx:176

        setError(null);
        setCreatedKey(null);
        setCreatedKeyName(null);
        setCopied(false);
        try {
            await runReauthableAction("create-api-key", async () => {
                setCreating(true);
                try {
                    const { response, payload } = await requestJson(
                        `/v1/api-keys`,
                        {
                            method: "POST",
                            body: JSON.stringify({ name }),
                        },
                        12000,
                    );

                    if (!response.ok) {
                        throw getRequestError(
                            payload,
                            response,
                            `Failed to create API key (${response.status}).`,
                        );
                    }

                    const nextKey = getCreatedKey(payload);
                    if (!nextKey) {
                        throw new Error(
                            "API key was created, but the secret was not returned.",
                        );
                    }

                    setCreatedKey(nextKey);
                    setCreatedKeyName(name);
                    setName("");
                    setShowCreateForm(false);
                    await loadApiKeys();

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Surface the payload's error detail to the user (e.g. duplicate name or validation message) and let them correct the input.
  2. For 401/403, confirm the user's role allows key creation or have an admin perform it.
  3. Retry with backoff on 429/5xx.
  4. Re-authenticate if the status is 401 due to an expired session.

Example fix

// before
await handleCreate(name);
// after
try {
  await handleCreate(name);
} catch (e) {
  if (/already exists|duplicate/i.test(e.message)) setNameError("That key name is already in use.");
  else if (e.status === 403) setNameError("You need admin permission to create API keys.");
  else setNameError("Key creation failed. Please try again.");
}
Defensive patterns

Strategy: validation

Validate before calling

const trimmed = name.trim();
if (!trimmed || trimmed.length > 128) throw new Error("API key name must be 1-128 characters");
if (existingKeys.some(k => k.name === trimmed)) throw new Error(`Key name "${trimmed}" already exists`);

Type guard

function isValidKeyName(name, existing) {
  return typeof name === "string" && name.trim().length > 0 && !existing.some(k => k.name === name.trim());
}

Try / catch

try {
  await handleCreate(name);
} catch (e) {
  const status = e.status ?? Number(/\((\d{3})\)/.exec(e.message)?.[1]);
  if (status === 409 || status === 422) showFieldError("Name already in use or invalid");
  else if (status === 403) showFieldError("Admin permission required");
  else showFieldError("Creation failed; try again.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/3aa96df02e560767. Report an issue: GitHub.