# iron-session

> Secure, stateless, and cookie-based session library for JavaScript

Latest version **9.0.1** (published 2026-08-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install iron-session
pnpm add iron-session
yarn add iron-session
bun add iron-session
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 9.0.1 |
| Published | 2026-08-30 |
| First published | 2020-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22.13.0 |
| Dependencies | 2 |
| Unpacked size | 97.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | Vincent Voyer <vincent@codeagain.com> (https://github.com/vvo) |
| Maintainers | vvo, brc-dd |
| Keywords | cookie, encryption, next.js, node.js, secure, security, session, stateless |

## Links

- npm: https://www.npmjs.com/package/iron-session
- Repository: github:vvo/iron-session
- Issues: https://github.com/vvo/iron-session/issues
- Funding: https://github.com/sponsors/vvo
- npm.io page: https://npm.io/package/iron-session

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) ^2.0.1
- [iron-webcrypto](https://npm.io/package/iron-webcrypto.md) ^2.0.0

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 9.0.1 (latest) — 2026-08-30
- 9.0.0-beta.1 (beta) — 2026-08-30
- 8.0.0-alpha.0 (alpha) — 2023-05-27
- 9.0.0 — 2026-08-30
- 9.0.0-beta.0 — 2026-08-30
- 8.0.4 — 2024-11-12
- 8.0.3 — 2024-08-19
- 8.0.2 — 2024-06-14
- 8.0.1 — 2023-11-21
- 8.0.0 — 2023-11-20
- 8.0.0-beta.6 — 2023-11-18
- 8.0.0-beta.5 — 2023-11-15
- 8.0.0-beta.4 — 2023-11-08
- 8.0.0-beta.3 — 2023-11-08
- 8.0.0-beta.2 — 2023-11-08
- … 55 more at https://npm.io/package/iron-session/versions

## README

# iron-session ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/vvo/iron-session/ci.yaml) [![GitHub license](https://img.shields.io/github/license/vvo/iron-session?style=flat)](https://github.com/vvo/iron-session/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/iron-session)](https://www.npmjs.com/package/iron-session) ![npm](https://img.shields.io/npm/dm/iron-session) ![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/iron-session?exports=getIronSession)

**`iron-session` is a secure, stateless, and cookie-based session library for JavaScript.**

---

The session data is stored in signed and encrypted cookies which are decoded by your server code in a stateless fashion (= no network involved). This is the same technique used by frameworks like
[Ruby On Rails](https://guides.rubyonrails.org/security.html#session-storage).

<p align="center"><i>Online demo and examples: <a href="https://get-iron-session.vercel.app/">https://get-iron-session.vercel.app</a></i> 👀 <br/>
 <i>Featured in the <a href="https://nextjs.org/docs/app/guides/authentication">Next.js documentation</a></i> ⭐️</p>

## Table of Contents

- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Upgrading to v9](#upgrading-to-v9)
- [Usage](#usage)
- [Examples](#examples)
- [Runtimes](#runtimes)
- [Session size](#session-size)
- [Watching for unreadable cookies](#watching-for-unreadable-cookies)
- [Validating session data](#validating-session-data)
- [Project status](#project-status)
- [Session options](#session-options)
- [API](#api)
  - [`getIronSession<T>(req, res, sessionOptions): Promise<IronSession<T>>`](#getironsessiontreq-res-sessionoptions-promiseironsessiont)
  - [`getIronSession<T>(cookieStore, sessionOptions): Promise<IronSession<T>>`](#getironsessiontcookiestore-sessionoptions-promiseironsessiont)
  - [`nodeCookies`, `webCookies`, `nextProxyCookies`](#nodecookiesreq-res-webcookiesrequest-responseorheaders-nextproxycookiesrequest-response)
  - [`session.save(): Promise<void>`](#sessionsave-promisevoid)
  - [`session.destroy(): void`](#sessiondestroy-void)
  - [`session.updateConfig(sessionOptions: SessionOptions): void`](#sessionupdateconfigsessionoptions-sessionoptions-void)
  - [`sealData(data: unknown, { password, ttl }): Promise<string>`](#sealdatadata-unknown--password-ttl--promisestring)
  - [`unsealData<T>(seal: string, { password, ttl }): Promise<T>`](#unsealdatatseal-string--password-ttl--promiset)
- [FAQ](#faq)
  - [Why use pure cookies for sessions?](#why-use-pure-cookies-for-sessions)
  - [How to invalidate sessions?](#how-to-invalidate-sessions)
  - [Can I use something else than cookies?](#can-i-use-something-else-than-cookies)
  - [How is this different from JWT?](#how-is-this-different-from-jwt)
- [Credits](#credits)
- [Good Reads](#good-reads)

## Installation

```sh
pnpm add iron-session
```

v9 needs **Node 22.13 or later** and is **ESM-only**. `require()` still works on
Node 22.13+, which supports `require()` of an ES module. If you are stuck on an
older Node, stay on v8: `pnpm add iron-session@8`.

## Upgrading to v9

Most apps change two things. Both are things v8 got wrong quietly.

**1. Store timestamps, not `Date` objects.**

```diff
- session.lastSeen = new Date();
+ session.lastSeen = Date.now();
```

v8 turned a `Date` into a string when sealing, so the type you wrote was not the
type you read back. v9 throws and names the field.

**2. Handle a session that does not exist yet.**

```diff
- const userId = session.user.id;
+ const userId = session.user?.id;
```

Reads are typed as `Partial<T>` now. A first visit, an expired cookie and a
`destroy()` all leave you an empty object, so the old type let this compile and
then throw at runtime.

Nothing else is required. `getIronSession(req, res, options)` and
`getIronSession(await cookies(), options)` both still work, v9 reads v8 cookies
and v8 reads v9 cookies, so you can roll a deploy back without signing everyone
out. If you had `as any` on `await cookies()`, delete it.

Worth adopting while you are here:

- [`nextProxyCookies`](#runtimes) if you ever tried to save a session in Next.js
  middleware and it did not stick.
- [`onUnsealError`](#watching-for-unreadable-cookies) to see why cookies get
  rejected instead of guessing.
- [`chunk: true`](#session-size) if your session outgrew one cookie.

The full guide, including the removed APIs and the security fix that signs pre-v8
cookies out once, is in [MIGRATION.md](./MIGRATION.md).

## Usage

_We have extensive examples here too: https://get-iron-session.vercel.app/._

To get a session, there's a single method to know: `getIronSession`.

```ts
// Next.js API Routes and Node.js/Express/Connect.
import { getIronSession } from "iron-session";

export async function get(req, res) {
  const session = await getIronSession(req, res, { password: "...", cookieName: "..." });
  return session;
}

export async function post(req, res) {
  const session = await getIronSession(req, res, { password: "...", cookieName: "..." });
  session.username = "Alison";
  await session.save();
}
```

```ts
// Next.js Route Handlers (App Router)
import { cookies } from "next/headers";
import { getIronSession } from "iron-session";

export async function GET() {
  const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
  return session;
}

export async function POST() {
  const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
  session.username = "Alison";
  await session.save();
}
```

```tsx
// Next.js Server Components and Server Actions (App Router)
import { cookies } from "next/headers";
import { getIronSession } from "iron-session";

async function getIronSessionData() {
  const session = await getIronSession(await cookies(), { password: "...", cookieName: "..." });
  return session;
}

async function Profile() {
  const session = await getIronSessionData();

  return <div>{session.username}</div>;
}
```

```ts
// Next.js proxy.ts (middleware.ts before Next 16)
import { NextResponse, type NextRequest } from "next/server";
import { getIronSession, nextProxyCookies } from "iron-session";

export async function proxy(request: NextRequest) {
  const response = NextResponse.next();
  const session = await getIronSession(nextProxyCookies(request, response), options);

  session.lastSeen = Date.now();
  await session.save();

  return response;
}
```

Middleware needs the adapter because Next only merges a cookie into the current
render when it goes through `response.cookies.set()`. Writing a raw `Set-Cookie`
header there looks like it works and then has no effect.

## Examples

We have many different patterns and examples on the online demo, have a look: https://get-iron-session.vercel.app/.

## Runtimes

`getIronSession(req, res, options)` covers Node, Express, Connect and Next.js
API routes, and `getIronSession(await cookies(), options)` covers the Next.js App
Router. When you want to be explicit, or when your framework hands you something
else, pass an adapter instead:

| Adapter                                  | For                                                                        |
| ---------------------------------------- | -------------------------------------------------------------------------- |
| `nodeCookies(req, res)`                  | Node `http`, Express, Connect, Next.js API routes                          |
| `webCookies(request, responseOrHeaders)` | Anything web-standard: Hono, Bun, Deno, Cloudflare Workers, Route Handlers |
| `nextProxyCookies(request, response)`    | Next.js Proxy (middleware), `proxy.ts`                                     |

Anything with `get(name)` and `set(name, value, options)`, like Next's
`cookies()`, can be passed directly. If your framework has neither, a cookie jar
is two functions:

```ts
const session = await getIronSession(
  {
    read: (name) => myFramework.getCookie(name),
    write: (name, value, options) => myFramework.setCookie(name, value, options),
  },
  options,
);
```

## Session size

A browser refuses a cookie over 4096 bytes, and iron-session throws rather than
letting one be silently dropped. Encryption adds overhead, so plan for roughly
3KB of actual data.

If you need more, `chunk: true` splits the session across several cookies. Before
you reach for it, know what the real limit is: every cookie is sent on **every
request**, and proxies cap the whole `Cookie` header well below what a few
chunks produce. nginx allows 8KB by default and a CDN in front of it may allow
less. Going over returns a 400 or 431 at the edge, before your code runs.
iron-session refuses more than 4 chunks for that reason.

The scalable answer is to keep an id in the session and the data in your
database:

```ts
session.userId = user.id; // small, stateless
const user = await db.user.findUnique({ where: { id: session.userId } });
```

## Watching for unreadable cookies

When a cookie cannot be read, iron-session starts a new empty session instead of
throwing. It has to: it cannot tell a tampered cookie from a password you
rotated out or a seal that simply expired, and a 500 on every request would be
worse. That makes real problems invisible, so log them:

```ts
const options = {
  cookieName: "session",
  password: process.env.SESSION_PASSWORD,
  onUnsealError: (reason, error) => {
    // "expired" is normal, that is how sessions end.
    if (reason !== "expired") {
      logger.warn({ reason, error }, "session cookie rejected");
    }
  },
};
```

A burst of `"unknown-password"` usually means a password rotation went wrong. A
burst of `"invalid"` can mean someone is probing your cookies.

## Validating session data

There is no `validate` option, on purpose. If you change the shape of your
session, old cookies still decrypt into the old shape, and the place to handle
that is the wrapper you already have:

```ts
// lib/session.ts
export async function getSession() {
  const session = await getIronSession<Session>(await cookies(), options);

  if (session.user && !SessionSchema.safeParse({ ...session }).success) {
    session.destroy();
  }

  return session;
}
```

## Project status

✅ Production ready and maintained.

## Session options

Two options are required: `password` and `cookieName`. Everything else is automatically computed and usually doesn't need to be changed.

- `password`, **required**: Private key used to encrypt the cookie. It has to be at least 32 characters long. Use <https://1password.com/password-generator/> to generate strong passwords. `password` can be either a `string` or an `object` with incrementing keys like this: `{2: "...", 1: "..."}` to allow for password rotation. iron-session will use the highest numbered key for new cookies.
- `cookieName`, **required**: Name of the cookie to be stored
- `ttl`, _optional_: In seconds. Default to the equivalent of 14 days. Setting it to `0` means the seal never expires, which also means it can never be revoked: do not use `0` for authentication.
- `chunk`, _optional_: Split a session that does not fit in one cookie across several cookies. Defaults to `false`. See [Session size](#session-size) before turning it on.
- `onUnsealError`, _optional_: Called when an existing cookie could not be read, with a reason of `"expired"`, `"invalid"` or `"unknown-password"`. The session is reset to empty either way, so this is for logging. See [Watching for unreadable cookies](#watching-for-unreadable-cookies).
- `cookieOptions`, _optional_: Any [Set-Cookie attribute](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes) supported by [jshttp/cookie](https://github.com/jshttp/cookie). Default to:

  ```js
  {
    httpOnly: true,
    secure: true, // set this to false in local (non-HTTPS) development
    sameSite: "lax",// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#lax
    maxAge: (ttl === 0 ? 2147483647 : ttl) - 60, // Expire cookie before the session expires. A ttl of 60 or less keeps its full value.
    path: "/",
  }
  ```

## API

### `getIronSession<T>(req, res, sessionOptions): Promise<IronSession<T>>`

```ts
type SessionData = {
  // Your data
};

const session = await getIronSession<SessionData>(req, res, sessionOptions);
```

### `getIronSession<T>(cookieStore, sessionOptions): Promise<IronSession<T>>`

```ts
type SessionData = {
  // Your data
};

const session = await getIronSession<SessionData>(await cookies(), sessionOptions);
```

Reads are typed as `Partial<T>`, because a session that does not exist yet is an
empty object. Use optional chaining, or narrow once and pass the result around.

### `nodeCookies(req, res)`, `webCookies(request, responseOrHeaders)`, `nextProxyCookies(request, response)`

Cookie jars you pass in place of `req, res`, for when the shorthand cannot tell
what your framework wants. See [Runtimes](#runtimes).

```ts
import { getIronSession, nextProxyCookies } from "iron-session";

const session = await getIronSession(nextProxyCookies(request, response), sessionOptions);
```

### `session.save(): Promise<void>`

Saves the session. This is an asynchronous operation. It must be done and awaited before headers are sent to the client.

```ts
await session.save();
```

### `session.destroy(): void`

Destroys the session. This is a synchronous operation as it only removes the cookie. It must be done before headers are sent to the client.

```ts
session.destroy();
```

`destroy()` is terminal. A `save()` after it is ignored, so a logout handler that calls both still signs the user out. Writing fields back into the session and then saving throws, because the last `Set-Cookie` would win and leave the user signed in.

### `session.updateConfig(sessionOptions: SessionOptions): void`

Updates the configuration of the session with new session options. You still need to call save() if you want them to be applied.

It rebuilds the whole configuration, including the password, so this is what you use to rotate a password mid-request. In v8 a new password passed here was ignored.

### `sealData(data: unknown, { password, ttl }): Promise<string>`

This is the underlying method and seal mechanism that powers `iron-session`. You can use it to seal any `data` you want and pass it around. One usecase are magic links: you generate a seal that contains a user id to login and send it to a route on your website (like `/magic-login`). Once received, you can safely decode the seal with `unsealData` and log the user in.

### `unsealData<T>(seal: string, { password, ttl }): Promise<T>`

This is the opposite of `sealData` and allow you to decode a seal to get the original data back.

## FAQ

### Why use pure cookies for sessions?

This makes your sessions stateless: since the data is passed around in cookies, you do not need any server or service to store session data.

More information can also be found on the [Ruby On Rails website](https://guides.rubyonrails.org/security.html#session-storage) which uses the same technique.

### How to invalidate sessions?

Sessions cannot be instantly invalidated (or "disconnect this customer") as there is typically no state stored about sessions on the server by default. However, in most applications, the first step upon receiving an authenticated request is to validate the user and their permissions in the database. So, to easily disconnect customers (or invalidate sessions), you can add an `isBlocked`` state in the database and create a UI to block customers.

Then, every time a request is received that involves reading or altering sensitive data, make sure to check this flag.

### Can I use something else than cookies?

Yes, we expose `sealData` and `unsealData` which are not tied to cookies. This way you can seal and unseal any object in your application and move seals around to login users.

### How is this different from [JWT](https://jwt.io/)?

Not so much:

- JWT is a standard, it stores metadata in the JWT token themselves to ensure communication between different systems is flawless.
- JWT tokens are not encrypted, the payload is visible by customers if they manage to inspect the seal. You would have to use [JWE](https://tools.ietf.org/html/rfc7516) to achieve the same.
- @hapi/iron mechanism is not a standard, it's a way to sign and encrypt data into seals

Depending on your own needs and preferences, `iron-session` may or may not fit you.

## Credits

- [Eran Hammer and hapi.js contributors](https://github.com/hapijs/iron/graphs/contributors)
  for creating the underlying cryptography library
  [`@hapi/iron`](https://hapi.dev/module/iron/).
- [Divyansh Singh](https://github.com/brc-dd) for reimplementing `@hapi/iron` as
  [`iron-webcrypto`](https://github.com/brc-dd/iron-webcrypto) using standard
  web APIs.
- [Hoang Vo](https://github.com/hoangvvo) for advice and guidance while building
  this module. Hoang built
  [`next-connect`](https://github.com/hoangvvo/next-connect) and
  [`next-session`](https://github.com/hoangvvo/next-session).
- All the
  [contributors](https://github.com/vvo/iron-session/graphs/contributors) for
  making this project better.

## Good Reads

- <https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html>
- <https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html>

---
_Source: https://npm.io/package/iron-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
