# client.agent_sessions

> The agent_sessions methods of the morevoice Python 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 `client.agent_sessions`, where `client` is a `MoreVoice` client (see [the Python SDK](https://docs.morevoice.ai/sdk/python/#connect)). On `AsyncMoreVoice` the same methods are awaited. Each one returns the response object and raises an exception 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.

```python
# client.agent_sessions
def create(self, body: _m.AgentSessionCreateParamsInput | Mapping[str, Any] | Unset = UNSET, *, more_voice_version: str | Unset = UNSET, idempotency_key: str | Unset = UNSET) -> _m.AgentSessionWithToken
```

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

### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `external_id` | `string` | no | Or: your own id of a managed user (POST /v1/users). |
| `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. |
| `ttl_s` | `integer` | no | Lifetime in seconds, 300–86400 (default 28800, eight hours). It never slides. |
| `user_id` | `string` | no | The member to sign in (an agent or supervisor). Give this or `external_id`. |

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

```python
from morevoice import MoreVoice

client = MoreVoice()  # MOREVOICE_API_KEY from the environment

agent_session_with_token = client.agent_sessions.create({
    "external_id": "agent-1042",
    "origin": "https://app.example.com",
    "ttl_s": 28800,
})
print(agent_session_with_token)
```

## `delete()`

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

```python
# client.agent_sessions
def delete(self, id: str, *, more_voice_version: str | Unset = UNSET, idempotency_key: str | Unset = UNSET) -> _m.DeletedAgentSession
```

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

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `str` | yes | A agent session ID (`ags_…`). |

### Returns

`DeletedAgentSession`:

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

### Example

```python
from morevoice import MoreVoice

client = MoreVoice()  # MOREVOICE_API_KEY from the environment

deleted_agent_session = client.agent_sessions.delete("ags_7Hk2Lm9Qp")
print(deleted_agent_session)
```

## `list()`

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

```python
# client.agent_sessions
def list(self, *, limit: int | Unset = 20, starting_after: str | Unset = UNSET, ending_before: str | Unset = UNSET, user_id: str | Unset = UNSET, external_id: str | Unset = UNSET, more_voice_version: str | Unset = UNSET) -> AsyncPage[_m.AgentSession]
```

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

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `limit` | `integer` | no | How many objects to return, 1–100 (default 20). |
| `starting_after` | `string` | no | A cursor (`next_cursor`) or object ID: return the objects after it (older). |
| `ending_before` | `string` | no | A cursor or object ID: return the objects before it (newer). |
| `user_id` | `string` | no | Only this member's sessions. |
| `external_id` | `string` | no | Only the sessions of the managed user with this id of yours. |

### Returns

A page of results (`AgentSessionList`): `data`, `has_more` and `next_cursor`.

### Example

```python
from morevoice import MoreVoice

client = MoreVoice()  # MOREVOICE_API_KEY from the environment

for agent_session in client.agent_sessions.list():
    print(agent_session)
```

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

```python
# client.agent_sessions
def retrieve(self, id: str, *, more_voice_version: str | Unset = UNSET) -> _m.AgentSession
```

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

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `str` | yes | A agent session ID (`ags_…`). |

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

```python
from morevoice import MoreVoice

client = MoreVoice()  # MOREVOICE_API_KEY from the environment

agent_session = client.agent_sessions.retrieve("ags_7Hk2Lm9Qp")
print(agent_session)
```

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

```python
# client.agent_sessions
def ticket(self, id: str, *, more_voice_version: str | Unset = UNSET, idempotency_key: str | Unset = UNSET) -> _m.AgentSessionTicket
```

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

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `str` | yes | A agent session ID (`ags_…`). |

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

```python
from morevoice import MoreVoice

client = MoreVoice()  # MOREVOICE_API_KEY from the environment

agent_session_ticket = client.agent_sessions.ticket("ags_7Hk2Lm9Qp")
print(agent_session_ticket)
```
