# getUser()

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

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

钥匙属于谁 平衡是什么 最短的电话能证明整个链条有效

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

## 返回值 {#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 |  | 账户。 稳定; 值得记录在任何你退帐的旁边。 |
| `email` | string |  | 开户地址。 |
| `credits` | integer |  | 平衡平衡,一种解读,而不是保留——见 [下 数](#balance). |
| `pack` | string |  | 计划。 由决定配额,而不是决定模式价格。 |

服务器持有的比这多; 方法将其缩小到四个字段, 程序实际上会分支。 当您需要大记录时, [更新Profile ()](/zh/sdk/ref/auth/update-profile) 返回该数值。

## 包包 {#pack}

| 值 | 含义 |
| --- | --- |
| 空空 | 计划开始一个账户。 |
| 上左 | 付款计划 |
| 启动器 | 一份付费计划, 有更多的一切。 |
| 企业 | 由安排而不是自用安排决定。 |
| freemium | An older name that follows `free`. |

阅读计划决定什么可以提供,从不计算价格:模型成本什么 [(目录)](/zh/sdk/ref/iagenify/catalogue) 计划规定一个服务设施有多少房间——储存室、邮箱、主机——这些限制可以从执行这些限制的服务中读取。

## 当它为无效时 {#null}

`null` 表示呼叫成功且没有记账。 它不是错误的密钥看起来像什么: 错误的密钥在会话交换之前就失败了 。

| 症状 | 发生的事情 |
| --- | --- |
| 未经许可扔 | 键被拒绝, 或者被撤销 。 |
| 无法到达的抛出 | 基地没有回复,先检查地址 |
| 超时 | 回答太慢了,或者根本不是 |
| 无效返回 | 请求奏效了,但背后没有说明。 |

## 靴子检查 {#healthcheck}

这是启动时要发出的呼吁。 它会运行密钥、 交换、 会话缓存和经认证的请求, 费用不高, 并且以四种不同的方式失败 。

```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;
  }
}
```

## 读取平衡 {#balance}

当数字被读取时,数字是真实的,没有任何东西可以维持。 运行开始后,与账户上所有其他流程一样,在同样的余额下支出第二笔支出。

> **你不要使任何差事在他们的间道之间,**
>
> 首先检查, 离开窗口后开支, 窗口成功和第二个成功都不应该有。 代之以给运行预算: 支出发生时强制执行, 而不是您所看的 。

```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 });
```

亏空表面作为 `NotEnoughCredits` 例外,包括需要什么和存在什么——抓住而不是预测。 [信贷信贷](/zh/sdk/concepts/credits).

## 多久称呼它一次 {#cost}

每个电话都是往返的。 阅读一次在启动时, 然后再阅读一次, 然后再阅读一次支出,就足够了; 投票一次请求会增加你自己的终点的丁字裤, 并且告诉你在支出之间没有新情况。

```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;
}
```

- [额度](/zh/sdk/concepts/credits): 事先授权、和解以及资金短缺如何被拒绝。
- [错误](/zh/sdk/concepts/errors): 三个班级 和值得分流的田地
- [updateProfile()](/zh/sdk/ref/auth/update-profile): 更改可写入的账户部分。

```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
```
