# Create an agent session

`POST https://api.morevoice.ai/v1/agent_sessions`

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.

Authenticate with a secret API key: `Authorization: Bearer $MOREVOICE_API_KEY`.

Operation ID: `agent_sessions_create`.

## Parameters

### Headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `MoreVoice-Version` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `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. |

## Request body

Optional, `application/json`.

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

Example:

```json
{
  "external_id": "agent-1042",
  "origin": "https://app.example.com",
  "ttl_s": 28800
}
```

## Responses

### 201 Created

The session, with its token. Returns `AgentSessionWithToken`.

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The session's ID. |
| `object` | `"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:

```json
{
  "created": "2026-11-03T08:00:00.000Z",
  "expires_at": "2026-11-03T16:00:00.000Z",
  "external_id": "agent-1042",
  "id": "ags_H97Oak9qchHXc1hB8b93SK",
  "last_used": "2026-11-03T09:14:22.000Z",
  "livemode": true,
  "object": "agent_session",
  "org_id": "org_87tzsRJO1egtexauAF8PwX",
  "origin": "https://app.example.com",
  "token": "<agent session token: mvas_…, keep it on the agent's device>",
  "user_id": "usr_2Lm8Pq4Rt6Vx0Zb3Nd5Fh7",
  "ws_ticket": "<one-time socket ticket>",
  "ws_ticket_expires_at": "2026-11-03T08:01:00.000Z",
  "ws_url": "wss://voice.example.com/ws/agent"
}
```

### 400 Bad Request

The request is invalid: a parameter is missing, malformed or unknown, or the version header is unknown. Returns `ErrorEnvelope`.

### 401 Unauthorized

No valid API key was sent. Returns `ErrorEnvelope`.

### 403 Forbidden

The key may not do this (a missing scope, a plan limit, or a compliance block). Returns `ErrorEnvelope`.

### 404 Not Found

No object with this ID (in the path or the body) exists in this organisation and mode. Returns `ErrorEnvelope`.

### 409 Conflict

The request conflicts with the object's state, or the Idempotency-Key was reused with other parameters. Returns `ErrorEnvelope`.

### 413 Payload Too Large

The request body is larger than the endpoint accepts, or has more parts or files than it takes. Returns `ErrorEnvelope`.

### 415 Unsupported Media Type

The request body's Content-Type is not one the endpoint takes. Returns `ErrorEnvelope`.

### 429 Too Many Requests

Too many requests, or no call capacity right now. Retry after the Retry-After delay. Returns `ErrorEnvelope`.

### 500 Internal Server Error

Something went wrong on MoreVoice's side. Retry with the same Idempotency-Key. Returns `ErrorEnvelope`.

## Code samples

**TypeScript**

```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);
```

**Python**

```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)
```

**cURL**

```sh
curl -X POST https://api.morevoice.ai/v1/agent_sessions \
  -H "Authorization: Bearer $MOREVOICE_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
  "external_id": "agent-1042",
  "origin": "https://app.example.com",
  "ttl_s": 28800
}'
```
