
# Sessions

> Remember who a visitor is between requests with a signed or encrypted token in a cookie or a header, and nothing stored on the server.

A session here is a token the server writes and the client carries back. Think of a cloakroom ticket that has your coat's description printed on it and stamped by the cloakroom: the server needs no list of tickets, it reads the ticket and checks the stamp. The token travels in a cookie the browser sends with every request, in a request header an app sets, or in your hand when it arrives some other way, such as in a message body. Nothing is stored on the server, so there is nothing to look up and nothing to clean up. [Concepts](/getting-started/concepts) explains the token vocabulary.

## Signed or sealed

You pick one of two kinds when you define a session:

| Kind | Define with          | The client can read the data | Use it when                                                                   |
| ---- | -------------------- | ---------------------------- | ----------------------------------------------------------------------------- |
| JWS  | `defineJWSSession()` | Yes, it is only signed       | The data is not secret, or other services verify the token with a public key. |
| JWE  | `defineJWESession()` | No, it is encrypted          | The data is nobody else's business. A good default for a web app.             |

Either way, nobody can change the data without your key. A JWS is a postcard with a seal: anyone who holds it can read it. A JWE is a locked envelope.

## How a session runs

You define a session once, at start-up, with its key and lifetime. In each request you call `use(event)`, which reads the token the request carries and hands you a session with its `data`. A visitor without a valid token gets an empty session whose `id` is `undefined`: no token exists until you call `update()`, typically at sign-in. `update()` issues a new token and, on a cookie session, sets the cookie on the response; `clear()` expires it again at sign-out. Expiry, an optional refresh of the cookie while the visitor is active, and the cookie layout are handled for you.

## Where the token comes from

Each request reads its token from one place, picked before the token is opened, and `session.source` says which. A **cookie** is what a browser sends back by itself, and the session writes the new cookie when the token changes. A **header**, named by `sessionHeader`, is what an app or a script sets on its own requests; the session writes nothing back, so a new token reaches the client in your response. A **token** in hand is one you pass to `use({ token })` or `open()`, from a link, a message body or a queue. A request that sends both a header and a cookie holding the same token runs on the cookie; one whose header holds another token runs on the header alone. [Configure a session](/sessions/configure#accept-the-token-in-a-header) has the full rule, and [Sessions without cookies](/sessions/api-clients) the flows that rely on it.

## Pick your framework

| Your framework                                             | Start here                                                                      |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------- |
| Any handler that gets a `Request` and returns a `Response` | [Read, update and clear](/sessions/use)                                         |
| A client without cookies: a CLI, a service, a mobile app   | [Sessions without cookies](/sessions/api-clients)                               |
| h3                                                         | [Sessions on h3](/examples/h3)                                                  |
| Nitro                                                      | [Sessions on Nitro](/examples/nitro)                                            |
| Nuxt                                                       | [Sessions on Nuxt](/examples/nuxt)                                              |
| Hono, Cloudflare Workers                                   | [Sessions on Hono and Workers](/examples/hono)                                  |
| Another framework with its own cookie API                  | [Write a driver](/sessions/drivers), [the full recipe](/examples/custom-driver) |
| A session whose data lives in your own store               | [Build a session of your own](/sessions/custom-session)                         |

## Where to go next

::card-group
::card{title="Configure a session" icon="i-lucide-settings" to="/sessions/configure"}
Key, lifetime, refresh, cookie name and attributes, and the session header.
::
::card{title="Read, update and clear" icon="i-lucide-refresh-cw" to="/sessions/use"}
Sign in, read the session, change it, sign out, and mint tokens without a request.
::
::card{title="Hooks" icon="i-lucide-webhook" to="/sessions/hooks"}
Run your code when a session is read, issued, cleared, expires or fails, and rotate keys.
::
::card{title="Sessions without cookies" icon="i-lucide-terminal" to="/sessions/api-clients"}
Command-line tools, service accounts and apps that send the token in a header, and tokens in links.
::
::card{title="Write a driver" icon="i-lucide-cable" to="/sessions/drivers"}
Run sessions on a framework that exposes cookies its own way.
::
::card{title="Build a session of your own" icon="i-lucide-blocks" to="/sessions/custom-session"}
Another kind of session, such as one backed by your store, that reads and writes its token like the built-in ones.
::
::
