# device()

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

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

Registra un telefono o un tablet come un storage. Chiamat dal tuo server, perché il telefono non detiene mai la chiave.

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

Un computer registra se: il SDK corre là, scambia la chiave, e la macchina devenia un storage. Un telefono non può — il SDK non è nel e non deve ser, perché la chiave sarebbe. Quindi la pagina envia un identificator al servèrver, e il servèrvèr l'aclama.

## Parâmetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `deviceId` | string | obrigatório | Un identificator al azar la pagina mantiene. Manca → il SDK lança. |
| `name` | string |  | Il nome display. Derivè del agente utente quando ausente. |
| `userAgent` | string |  | Agent d'usuari del telefono, usat per quel nome. |

## Il medès divizimentId è il medès stoccatè {#idempotent}

L'invocare di nuovo con un identificatore già visto restitue il stoccaggio existente in lugar de fare un altro. Così la pagina puèt inchinar a ogni carica senza cumulare registrazioni — e due chiamate simultanee ancora producen uno.

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

> **Generare l'identificatore, mai derivar**
>
> Un valore costruito a partir del agente utente, l'ecran o un cont è lo stesso per molte persone — e chi lo envia ottiene que storage. Random, mantenut in browser, è lo che lo rende un dispositivo.

## Retorno {#returns}

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

## I suoi files non sono a conta {#files}

> **Un telefono mantiene i propri files, in suo browser**
>
> Salvar a lui a través del server è negat con storage_is_local. Lectura e scrivi-le avviene in la pagina, che è onde sono — il server releve la richiesta e la pagina risponde.

E questo rende un telefono un luogo sensat per un agente per lavorare: nulla è caricat per lettura. [i pods de limas](/pt/sdk/agent/bridges/files).

## appName separa due apps su un telefono {#appname}

Un storage è identificat dal conto, il dispositivo e il `appName` il client è stato costruito con. Due apps in un telefono sono due storages — che è generalmente quello che si desidera, dal momento que i loro files non hanno nulla a che fare l'un cun l'altro.

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

## Erros {#errors}

| Código | Status | Quando |
| --- | --- | --- |
| — (erro lançado localmente) | — | Nessun dispositivo ha superat. |
| non autorizzato | 401 | Non sessione o chiave valida — la chiamata è del tuo server, non del telefono. |

- [Telefone](/pt/sdk/ref/providers/phone): Que un archive telefonic puè e non puèt fare.
- [Il ponte di files](/pt/sdk/agent/bridges/files): Permettendo un agente lavora su files non puèt acceder direttamente.

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