# Session storage

Canonical URL: https://developers.iagenify.com/sdk/concepts/session-storage/
Language: en

Where the cached session is kept is decided by the runtime — a file, localStorage, or memory. You can replace it with three methods.

Status: read · write · clear

## What you get by default {#default}

| Runtime | Kept in | Survives |
| --- | --- | --- |
| node | ~/.iagenify/session, in a directory created with mode 0700 | restarts |
| browser | localStorage, under iagenify_session | reloads, per origin |
| worker | memory | only the isolate |

In Node, `sessionPath` moves the file. Everywhere else the location is fixed, and the way to change it is to supply your own storage.

## Supplying your own {#custom}

Three async methods. `read` returns what was last written, or `null`; `write` stores it; `clear` removes it.

```javascript
const api = new IaGenify({
  apiKey,
  storage: {
    storageType: "custom",          // reported at exchange; informational
    async read()      { return JSON.parse((await redis.get(key)) || "null"); },
    async write(data) { await redis.set(key, JSON.stringify(data)); },
    async clear()     { await redis.del(key); },
  },
});
```

> **read must return null, not throw**
>
> A first run has nothing stored. Catch inside read and return null, or the very first call fails instead of exchanging the key.

## A client with no key at all {#without-key}

If your storage already holds a session, you can build a client without a key. Pass neither and the constructor refuses, naming both ways out.

```javascript
// Valid: the session comes from the store
const api = new IaGenify({ storage: myStore });

// Refused at construction
new IaGenify({});
// Error: IaGenify requires an API key. Pass { apiKey } or provide a custom
// storage with an existing session.
```

## Keeping tenants apart {#isolation}

```javascript
const clientFor = (tenant) => new IaGenify({
  apiKey: keyOf(tenant),
  appName: "billing-worker",
  storage: {
    storageType: "custom",
    async read()      { return JSON.parse((await kv.get(`sess:${tenant}`)) || "null"); },
    async write(data) { await kv.put(`sess:${tenant}`, JSON.stringify(data)); },
    async clear()     { await kv.delete(`sess:${tenant}`); },
  },
});
```

```python
import json

class RedisSession:
    storage_type = "custom"          # informational
    def read(self): return json.loads(redis.get(key) or "null")
    def write(self, data): redis.set(key, json.dumps(data))
    def clear(self): redis.delete(key)

api = IaGenify(api_key=api_key, storage=RedisSession())
```

```python
# Valid: the session comes from the store
api = IaGenify(storage=my_store)

# Refused at construction
IaGenify()
# ValueError: IaGenify requires api_key or a custom session storage.
```

```python
import json

class TenantSession:
    def __init__(self, tenant): self.key = f"sess:{tenant}"
    def read(self): return json.loads(kv.get(self.key) or "null")
    def write(self, data): kv.set(self.key, json.dumps(data))
    def clear(self): kv.delete(self.key)

def client_for(tenant):
    return IaGenify(api_key=key_of(tenant), app_name="billing-worker", storage=TenantSession(tenant))
```
