# device()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/storage/device/
Language: zh

`api.storage.device({ deviceId, name, userAgent }) → Promise<ConnectedStorage>`

将电话或平板电脑注册为存储器。 从服务器调用, 因为电话从不持有密钥 。

Status: POST /api/storage/device · free · same deviceId, same storage

计算机注册: SDK 手机不能—— 手机 SDK 页面上没有显示, 也决不能显示, 因为密钥是。 因此页面会发送您服务器的识别符, 您的服务器会调用此功能 。

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `deviceId` | string | 必填 | 页面保存的随机标识符。 缺少 {} SDK 扔掷 |
| `name` | string |  | 显示名称。 缺少时从用户代理器中提取 。 |
| `userAgent` | string |  | 电话的用户代理, 用来这个名字。 |

## 同一设备ID是同一存储器 {#idempotent}

以已经看到的身份识别符再次调用它, 返回现有的存储, 而不是做另一个。 因此页面可以调用它, 而不填充注册记录, 并且同时调用两个电话仍然产生一个。

```javascript
// In the page: an identifier that survives reloads
let deviceId = localStorage.getItem("device");
if (!deviceId) {
  deviceId = `dev_${crypto.randomUUID()}`;
  localStorage.setItem("device", deviceId);
}

// On your server, where the key is
export async function registerPhone(deviceId, userAgent) {
  const phone = await api.storage.device({ deviceId, userAgent });
  return phone.id;
}
```

> **生成标识符, 永远不产生**
>
> 由用户代理、屏幕或账户建立的值对许多人来说是一样的,而无论谁发送,都得到存储。随机,保存在浏览器中,它就是一个设备。

## 返回值 {#returns}

```javascript
phone.id;
phone.type;       // "phone"
phone.provider;   // "browser"
phone.deviceId;   // the identifier you passed
```

## 账户上没有它的档案 {#files}

> **电话在自己的浏览器中保存自己的文件**
>
> 拒绝通过服务器保存到服务器 storage_is_local。读写它们发生在页面上,即他们所在的页面上——您的服务器转发请求和页面答案。

这也是为什么电话是代理人工作的合理场所:没有东西上传阅读。 [文件桥](/zh/sdk/agent/bridges/files).

## 应用程序Name 将两个应用程序分隔在一个电话上 {#appname}

存储由账户、装置和 `appName` 与客户一起建的。一个电话上的两个应用程序是两个存储器——这是通常你想要的,因为他们的档案与彼此无关。

```javascript
const api = new IaGenify({ apiKey, appName: "field-notes" });
await api.storage.device({ deviceId });   // this app's storage on this phone
```

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| —（在本地抛出） | — | 没有设备IDD通过 。 |
| 未经授权 | 401 | 没有有效的会话或密钥—— 电话是您的服务器的, 而不是电话的。 |

- [手机](/zh/sdk/ref/providers/phone): 手机存储器能做什么,也不能做什么。
- [文件桥](/zh/sdk/agent/bridges/files): 让代理商对它无法直接触及的文件工作 。

```python
# In the page (JavaScript): an identifier that survives reloads
#   localStorage.getItem("device") || `dev_${crypto.randomUUID()}`

# On your Python server, where the key is
def register_phone(device_id, user_agent):
    phone = api.storage.device(device_id=device_id, user_agent=user_agent)
    return phone.id
```

```python
phone.id
phone.type       # "phone"
phone.provider   # "browser"
```

```python
api = IaGenify(api_key=api_key, app_name="field-notes")
api.storage.device(device_id=device_id)   # this app's storage on this phone
```
