# Sessions

Canonical URL: https://developers.iagenify.com/sdk/concepts/sessions/
Language: en

The key is exchanged once for a JWT and a refresh token. After that the key stays where you put it, and the client renews the session on its own.

Status: exchanged once · refreshed automatically

## The order of preference {#flow}

Before each call the client picks the cheapest thing that will work.

| State | What happens |
| --- | --- |
| A cached JWT that has not expired | used as-is — no network |
| Expired, but a refresh token is cached | renewed, then used |
| Refresh fails, or nothing is cached | the key is exchanged |
| No key and no cached session | the constructor refused long before this |

Expiry is read from the token itself, not guessed from a timer, so a process that was asleep for a day behaves the same as one that was busy.

## What is cached {#cached}

```javascript
{ jwt, refreshToken, sessionId, storageId }
```

`storageId` is how a machine keeps the same identity across restarts: it is sent back on the next exchange so the same machine is recognised rather than registered twice.

```javascript
await api.session();
// { id, storageId } — or null when nothing has been exchanged yet
```

## Concurrent first calls {#dedup}

Fire ten calls at once on a cold client and there is one exchange, not ten. The deduplication is keyed by the key and the `appName`, and it holds across client instances in the same process — which is what makes a double render harmless.

```javascript
// One exchange, ten calls
await Promise.all([
  api.getUser(),
  api.catalogue(),
  api.storage.list(),
  api.keys.list(),
  /* … */
]);
```

## Ending a session {#ending}

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

> **Logging out is not the same as revoking a key**
>
> A session ends; the key still works and the next call exchanges it again. To stop a key entirely, revoke the key.

```python
# Stored in ~/.iagenify/session (or your session_path / storage)
{"jwt": ..., "refresh_token": ..., "session_id": ..., "storage_id": ...}
```

```python
api.session()
# {"id", "storageId"} — or None when nothing has been exchanged yet
```

```python
# One exchange, ten calls: the first call exchanges the key, the others reuse it
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    user, prices, storages, keys = pool.map(lambda call: call(), [
        api.get_user,
        api.catalogue,
        api.storage.list,
        api.keys.list,
    ])
```

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