usebruno/bruno · error · Error

cookieObject.key (name) is required

Error message

cookieObject.key (name) is required

What it means

Thrown by setCookie CASE 2 (object form) when the cookie object has neither a 'key' nor a 'name' field. The code first aliases obj.name to obj.key, so providing either works; failing both, it rejects the object.

Source

Thrown at packages/bruno-requests/src/cookies/index.ts:323

          if (!cookieName) throw new Error('Cookie name is required');

          const cookie = new Cookie(
            hasHostPrefix(cookieName)
              ? { key: cookieName, value: cookieValue }
              : { key: cookieName, value: cookieValue, domain: new URL(url).hostname }
          );

          cookieJar.setCookieSync(cookie, url, { ignoreError: true });
          return;
        }

        // CASE 2: cookie object provided
        if (typeof nameOrCookieObj === 'object' && nameOrCookieObj !== null) {
          const obj = { ...(nameOrCookieObj as any) } as any;

          if (!obj.key && obj.name) obj.key = obj.name;
          if (!obj.key) throw new Error('cookieObject.key (name) is required');

          const defaults = hasHostPrefix(obj.key) ? {} : { domain: new URL(url).hostname };
          const base = { ...defaults, ...obj } as any;

          const processedCookie = createCookieObj(base);
          const cookie = new Cookie(processedCookie);
          cookieJar.setCookieSync(cookie, url, { ignoreError: true });
          return;
        }

        // If we reach here, arguments were invalid
        throw new Error('Invalid arguments passed to setCookie');
      };

      if (callback) {
        // Callback mode
        try {
          executeSetCookie();

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Include a 'key' (or 'name') field on the cookie object.
  2. If the source uses a different field name, map it to 'key' before calling setCookie.
  3. Validate the object has a key/name property at the boundary.

Example fix

// before
cookieJar.setCookie(url, { value: 'abc', domain: '.example.com' });

// after
cookieJar.setCookie(url, { key: 'session', value: 'abc', domain: '.example.com' });
// or equivalently:
cookieJar.setCookie(url, { name: 'session', value: 'abc', domain: '.example.com' });
Defensive patterns

Strategy: type-guard

Validate before calling

function normalizeCookieObj(obj) {
  if (!obj.key && obj.name) obj.key = obj.name;
  if (!obj.key) throw new Error('Cookie object needs key or name');
  return obj;
}
cookieJar.setCookie(url, normalizeCookieObj(raw));

Type guard

function isCookieObjectReady(o) {
  return o != null && typeof o === 'object' && !Array.isArray(o)
    && (typeof o.key === 'string' && o.key.length > 0 || typeof o.name === 'string' && o.name.length > 0);
}

Try / catch

try { cookieJar.setCookie(url, obj); }
catch (e) { if (e.message === 'cookieObject.key (name) is required') { /* set key */ } else throw e; }

Prevention

When it happens

Trigger: Calling setCookie(url, { value: 'x', domain: '.example.com' }) — the object has no key or name. Also when the object is something like { expires: ... } with the key field accidentally omitted.

Common situations: Cookie object built from a Set-Cookie parser that used a different key (e.g. 'cookieName'); partial object literal; typo like 'Key' or 'keu'.

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/84702a129dd93055. Report an issue: GitHub.