> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ollie.shop/llms.txt
> Use this file to discover all available pages before exploring further.

# Session Storage

> Share small pieces of state between your functions during a shopper's session, using the familiar sessionStorage API.

Functions can read and write a per-session `sessionStorage`, the same Web Storage API you already know from the browser. Use it to **share data between functions across a shopper's session**, so work one function already did can be reused by the functions that run after it, instead of being repeated.

It works **automatically**: the runtime exposes a `sessionStorage` global to every function, loads it with the session's current values before your function runs, and saves any changes back when it finishes. You don't import it, configure it, or return it.

## What it's for

The main win is **avoiding redundant requests**. Without shared storage, every function that needs the same data ends up calling the same upstream API again. With `sessionStorage`, the first function fetches it once, caches it on the session, and every later function in that session reads it straight from storage, with no extra round-trips.

The common shape is **resolver → enricher**:

* A **resolver** function calls the API (or computes a result) once and caches it on the session.
* One or more **enricher** functions later read that cache and use it, without hitting the API again.

For example, a request function fetches the coupon catalog once per cart and caches it; the response functions that build the order read that same cache instead of each re-fetching the catalog.

## How to use it

Use it exactly like browser `sessionStorage`. Because the runtime injects it as a global, declare its type locally so TypeScript is happy:

```ts theme={"system"}
// Resolver: compute once and cache it
import type { CustomFunction } from "@ollie-shop/functions";

declare const sessionStorage: {
  setItem(key: string, value: string): void;
};

export const handler: CustomFunction = async ({ req }) => {
  const catalog = await fetchCatalog(req);
  sessionStorage.setItem("coupon_catalog", JSON.stringify(catalog));
  return Response.json(catalog);
};
```

```ts theme={"system"}
// Enricher: a later function reads it back
import type { CustomFunction } from "@ollie-shop/functions";

declare const sessionStorage: {
  getItem(key: string): string | null;
};

export const handler: CustomFunction = async ({ res }) => {
  const cached = sessionStorage.getItem("coupon_catalog");
  const catalog = cached ? JSON.parse(cached) : null;
  // ...use catalog to enrich the response
};
```

## Notes

* **Persists across the session.** Values stay available across function invocations within the same shopper's session, which is what makes the resolver's result reusable downstream. They're never shared between shoppers.
* **Strings only.** Store objects with `JSON.stringify` and read them back with `JSON.parse`.

## Related

* [Functions](./functions.mdx)
* [Request Functions](./request-functions.mdx)
* [Response Functions](./response-functions.mdx)
