# The device flow, for CLIs and AI agents

> How a CLI or an AI agent signs in for you without anyone pasting a secret, keeps a session that can last forever, and how you revoke it.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/machines/device-flow-and-agents/

For agents: Device authorization grant (RFC 8628) for agents: `npx @sypnia/data login --project <ref>` generates an Ed25519 key, POST /auth/v1/oauth/device_authorization, shows verification_uri + user_code; the user approves while signed in; the CLI polls /oauth/token with grant_type=urn:ietf:params:oauth:grant-type:device_code and a DPoP proof; receives DPoP-bound access + refresh tokens in an agent session whose policy is the client's (can be never). Stored in ~/.config/sypnia/credentials.json and agent-key.json, mode 600. Revoke in the console (Agents and devices), with `npx @sypnia/data logout`, or by deleting the key. Never paste keys into a chat.

An agent that manages your backend needs a credential that works for a long time, that nobody had to copy into a chat, and that you can take back. The **device flow** (RFC 8628) gives exactly that, with DPoP on top.

## Signing in

### 1. The agent starts

```bash
npx @sypnia/data login --project <ref>
```

It generates a key pair on your machine and asks the project for a device code.

### 2. You approve

```text
Open https://data.app.sypnia.com/device and enter the code  WXYZ-1234
```

You open it **signed in**. The page shows the device name, the agent, and the scopes it asks for. You approve, or deny. Codes expire after 10 minutes. Approving privileged scopes (`sql:*`, `auth:admin`, `project:admin`) needs an account an admin entitled to them; otherwise the approval fails with `403 scope_not_allowed`. With no `--scope`, `npx @sypnia/data login` asks for `data:read data:write offline_access`.

### 3. The agent receives its session

The CLI was asking every 5 seconds; now it receives an access token and a refresh token, both **bound to its key** with DPoP, in a session of its own.

## Where it is kept

```text
~/.config/sypnia/credentials.json   the project and the refresh token     (mode 600)
~/.config/sypnia/agent-key.json     the private key                       (mode 600)
```

The refresh token is useless without the private key, and the private key never leaves your machine.

## How long it lasts

The session follows the policy of the client that started it. The `sypnia` CLI client's default is **never expires and never times out**: the agent keeps working until you revoke it. A project can change that policy, for example to 90 days of inactivity.

## Revoking

- In the console → Account → **Agents and devices**: every agent session with its name, its scopes, where it was last used and when. **Revoke** ends it at once: its next refresh fails.
- `npx @sypnia/data logout` on the machine ends the session and deletes both files.
- Deleting the key file makes the session unusable even if the token leaked.

> **In plain words**
>
> The agent gets its own session, so revoking it never signs you out, and your own sign-out never breaks the agent.
