# logout()

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

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

Ends the session this client is using. The key is untouched, so the very next call starts a new one.

Status: POST /api/auth/logout · free · this session only

## Returns {#returns}

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

There is nothing to pass: the session is identified from the token you are already sending. Its refresh token stops being renewable at the same moment — this is not a token that expires later, it is a session that is over.

## What happens on the next call {#after}

The cached token is no longer accepted, the refresh fails, and the key is exchanged. Your code keeps working, under a new session. That is rarely what someone calling `logout()` has in mind, so be deliberate about which of the three you want.

```javascript
await api.auth.logout();

await api.getUser();   // works — a different session now
```

| What you want | Call |
| --- | --- |
| This process to stop being this session | api.auth.logout() |
| This key to stop working anywhere | api.keys.revoke(id) |
| Another device to be signed out | api.auth.revokeSession(id) |

## Calling it twice {#idempotent}

The second call is made under the session the first one caused, so it answers `{ ok: true }` again — and ends that one too. It is not an error, and it is not a no-op: each call ends whatever session is current.

## When it is the right call {#when}

- A short-lived job that has finished, and you would rather not leave a session alive behind it.
- A page that signs a visitor out, where the session is the visitor's and not your server's.
- A machine being decommissioned — together with revoking the key it holds.

And when it is not: as cleanup in a long-running server. There, the session is the thing making your calls cheap — ending it on every shutdown just means exchanging the key again on the next boot.

### Ending a job's session cleanly

```javascript
try {
  await doTheWork(api);
} finally {
  // Best effort: the work is done either way, and a failed logout
  // must not become the error the caller sees.
  await api.auth.logout().catch(() => {});
}
```

> **In a browser, clear what is cached too**
>
> The session was kept in the page's own store. Ending it server-side leaves a dead entry there, which the next call will try before exchanging. Replace the client, or clear the store.

```javascript
await api.auth.logout();

// The page keeps its own copy. Clear it, or the next call tries a dead token first.
await myStore.clear();
```

- [revokeSession()](/sdk/ref/auth/revoke-session): Ending a session that is not this one.
- [api.keys.revoke()](/sdk/ref/keys/revoke): Stopping the key behind it.

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

```python
api.auth.logout()

api.get_user()   # works — a different session now
```

```python
from iagenify import IaGenifyError

try:
    do_the_work(api)
finally:
    # Best effort: the work is done either way, and a failed logout
    # must not become the error the caller sees.
    try:
        api.auth.logout()
    except IaGenifyError:
        pass
```

```python
api.auth.logout()

# A custom store keeps its own copy. Clear it, or the next call tries a dead token first.
my_store.clear()
```
