method

Cookie.from

static from(
name: string,
value: string,
options?: CookieInit
): Cookie;

Creates a cookie from a name, value, and optional attributes

Referenced types

interface CookieInit

class Cookie

A single HTTP cookie: its name, value, and attributes.

const cookie = new Bun.Cookie("name", "value");
console.log(cookie.toString()); // "name=value; Path=/; SameSite=Lax"
  • domain?: string

    The cookie's Domain attribute, or undefined if not set

  • expires?: Date

    The cookie's expiration date, or undefined if not set

  • httpOnly: boolean

    Whether the cookie has the HttpOnly attribute

  • maxAge?: number

    The cookie's maximum age in seconds, or undefined if not set

  • readonly name: string

    The name of the cookie

  • partitioned: boolean

    Whether the cookie has the Partitioned attribute

  • path: string

    The cookie's Path attribute. Defaults to /.

  • sameSite: CookieSameSite

    The cookie's SameSite attribute. Defaults to lax.

  • secure: boolean

    Whether the cookie has the Secure attribute

  • value: string

    The value of the cookie

  • isExpired(): boolean;

    Returns true if the cookie has expired

  • serialize(): string;

    Serializes the cookie to a string

    const cookie = Bun.Cookie.from("session", "abc123", {
      domain: "example.com",
      path: "/",
      secure: true,
      httpOnly: true
    }).serialize(); // "session=abc123; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Lax"
  • Returns the cookie's name, value, and attributes as a plain object

  • toString(): string;

    Serializes the cookie to a string. Alias of Cookie.serialize.

  • static from(
    name: string,
    value: string,
    options?: CookieInit
    ): Cookie;

    Creates a cookie from a name, value, and optional attributes

  • static parse(
    cookieString: string
    ): Cookie;

    Parses a serialized cookie string into a Cookie

    @param cookieString

    A serialized cookie string, like "name=value; Path=/"