# mv.agentSessions

> The agentSessions methods of @morevoice/sdk: Sign your agents in to the agent desk from your own app (embedded sessions).

Sign your agents in to the agent desk from your own app (embedded sessions). These methods are on `mv.agentSessions`, where `mv` is your client (see [the Node.js SDK](https://docs.morevoice.ai/sdk/node/#connect)). Each one returns the response object and throws when the API answers with an error.

## `create()`

**Create an agent session.** Signs one of your agents or supervisors in to the agent desk, from your server: no login page of ours. Returns the session's bearer token (once) for the desk's REST calls, and a first one-use socket ticket. The session reaches only the agent desk (other routes answer 403 `embedded_route_forbidden`), with the member's own role; bind it to your website with `origin`. It lasts `ttl_s` (default eight hours, at most 24) and ends early when you revoke it or deactivate the member. Live keys only.

```ts
mv.agentSessions.create(body?: AgentSessionsCreateData["body"], options?: RequestOptions): Promise<AgentSessionsCreateResponse>
```

`POST /agent_sessions` · [API reference](https://docs.morevoice.ai/api/operations/agent_sessions_create/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `body.external_id` | `string` | no | Or: your own id of a managed user (POST /v1/users). |
| `body.origin` | `string` | no | The only website the session works from (`https://app.example.com`): every request and socket must carry this Origin. Strongly recommended for sessions used in a browser. |
| `body.ttl_s` | `integer` | no | Lifetime in seconds, 300–86400 (default 28800, eight hours). It never slides. |
| `body.user_id` | `string` | no | The member to sign in (an agent or supervisor). Give this or `external_id`. |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options.headers["Idempotency-Key"]` | `string` | no | A unique key (for example a UUID) that makes this request safe to retry: for 24 hours, a retry with the same key and parameters returns the first response instead of acting twice. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`AgentSessionWithToken`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The session's ID. |
| `object` | `"agent_session"` | Always `agent_session`. |
| `livemode` | `boolean` | `true` in live mode, `false` in test mode. |
| `user_id` | `string` | The member it signs in. |
| `external_id` | `string \| null` | Your own id for that member (managed users), else null. |
| `org_id` | `string` | The organisation it is bound to (it never switches). |
| `origin` | `string \| null` | The only website it works from, or null for any. |
| `created` | `string` | An ISO-8601 timestamp in UTC. |
| `expires_at` | `string` | When it ends (a fixed lifetime: using it does not extend it). |
| `last_used` | `string \| null` | When it was last used (to the nearest few minutes). |
| `token` | `string` | The session's bearer token (`Authorization: Bearer mvas_…`) for the agent desk's calls. Returned once: give it to the agent's browser, never log it. |
| `ws_ticket` | `string` | A one-time ticket for the first socket (`<ws_url>?ticket=…`), valid 60 seconds. Get more with POST /v1/agent_sessions/{id}/ticket. |
| `ws_ticket_expires_at` | `string` | An ISO-8601 timestamp in UTC. |
| `ws_url` | `string` | The agent socket's URL (offers, presence): `wss://…/ws/agent`. |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const agentSessionWithToken = await mv.agentSessions.create({
	external_id: "agent-1042",
	origin: "https://app.example.com",
	ttl_s: 28800,
});
console.log(agentSessionWithToken);
```

## `delete()`

**Revoke an agent session.** Ends the session now: its token and unused tickets stop working, and its open sockets close.

```ts
mv.agentSessions.delete(id: AgentSessionsDeleteData["path"]["id"], options?: RequestOptions): Promise<AgentSessionsDeleteResponse>
```

`DELETE /agent_sessions/{id}` · [API reference](https://docs.morevoice.ai/api/operations/agent_sessions_delete/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | A agent session ID (`ags_…`). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options.headers["Idempotency-Key"]` | `string` | no | A unique key (for example a UUID) that makes this request safe to retry: for 24 hours, a retry with the same key and parameters returns the first response instead of acting twice. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`DeletedAgentSession`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | A agent session ID (prefix `ags_`). |
| `object` | `"agent_session"` | Always `agent_session`. |
| `deleted` | `true` | — |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const deletedAgentSession = await mv.agentSessions.delete("ags_7Hk2Lm9Qp");
console.log(deletedAgentSession);
```

## `list()`

**List agent sessions.** Your organisation's live agent sessions (not expired, not revoked), newest first. Tokens are never shown again.

```ts
mv.agentSessions.list(query?: NonNullable<AgentSessionsListData["query"]>, options?: RequestOptions): PagedList<AgentSessionsListResponse["data"][number], NonNullable<AgentSessionsListData["query"]>>
```

`GET /agent_sessions` · [API reference](https://docs.morevoice.ai/api/operations/agent_sessions_list/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `query.limit` | `integer` | no | How many objects to return, 1–100 (default 20). |
| `query.starting_after` | `string` | no | A cursor (`next_cursor`) or object ID: return the objects after it (older). |
| `query.ending_before` | `string` | no | A cursor or object ID: return the objects before it (newer). |
| `query.user_id` | `string` | no | Only this member's sessions. |
| `query.external_id` | `string` | no | Only the sessions of the managed user with this id of yours. |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

A [`PagedList`](https://docs.morevoice.ai/sdk/typescript/pagination/): `await` it for the first page, `for await` it for every item.

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

for await (const agentSession of mv.agentSessions.list()) {
	console.log(agentSession);
}
```

## `retrieve()`

**Retrieve an agent session.** Returns the `AgentSession` object. Answers `404` with the code `resource_missing` when nothing has this ID in this organisation and mode.

```ts
mv.agentSessions.retrieve(id: AgentSessionsRetrieveData["path"]["id"], options?: RequestOptions): Promise<AgentSessionsRetrieveResponse>
```

`GET /agent_sessions/{id}` · [API reference](https://docs.morevoice.ai/api/operations/agent_sessions_retrieve/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | A agent session ID (`ags_…`). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`AgentSession`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The session's ID. |
| `object` | `"agent_session"` | Always `agent_session`. |
| `livemode` | `boolean` | `true` in live mode, `false` in test mode. |
| `user_id` | `string` | The member it signs in. |
| `external_id` | `string \| null` | Your own id for that member (managed users), else null. |
| `org_id` | `string` | The organisation it is bound to (it never switches). |
| `origin` | `string \| null` | The only website it works from, or null for any. |
| `created` | `string` | An ISO-8601 timestamp in UTC. |
| `expires_at` | `string` | When it ends (a fixed lifetime: using it does not extend it). |
| `last_used` | `string \| null` | When it was last used (to the nearest few minutes). |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const agentSession = await mv.agentSessions.retrieve("ags_7Hk2Lm9Qp");
console.log(agentSession);
```

## `ticket()`

**Create a socket ticket.** A fresh one-use ticket (60 seconds) for one socket of this session: `/ws/agent`, `/ws/call`, `/ws/meeting` or `/ws/join`, as `?ticket=…`. Your server calls this each time the agent's browser (re)connects.

```ts
mv.agentSessions.ticket(id: AgentSessionsTicketData["path"]["id"], options?: RequestOptions): Promise<AgentSessionsTicketResponse>
```

`POST /agent_sessions/{id}/ticket` · [API reference](https://docs.morevoice.ai/api/operations/agent_sessions_ticket/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | A agent session ID (`ags_…`). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options.headers["Idempotency-Key"]` | `string` | no | A unique key (for example a UUID) that makes this request safe to retry: for 24 hours, a retry with the same key and parameters returns the first response instead of acting twice. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`AgentSessionTicket`:

| Field | Type | Description |
| --- | --- | --- |
| `object` | `"ws_ticket"` | Always `ws_ticket`. |
| `ticket` | `string` | Append to a socket URL as `?ticket=…`: works once, within 60 seconds, for this session's member and organisation. |
| `expires_at` | `string` | An ISO-8601 timestamp in UTC. |
| `ws_url` | `string` | The agent socket's URL (`wss://…/ws/agent`); the same ticket also opens `/ws/call`, `/ws/meeting` or `/ws/join` on that host. |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const agentSessionTicket = await mv.agentSessions.ticket("ags_7Hk2Lm9Qp");
console.log(agentSessionTicket);
```
