# revokeSession()

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

`api.auth.revokeSession(id) → Promise<{ ok: true }>`

Ends one of the account's sessions, by id. A machine that was signed in stops being signed in.

Status: DELETE /api/auth/sessions/:id · free · same account only

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string | required | The session id. Yours comes from [api.session()](/sdk/ref/iagenify/session). |

## Returns {#returns}

```javascript
{ ok: true }
```

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| session_not_found | 404 | No active session of this account carries that id. |

A session belonging to another account answers the same `404`. The lookup is scoped to the caller, so one account cannot discover — or end — another's sessions, and the refusal says nothing about whether that id exists elsewhere.

## Revoking twice {#idempotent}

The second call answers `404`, not `{ ok: true }`: only an active session matches. When all you wanted was for the session to be gone, that `404` is success.

```javascript
import { IaGenifyError } from "iagenify";

async function ensureRevoked(api, id) {
  try {
    await api.auth.revokeSession(id);
    return "revoked";
  } catch (err) {
    if (err instanceof IaGenifyError && err.status === 404) return "already gone";
    throw err;
  }
}
```

## Revoking your own session {#own}

Nothing stops you: pass the id `api.session()` gave you and this client's session ends. The effect is `logout()`'s, reached from the other direction — and the same thing follows, namely that the next call exchanges the key and carries on.

```javascript
const mine = await api.session();
await api.auth.revokeSession(mine.id);   // same effect as api.auth.logout()
```

## Where another session's id comes from {#ids}

Not from the SDK. This resource ends a session by id; it does not enumerate them. In practice that leaves two honest patterns.

- **Record it where the session is made.** A process that knows its own id at boot can publish it somewhere your operator tooling reads.
- **Revoke the key instead.** When you cannot name the session, name the key — and the machine cannot come back either, which is usually the actual goal.

```javascript
// At boot, each worker records who it is
const me = await api.session();
await registry.set(`worker:${process.env.HOSTNAME}`, me.id);

// Later, from your operator tool
const id = await registry.get(`worker:${host}`);
if (id) await api.auth.revokeSession(id);
```

> **Revoking a session does not revoke the key behind it**
>
> A process still holding the key exchanges it again and comes back, under a new session. When a machine should not return, revoke the key it holds — and revoke the key first.

## The order, when you do both {#order}

```javascript
// Key first: it closes the door.
await api.keys.revoke(keyId);

// Session second: it removes whoever is already inside.
await api.auth.revokeSession(sessionId);
```

Reversed, there is a window in which the still-valid key exchanges a fresh session and the machine is back before you have finished.

- [logout()](/sdk/ref/auth/logout): Ending this client's own session.
- [api.keys.revoke()](/sdk/ref/keys/revoke): Stopping a key for good.

```python
{"ok": True}
```

```python
from iagenify import IaGenifyError

def ensure_revoked(api, session_id):
    try:
        api.auth.revoke_session(session_id)
        return "revoked"
    except IaGenifyError as error:
        if error.status == 404:
            return "already gone"
        raise
```

```python
mine = api.session()
api.auth.revoke_session(mine["id"])   # same effect as api.auth.logout()
```

```python
import os

# At boot, each worker records who it is
me = api.session()
registry.set(f"worker:{os.environ['HOSTNAME']}", me["id"])

# Later, from your operator tool
session_id = registry.get(f"worker:{host}")
if session_id:
    api.auth.revoke_session(session_id)
```

```python
# Key first: it closes the door.
api.keys.revoke(key_id)

# Session second: it removes whoever is already inside.
api.auth.revoke_session(session_id)
```
