# getUser()

Canonical URL: https://developers.iagenify.com/sdk/ref/iagenify/get-user/
Language: en

`api.getUser() → Promise<User | null>`

Who the key belongs to, and what the balance is. The shortest call that proves the whole chain works.

Status: GET /api/auth/me · free · four fields

## Returns {#returns}

```javascript
{
  id: "…",                  // the account id
  email: "you@example.com",
  credits: 10869,           // the balance, at the moment of the call
  pack: "go"                // the plan this account is on
}
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | The account. Stable; worth logging next to anything you bill back. |
| `email` | string |  | The address the account was opened with. |
| `credits` | integer |  | The balance. A reading, not a reservation — see [below](#balance). |
| `pack` | string |  | The plan. It is what decides quotas, not what decides a model's price. |

The server holds more than this; the method narrows it to the four fields a program actually branches on. When you need the wider record, [updateProfile()](/sdk/ref/auth/update-profile) returns it.

## pack {#pack}

| Value | Means |
| --- | --- |
| free | The plan an account starts on. |
| go | A paid plan. |
| starter | A paid plan, with more of everything. |
| enterprise | A paid plan: one balance shared by up to five people. |
| freemium | An older name that follows `free`. |

Read the plan to decide what to offer, never to compute a price: a model costs what [catalogue()](/sdk/ref/iagenify/catalogue) says. What the plan governs is how much room a service has — storages, mailboxes, hosts — and those limits are readable from the service that enforces them.

## When it is null {#null}

`null` means the call succeeded and carried no account. It is not what a wrong key looks like: a wrong key fails the session exchange, long before this.

| Symptom | What happened |
| --- | --- |
| Unauthorized thrown | The key was refused, or it has been revoked. |
| unreachable thrown | No answer at baseUrl. Check the address first. |
| timeout thrown | Something answered too slowly, or not at all. |
| null returned | The request worked, and there was no account behind it. |

## The boot check {#healthcheck}

This is the call to make at startup. It exercises the key, the exchange, the session cache and an authenticated request, costs nothing, and fails in four distinguishable ways.

```javascript
import { IaGenify, IaGenifyError, Unauthorized } from "iagenify";

export async function checkIaGenify(api) {
  try {
    const user = await api.getUser();
    if (!user) return { ok: false, why: "no account behind this key" };
    return { ok: true, email: user.email, credits: user.credits };
  } catch (err) {
    if (err instanceof Unauthorized) return { ok: false, why: "key refused or revoked" };
    if (err instanceof IaGenifyError && err.code === "unreachable") {
      return { ok: false, why: `no answer at ${api.baseUrl}` };
    }
    throw err;
  }
}
```

## Reading the balance {#balance}

The number is true when it is read and nothing holds it. A run started a second later spends against the same balance, as does every other process on the account.

> **Do not gate a run on the balance**
>
> Checking first and spending after leaves a window where both succeed and the second one should not have. Give the run a budget instead: it is enforced where the spending happens, not where you looked.

```javascript
// Fragile: two processes both read 400 and both start a 300-credit run
const { credits } = await api.getUser();
if (credits < 300) throw new Error("not enough");
await agent.run({ message });

// Sound: the ceiling travels with the run, and a shortfall is refused at the debit
const agent = api.agents.create({ runtime: "local", machine: pc, budget: 300 });
await agent.run({ message });
```

A shortfall surfaces as a `NotEnoughCredits` exception carrying what was needed and what was there — catch that rather than predicting it. See [Credits](/sdk/concepts/credits).

## How often to call it {#cost}

Every call is a round trip. Reading it once at boot, and again after something that spends, is enough; polling it per request adds latency to your own endpoints and tells you nothing new between spends.

```javascript
let cached = null;
let readAt = 0;

export async function user(api, maxAgeMs = 60_000) {
  if (!cached || Date.now() - readAt > maxAgeMs) {
    cached = await api.getUser();
    readAt = Date.now();
  }
  return cached;
}
```

- [Credits](/sdk/concepts/credits): Pre-authorisation, settlement, and how a shortfall is refused.
- [Errors](/sdk/concepts/errors): The three classes, and the fields worth branching on.
- [updateProfile()](/sdk/ref/auth/update-profile): Changing the part of the account that is writable.

```python
{
    "id": "…",                  # the account id
    "email": "you@example.com",
    "credits": 10869,           # the balance, at the moment of the call
    "pack": "go",               # the plan this account is on
}
```

```python
from iagenify import IaGenifyError, Unauthorized

def check_iagenify(api):
    try:
        user = api.get_user()
        if not user:
            return {"ok": False, "why": "no account behind this key"}
        return {"ok": True, "email": user["email"], "credits": user["credits"]}
    except Unauthorized:
        return {"ok": False, "why": "key refused or revoked"}
    except IaGenifyError as error:
        if error.code == "unreachable":
            return {"ok": False, "why": f"no answer at {api.base_url}"}
        raise
```

```python
# Fragile: two processes both read 400 and both start a 300-credit run
if api.get_user()["credits"] < 300:
    raise RuntimeError("not enough")
agent.run(message=message)

# Sound: the ceiling travels with the run, and a shortfall is refused at the debit
agent = api.agents.create(runtime="local", machine=pc, model=model, budget=300)
agent.run(message=message)
```

```python
import time

_cached = None
_read_at = 0.0

def user(api, max_age=60):
    global _cached, _read_at
    if _cached is None or time.monotonic() - _read_at > max_age:
        _cached = api.get_user()
        _read_at = time.monotonic()
    return _cached
```
