# list()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/keys/list/
Language: zh

`api.keys.list() → Promise<{ ok, keys }>`

账户上的所有活动密钥, 都以其名称、 前缀和上次使用时的前缀, 没有任何秘密回来 。

Status: GET /api/keys · free · an envelope, not an array

## 返回值 {#returns}

```javascript
{
  ok: true,
  keys: [
    {
      id: "…",
      name: "ci",
      prefix: "ia_…",
      scopes: ["*"],
      lastUsedAt: "2026-10-07T09:12:44.102Z",
      expiresAt: null,
      isActive: true,
      createdAt: "2026-09-30T18:02:11.000Z"
    }
  ]
}
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `id` | string |  | 什麽 [撤销( )](/zh/sdk/ref/keys/revoke) 取了,这里和别处都出现 |
| `name` | string |  | 键创建时在您的账户中给定的名称。 并非唯一—— 有两个键可能共享一个 。 |
| `prefix` | string |  | 短识别符。 这就是您如何匹配存储的密钥到一行 。 |
| `scopes` | string[] |  | 标签记录在密钥上 。 `["*"]` 他们没有被赏赐, |
| `lastUsedAt` | string|null |  | 使用键时更新。 `null` 意味着它从来没有。 |
| `expiresAt` | string|null |  | 到期时, 当密钥随身携带 。 `null` 否则。 |
| `isActive` | boolean |  | 撤销此声明是虚假的。 |
| `createdAt` | string |  | 刚造的时候 最新的 |

日期为 ISO 字符字符字符, 如此 `Date.parse` 读取它们, 并有一个简单的字符串比较 。 被修改的密钥不返回: 查找范围被定位到账户的活跃密钥 。

## 哪个行是我手上的钥匙 {#identify}

永远不要比较键本身, 列表没有可以比较的秘密。 在部署时, 将前缀保存在键旁边, 匹配是准确和无害的 。

```javascript
const { keys } = await api.keys.list();

const mine = keys.find((k) => k.prefix === process.env.IAGENIFY_KEY_PREFIX);

if (!mine) {
  // The key this process holds is not an active key on the account any more.
  throw new Error("this deployment is running on a revoked or unknown key");
}
```

## 审计结果 让你 {#audit}

`lastUsedAt` 这里唯一能告诉你你不知道的事情的字段。有两个问题值得问一问。

### 哪些未使用

```javascript
const { keys } = await api.keys.list();
const month = Date.now() - 30 * 24 * 60 * 60 * 1000;

const idle = keys.filter((k) => !k.lastUsedAt || Date.parse(k.lastUsedAt) < month);

for (const k of idle) {
  console.warn(`${k.name} (${k.prefix}) · last used ${k.lastUsedAt ?? "never"}`);
}
```

关键无用是可以撤销的钥匙,而每个退休者都是少漏漏的。 *从未* 通常使用的是创建和从未部署的旋转。

### 使用什么时不应使用什么

```javascript
// A key belonging to a deployment you have retired should go quiet.
// If it does not, something still holds it.
const retired = keys.find((k) => k.name === "old-batch-runner");

if (retired?.lastUsedAt && Date.parse(retired.lastUsedAt) > Date.parse(retiredOn)) {
  // Something is still calling with it. Revoke it and find out what breaks.
}
```

> **上次使用时间是时间, 不是线索**
>
> 它告诉你一个密钥是用过的, 上次是用的时候。 它没有说明从何而来, 也没有说明什么。 它把它当作一条值得检查的绊线, 并说出足够清楚的钥匙名称, 答案是显而易见的 。

## 可读库存 {#inventory}

```javascript
const { keys } = await api.keys.list();

const rows = keys.map((k) => ({
  name: k.name,
  prefix: k.prefix,
  age: Math.round((Date.now() - Date.parse(k.createdAt)) / 86_400_000) + "d",
  used: k.lastUsedAt
    ? Math.round((Date.now() - Date.parse(k.lastUsedAt)) / 86_400_000) + "d ago"
    : "never",
}));

console.table(rows);
```

- [revoke()](/zh/sdk/ref/keys/revoke): 使用此列表中的id。
- [api.keys](/zh/sdk/ref/keys): 轮回 钥匙放哪

```python
{
    "ok": True,
    "keys": [
        {
            "id": "…",
            "name": "ci",
            "prefix": "ia_…",
            "scopes": ["*"],
            "lastUsedAt": "2026-10-07T09:12:44.102Z",
            "expiresAt": None,
            "isActive": True,
            "createdAt": "2026-09-30T18:02:11.000Z",
        }
    ],
}
```

```python
import os

keys = api.keys.list()["keys"]

mine = next((k for k in keys if k["prefix"] == os.environ["IAGENIFY_KEY_PREFIX"]), None)

if mine is None:
    # The key this process holds is not an active key on the account any more.
    raise RuntimeError("this deployment is running on a revoked or unknown key")
```

```python
from datetime import datetime, timedelta, timezone

keys = api.keys.list()["keys"]
month = datetime.now(timezone.utc) - timedelta(days=30)

def when(value): return datetime.fromisoformat(value.replace("Z", "+00:00"))

idle = [k for k in keys if not k.get("lastUsedAt") or when(k["lastUsedAt"]) < month]

for k in idle:
    print(f"{k['name']} ({k['prefix']}) · last used {k.get('lastUsedAt') or 'never'}")
```

```python
# A key belonging to a deployment you have retired should go quiet.
# If it does not, something still holds it.
retired = next((k for k in keys if k["name"] == "old-batch-runner"), None)

if retired and retired.get("lastUsedAt") and when(retired["lastUsedAt"]) > when(retired_on):
    pass  # Something is still calling with it. Revoke it and find out what breaks.
```

```python
from datetime import datetime, timezone

keys = api.keys.list()["keys"]
now = datetime.now(timezone.utc)

def days_since(value): return (now - datetime.fromisoformat(value.replace("Z", "+00:00"))).days

for k in keys:
    used = f"{days_since(k['lastUsedAt'])}d ago" if k.get("lastUsedAt") else "never"
    print(f"{k['name']:<20} {k['prefix']:<12} {days_since(k['createdAt'])}d  {used}")
```
