# session()

Canonical URL: https://developers.iagenify.com/sdk/ref/iagenify/session/
Language: en

`api.session() → Promise<{ id, storageId } | null>`

Which session this client is using. It reads the cache first, and asks the server only when the cache cannot say.

Status: cache first · never throws · null before the first exchange

## Returns {#returns}

```javascript
{
  id: "…",          // the session id
  storageId: "…"    // the storage this session is bound to, or null
}
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | What [revokeSession()](/sdk/ref/auth/revoke-session) takes. |
| `storageId` | string|null |  | Which storage the server recognised this process as. For a machine, this is how it keeps one identity across restarts. |

## How it answers {#order}

| State of the cache | What happens |
| --- | --- |
| A session id is cached | returned straight away — no network |
| A token is cached but no id | the active sessions are listed, and the one bound to the same storage is matched |
| That listing fails | null — the failure is swallowed, not thrown |
| Nothing is cached | null, and nothing is exchanged to find out |

The second row is why this method can be slower than it looks on a client restored from an older cache. The first row is the normal case.

> **It does not create a session, and it never throws**
>
> On a cold client it answers null. Make any ordinary call first — getUser() is the cheapest — and the exchange happens there. Because it swallows its own failure, a null never tells you the network is down; it tells you the method could not name a session.

```javascript
const fresh = new IaGenify({ apiKey });

await fresh.session();      // null — nothing has been exchanged
await fresh.getUser();      // this is what exchanges
await fresh.session();      // { id, storageId }
```

## What it is for {#use}

- Showing which session a process is running as — in a dashboard, or in one log line at boot.
- Passing the id to [revokeSession()](/sdk/ref/auth/revoke-session), including to end this very session from somewhere else.
- Reading `storageId` to confirm the server recognised this machine as the one you expected, rather than registering a second.

### One line at boot

```javascript
const [user, session] = await Promise.all([api.getUser(), api.session()]);

console.log(
  `iagenify · ${user?.email} · session ${session?.id ?? "none"} · storage ${session?.storageId ?? "none"}`,
);
```

### Confirming one machine, not many

A machine keeps the same identity by sending its `storageId` back on the next exchange. When a deployment loses its session cache on every restart, each boot registers a new one — and that shows up as a pile of near-identical storages.

```javascript
const before = (await api.session())?.storageId;

// … restart the process, with the same sessionPath …

const after = (await api.session())?.storageId;
// Same value: one machine. A different one: the session cache did not survive.
```

> **A container with no persistent session path registers once per boot**
>
> Mount the session path, or pass a storage you control. Otherwise each restart is a new machine to the server, and the identity you were relying on changes under you.

## Where sessions do not persist {#worker}

In a runtime without durable storage the session lives in memory, for as long as the isolate does — so `session()` answers for this isolate and nothing more. That is a property of the store, not of this method, and a store you supply changes it.

## Ending it {#ending}

```javascript
const current = await api.session();

await api.auth.logout();              // this one
await api.auth.revokeSession(id);     // another, by id
```

Note the asymmetry: this method gives you *your* id. Another machine's id is not something the SDK lists — when you want a machine shut out, revoke the key it holds.

- [Sessions](/sdk/concepts/sessions): The exchange, the refresh, and what is cached.
- [Session storage](/sdk/concepts/session-storage): Where the cache lives, per runtime, and how to replace it.
- [revokeSession()](/sdk/ref/auth/revoke-session): Ending a session by id.

```python
{
    "id": "…",          # the session id
    "storageId": "…",   # the storage this session is bound to, or None
}
```

```python
fresh = IaGenify(api_key=api_key)

fresh.session()     # None — nothing has been exchanged
fresh.get_user()    # this is what exchanges
fresh.session()     # {"id", "storageId"}
```

```python
user, session = api.get_user(), api.session()

print(
    f"iagenify · {(user or {}).get('email')} · "
    f"session {(session or {}).get('id', 'none')} · storage {(session or {}).get('storageId') or 'none'}"
)
```

```python
before = (api.session() or {}).get("storageId")

# … restart the process, with the same session_path …

after = (api.session() or {}).get("storageId")
# Same value: one machine. A different one: the session cache did not survive.
```

```python
current = api.session()

api.auth.logout()                    # this one
api.auth.revoke_session(session_id)  # another, by id
```
