
# Sign in as the user

Domain requests use `Authorization: Bearer <access_token>` and existing account/project permissions, never cookies, share links, database credentials, or project IDs as credentials.

## Obtain a session through browser approval

Use the public web origin. The CLI is optional.

Send a descriptive `User-Agent`, for example `User-Agent: HydroDesignAgent/1.0 (Tarvena API client)`, on authentication and API requests.

Send JSON for both requests:

```http
POST /api/auth/device/code
Content-Type: application/json

{"client_id":"tarvena-cli"}
```

The response supplies `device_code`, `user_code`, `verification_uri`, optional `verification_uri_complete`, `expires_in`, and polling `interval`. Copy `device_code` into the token request:

```http
POST /api/auth/device/token
Content-Type: application/json

{"client_id":"tarvena-cli","device_code":"DEVICE_CODE_FROM_RESPONSE","grant_type":"urn:ietf:params:oauth:grant-type:device_code"}
```

Success returns `access_token`, `token_type`, `expires_in`, and `scope`. No fine-grained scopes are requested.

Open `verification_uri_complete` when present; otherwise show the code at `verification_uri`. The user signs in and explicitly approves **Connect this CLI**. Opening the page is insufficient. Never automate approval. Native code/token requests send neither Cookie nor Origin. Store your own tokens privately. Never reuse the CLI session, read its stored credentials, or ask the user to paste a password, token, or session file.

Poll at the returned interval until expiry. Wait on `authorization_pending`; increase the interval on `slow_down`; stop on `access_denied` or `expired_token`.

## Handle expired sessions and access denial

On 401, sign in again and retry a read once. On 403, report denied access. Requests recheck access; local storage/displayed roles grant no additional authority.

Write requests also require the [current contract header](actions.md#write-contract). Authentication endpoints do not require it.

## Find a project

[Account and project endpoints](projects.md).

## End the session

Send the bearer session with a JSON body and content type. An empty body without `Content-Type` is refused by the authentication service.

```http
POST /api/auth/sign-out
Authorization: Bearer <access_token>
Content-Type: application/json

{}
```

A successful response is `{"success":true}`. This request revokes this session; the browser remains signed in. Delete your stored token. A later `GET /api/v1/me` with the revoked token returns 401 within a minute.

## Retry a limited request

On 429, honor `Retry-After`; otherwise use bounded exponential backoff with cancellation. Hydrology can omit the header.
