# App API

> The routes under /v1/ that the app's backend calls with its app key. The SDK wraps them, for example check is POST /v1/check.

From `server/openapi.json` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/reference/api/app.

The app's backend calls these routes with its app key. The SDK wraps them: `check` is `POST /v1/check`.

## /v1/agents

### POST /v1/agents

Create an agent for the subject, a person signed in with a human session.

Auth: App key, in the `Tensile-App-Key` header.

Body: `CreateAgentRequest` as `application/json`.

| Status | Meaning | Body |
|---|---|---|
| 201 | The agent | `Agent` |
| 400 | The body isn't valid, or the library refused the call | `Problem` |
| 401 | The app key isn't live (app_key_invalid), or the subject's credential isn't valid (unauthenticated) | `Problem` |
| 403 | The library's rules refuse the subject | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

### POST /v1/agents/{id}/delegate

Give the agent a token, delegated from the subject's session.

Auth: App key, in the `Tensile-App-Key` header.

| Parameter | In | Type | Description |
|---|---|---|---|
| `id` (required) | path | `string` |  |

Body: `DelegateRequest` as `application/json`.

| Status | Meaning | Body |
|---|---|---|
| 201 | The token, shown once | `Delegated` |
| 400 | The body isn't valid, or the library refused the call | `Problem` |
| 401 | The app key isn't live (app_key_invalid), or the subject's credential isn't valid (unauthenticated) | `Problem` |
| 403 | The library's rules refuse the subject | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## /v1/approvals

### POST /v1/approvals

Ask the subject agent's human to approve an action; a pending request for the same action returns the same approval.

Auth: App key, in the `Tensile-App-Key` header.

Body: `RequestApprovalRequest` as `application/json`.

| Status | Meaning | Body |
|---|---|---|
| 201 | The approval | `ApprovalCreated` |
| 400 | The body isn't valid, or the library refused the call | `Problem` |
| 401 | The app key isn't live (app_key_invalid), or the subject's credential isn't valid (unauthenticated) | `Problem` |
| 403 | The library's rules refuse the subject | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## /v1/check

### POST /v1/check

Authenticate a credential, apply a need, and redeem or request an approval.

With the header Prefer: wait=N (RFC 7240), N from 1 to 60, a retry that redeems a pending approval is held until the person approves or denies, the approval expires, or N seconds pass, and answers approval_pending when they pass. On the check that creates an approval, N caps the wait that the refusal's fix and wait_hint offer; without the header, the refusal offers no wait and tells the agent to retry after Retry-After. Each app key holds at most 256 waits on each replica; a retry over that answers at once. Give a waiting call a timeout of N seconds plus your usual timeout.

Auth: App key, in the `Tensile-App-Key` header.

| Parameter | In | Type | Description |
|---|---|---|---|
| `Prefer` | header | `string` | wait=N, from 1 to 60: how long to hold a retry for the person's decision, and the longest wait the refusal offers. Larger values count as 60. |

Body: `CheckRequest` as `application/json`.

| Status | Meaning | Body |
|---|---|---|
| 200 | The server decided; the end user's outcome is in the body | `CheckResponse` |
| 400 | The body isn't valid | `Problem` |
| 401 | The app key is missing or isn't live: code app_key_invalid | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## /v1/events

### GET /v1/events/stream

Server-sent events that tell the SDK when to clear its cache: ready once subscribed, then invalidate after each change to sessions, agents, or users. The stream ends when the server stops hearing every change; bypass the cache until it reconnects.

Auth: App key, in the `Tensile-App-Key` header.

| Status | Meaning | Body |
|---|---|---|
| 200 | The stream: event ready, then event invalidate after each change, and a comment every 15 seconds | `string` |
| 401 | The app key is missing or isn't live: code app_key_invalid | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't hear every change, or can't reach its database; bypass the cache and retry after Retry-After | `Problem` |

## /v1/meta

### GET /v1/meta

The server's version, the API versions it serves, and its preset.

Auth: App key, in the `Tensile-App-Key` header.

| Status | Meaning | Body |
|---|---|---|
| 200 | The server's metadata | `Meta` |
| 401 | The app key is missing or isn't live: code app_key_invalid | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## /v1/openapi.json

### GET /v1/openapi.json

This document.

Auth: None.

| Status | Meaning | Body |
|---|---|---|
| 200 | The OpenAPI document | `object` |

## /v1/sessions

### GET /v1/sessions/tree

The subject's live sessions, as a tree of delegations.

Auth: App key, in the `Tensile-App-Key` header.

| Parameter | In | Type | Description |
|---|---|---|---|
| `Tensile-Subject` (required) | header | `string` | base64url JSON in the shape of Credential |

| Status | Meaning | Body |
|---|---|---|
| 200 | The tree | `SessionTree` |
| 401 | The app key isn't live (app_key_invalid), or the subject's credential isn't valid (unauthenticated) | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## /v1/users

### GET /v1/users/{id}

What the server knows of a user: the address, whether it's verified, and live sessions and agents.

Auth: App key, in the `Tensile-App-Key` header.

| Parameter | In | Type | Description |
|---|---|---|---|
| `id` (required) | path | `string` |  |

| Status | Meaning | Body |
|---|---|---|
| 200 | The user | `User` |
| 401 | The app key isn't live (app_key_invalid), or the subject's credential isn't valid (unauthenticated) | `Problem` |
| 404 | No session, agent, or address has this user ID: code not_found | `Problem` |
| 429 | The app key sent too many requests; see Retry-After | `Problem` |
| 503 | The server can't reach its database | `Problem` |

## Schemas

### Action

| Field | Type | Description |
|---|---|---|
| `kind` (required) | `string` |  |
| `resource` | `string` |  |
| `params` | `map of string` |  |
| `summary` | `string` |  |

### Agent

| Field | Type | Description |
|---|---|---|
| `id` (required) | `string` |  |
| `owner` (required) | `string` |  |
| `name` (required) | `string` |  |
| `scopes` (required) | `array of string` |  |
| `created_at` (required) | `string (date-time)` |  |

### ApprovalCreated

| Field | Type | Description |
|---|---|---|
| `id` (required) | `string` |  |
| `state` (required) | `string` |  |
| `expires_at` (required) | `string (date-time)` |  |
| `url` | `string` |  |

### Challenge

What would turn a refused request into a success: the need that failed, and the factors that would meet it

| Field | Type | Description |
|---|---|---|
| `action` (required) | `string` |  |
| `assurance` (required) | `"none" or "recovering" or "single" or "multi"` |  |
| `within_seconds` | `integer` | At least 0. |
| `human` | `boolean` |  |
| `scopes` | `array of string` |  |
| `satisfy_with` (required) | `array of "passkey" or "totp" or "recovery" or "external" or "federated" or "password" or "bridged"` |  |
| `begin` | `string` |  |
| `approval` | `ChallengeApproval` |  |
| `purpose` | `Purpose` |  |

### ChallengeApproval

The approval an agent can wait for instead of stopping at a refusal

| Field | Type | Description |
|---|---|---|
| `id` (required) | `string` |  |
| `url` | `string` |  |
| `summary` (required) | `string` |  |
| `expires_in` (required) | `integer` |  |
| `wait_hint` | `string` |  |

### CheckRequest

| Field | Type | Description |
|---|---|---|
| `credential` (required) | `object` | Exactly one of cookie or authorization; dpop only with authorization |
| `credential.cookie` | `string` | The __Host-session cookie's value |
| `credential.authorization` | `string` |  |
| `credential.dpop` | `string` |  |
| `request` (required) | `object` |  |
| `request.method` | `string` |  |
| `request.url` (required) | `string` | The URL the client called on the app; its origin must be one of the server's origins, or the answer refuses with origin_not_allowed |
| `request.client_ip` | `string` |  |
| `request.user_agent` | `string` |  |
| `request.origin` | `string` | The browser's Origin header. Cookie requests get the library's cross-origin protection |
| `request.sec_fetch_site` | `string` | The browser's Sec-Fetch-Site header |
| `need` | `Need` |  |
| `approval` | `object` | With id, redeems that approval for action; without id, an agent that misses the need gets human_required with an approval for action in challenge.approval |
| `approval.id` | `string` |  |
| `approval.action` (required) | `Action` |  |
| `resource` | `string` | The canonical URI of the resource server that checks; tokens bound to another resource are refused |

### CheckResponse

| Field | Type | Description |
|---|---|---|
| `allowed` (required) | `boolean` |  |
| `principal` | `Principal` |  |
| `problem` | `Problem` |  |
| `set_cookies` (required) | `array of string` |  |
| `headers` (required) | `map of string` |  |

### CreateAgentRequest

| Field | Type | Description |
|---|---|---|
| `subject` (required) | `Credential` |  |
| `name` (required) | `string` |  |
| `scopes` | `array of string` |  |

### Credential

One of cookie or authorization; dpop only with authorization

| Field | Type | Description |
|---|---|---|
| `cookie` | `string` |  |
| `authorization` | `string` |  |
| `dpop` | `string` |  |

### DelegateRequest

| Field | Type | Description |
|---|---|---|
| `subject` (required) | `Credential` |  |
| `name` | `string` |  |
| `scopes` | `array of string` |  |
| `ttl_seconds` | `integer` | At least 0. |
| `key_thumbprint` | `string` |  |

### Delegated

The agent's token, shown once

| Field | Type | Description |
|---|---|---|
| `token` (required) | `string` |  |
| `session` (required) | `string` |  |
| `agent` (required) | `string` |  |
| `scopes` (required) | `array of string` |  |
| `expires_at` (required) | `string (date-time)` |  |

### Meta

| Field | Type | Description |
|---|---|---|
| `version` (required) | `string` |  |
| `api_versions` (required) | `array of string` |  |
| `preset` (required) | `string` |  |

### Need

| Field | Type | Description |
|---|---|---|
| `action` (required) | `string` |  |
| `human` | `boolean` |  |
| `assurance` | `"none" or "recovering" or "single" or "multi"` |  |
| `within_seconds` | `integer` | At least 0. |
| `scopes` | `array of string` |  |
| `approval_ttl_seconds` | `integer` | How long an approval for this need lives: 60 to 86400 seconds, or 0 for 5 minutes. From 0 to 86400. |

### Principal

| Field | Type | Description |
|---|---|---|
| `user` (required) | `string` |  |
| `session` (required) | `string` |  |
| `kind` (required) | `"human" or "agent" or "pat" or "oauth"` |  |
| `agent` (required) | `string or null` |  |
| `scopes` (required) | `array of string` |  |
| `assurance` (required) | `string` |  |
| `methods` (required) | `array of string` |  |
| `authenticated_at` (required) | `string (date-time)` |  |
| `expires_at` (required) | `string (date-time)` |  |

### Problem

An RFC 9457 problem, as authhttp writes it and the browser client reads it

| Field | Type | Description |
|---|---|---|
| `type` (required) | `string` |  |
| `title` (required) | `string` |  |
| `status` (required) | `integer` |  |
| `code` (required) | `string` |  |
| `detail` | `string` | What happened in this occurrence, for people |
| `fix` | `string` |  |
| `retry_after` | `integer` | Seconds to wait before retrying, for throttled At least 0. |
| `challenge` | `Challenge` |  |

### Purpose

The purpose an elevation must be for

| Field | Type | Description |
|---|---|---|
| `name` (required) | `string` |  |
| `resource` | `string` |  |

### RequestApprovalRequest

| Field | Type | Description |
|---|---|---|
| `subject` (required) | `Credential` |  |
| `action` (required) | `Action` |  |
| `need` (required) | `Need` |  |

### Session

| Field | Type | Description |
|---|---|---|
| `id` (required) | `string` |  |
| `kind` (required) | `"human" or "agent" or "pat" or "oauth"` |  |
| `agent` (required) | `string or null` |  |
| `parent` (required) | `string or null` |  |
| `name` | `string` |  |
| `device` | `string` |  |
| `scopes` (required) | `array of string` |  |
| `created_at` (required) | `string (date-time)` |  |
| `last_seen_at` (required) | `string (date-time)` |  |
| `expires_at` (required) | `string (date-time)` |  |
| `children` (required) | `array of Session` |  |

### SessionTree

| Field | Type | Description |
|---|---|---|
| `sessions` (required) | `array of Session` |  |

### User

| Field | Type | Description |
|---|---|---|
| `id` (required) | `string` |  |
| `email` (required) | `string or null` |  |
| `email_verified` (required) | `boolean` |  |
| `live_sessions` (required) | `integer` |  |
| `agents` (required) | `integer` |  |
| `last_seen_at` (required) | `string (date-time) or null` |  |
