> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.rasen.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.rasen.ai/_mcp/server.

# Start an agent session on your own call leg

POST https://api.rasen.ai/v1/agent-sessions
Content-Type: application/json

Admits a call that runs the agent's live published version and returns a signed media WebSocket for a call leg that your switch, carrier, dialer or application already owns. Rasen places, answers and routes no telephone call and needs no phone number. Stream the leg's audio to websocket_url before expires_at; the agent's audio comes back on the same socket. Optional expected_version_id only refuses an unexpected live version. Requires current admission capacity: at_capacity and quota_exceeded say to retry with the same key after Retry-After. Same key and body replay the same call, websocket_url and expires_at. The call's metadata is returned on call reads.

Reference: https://docs.rasen.ai/api-reference/calls/create-agent-session

## Authentication

- `Authorization` header (bearer token, required) — Create and manage workspace-owned keys through the existing app session routes, outside this public API. Creator and manager identities are audit actors only. Key validity follows workspace policy, expiry and explicit revocation, independently of creator membership. Do not include a workspace-selection header.

## Request

### Headers

- `Idempotency-Key` (string, required) — Stable key per logical intent, 1–255 printable characters. Persist it before sending. Retry the same key/body after an uncertain result for 24 hours. A changed body conflicts. Beyond retention, reconcile the resource before retrying; this is not permanent deduplication. Current authorization is rechecked on replay. Evaluation order: authenticate and check current authorization; look up intent; replay its stored receipt before checking the current revision; only a new intent evaluates If-Match and executes. Fingerprint includes method, path, body and original If-Match. A changed request returns idempotency_key_reused. The server stores only a keyed (HMAC) fingerprint, and no error or response ever returns a fingerprint or hash of a request. In-flight intent returns 409 idempotency_in_progress with Retry-After; retry the same intent after reconciliation. Receipts describe that attempt, not current resource state. A replay returns the original status code and body, with the response header Idempotent-Replayed: true.

### Body (application/json)

This endpoint expects an AgentSessionCreate.

- `agent_id` (string, required) — Opaque agent identifier. Do not parse its contents.
- `direction` (enum, optional) — A label for call reads and filters. It does not place or answer a call. Omitted, it is the live version's call_direction setting, or outbound when that is not set.
  - Allowed values: `inbound`, `outbound`
- `expected_version_id` (string, optional) — Optional precondition: refuse if this is not the live version at admission. Does not select an old version.
- `media` (AgentSessionMedia, optional) — Audio framing for both directions of the media WebSocket. Omitted fields take their defaults. mulaw with 16000 Hz is refused.
- `dynamic_variables` (map from string to string, optional) — Flat object of string values. At most 64 keys; each key matches ^\[a-z]\[a-z0-9\_]\$. Each value is at most 4096 characters and contains no control characters except tab and newline. The whole object is at most 16 KiB as JSON. Nested objects and non-string values are refused, never converted. Overrides literal defaults; undeclared names are allowed. Credentials are forbidden.
- `metadata` (map from string to string, optional) — Bounded customer correlation map: at most 16 keys; each key matches ^\[a-z]\[a-z0-9\_]\$ (the same form the metadata\_key filter accepts); each value at most 512 characters; at most 4 KiB in total. No credential storage. Metadata is not a uniqueness or permanent deduplication guarantee.

## Response

### 201

Start an agent session on your own call leg.

- `call_id` (string, required) — Opaque call identifier. Do not parse its contents.
- `agent_id` (string, required) — Opaque agent identifier. Do not parse its contents.
- `published_version_id` (string, required) — Opaque pub identifier. Do not parse its contents.
- `state` (string, required) — The call state at admission. The call becomes active when the first audio frame arrives on the socket.
- `direction` (enum, required)
  - Allowed values: `inbound`, `outbound`
- `media` (AgentSessionMedia, required) — Audio framing for both directions of the media WebSocket. Omitted fields take their defaults. mulaw with 16000 Hz is refused.
- `websocket_url` (string, required) — The signed media WebSocket URL of this call. It is a credential: connect to it exactly as returned, before expires_at, and do not log it. One socket per call.
- `expires_at` (datetime, required) — The socket must connect and send audio before this time (120 seconds after admission). After it, the reserved capacity is released and the call ends with end_reason never_connected. A replay never extends it.

## Errors

### 400 Bad Request Error

Invalid input or cursor. Correct the request.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 401 Unauthorized Error

Invalid, expired or revoked API key. Creator departure or membership changes do not invalidate a workspace key.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 403 Forbidden Error

Operation not allowed by current credential or workspace access.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 404 Not Found Error

Resource absent or not visible in this workspace.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 409 Conflict Error

Conflict. Not retryable: read the current state, fix the request and send a new request. The one exception is idempotency_in_progress, which carries Retry-After: wait, then resend the identical request with the same Idempotency-Key. The operation lists its codes in x-error-codes; branch on error.code and, for conflict, error.details.reason.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 413 Content Too Large Error

Body or upload exceeds its allowed limit.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 429 Too Many Requests Error

Rate limited. Respect Retry-After and retry the same intent where permitted.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 500 Internal Server Error

Internal error. The effect may have happened. Retry the same request with the same Idempotency-Key; read the resource before creating a new intent. For an operation that takes an Idempotency-Key, a client retry with the same key is safe: the server replays the stored outcome if the first attempt took effect; if the first attempt had no effect, the retry performs the operation once. Never retry with a new key.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

### 503 Service Unavailable Error

Service unavailable. Respect Retry-After where present. For an operation that takes an Idempotency-Key, a client retry with the same key is safe: the server replays the stored outcome if the first attempt took effect; if the first attempt had no effect, the retry performs the operation once. Never retry with a new key.

- `error` (ErrorResponseError, required) — Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

## Types

### AgentSessionMedia

Audio framing for both directions of the media WebSocket. Omitted fields take their defaults. mulaw with 16000 Hz is refused.

- `transport` (enum, optional, default: binary) — binary: raw audio in binary WebSocket frames. json: base64 media events in text frames.
  - Allowed values: `binary`, `json`
- `encoding` (enum, optional, default: pcm16) — pcm16: signed 16-bit little-endian mono. mulaw: G.711 mu-law, valid at 8000 Hz only.
  - Allowed values: `pcm16`, `mulaw`
- `sample_rate` (enum, optional, default: 8000) — Samples per second, the same in both directions.
  - Allowed values: `8000`, `16000`

### ErrorResponseError

Only code and message are required. Rasen sends details, request_id and retry on every error, but clients must parse an error that lacks them, so a typed error is never lost to a parsing failure.

- `code` (string, required) — Stable code. Branch on code (and details.reason for conflict), never on message. Each code has one HTTP status; see components.x-error-catalog.
- `message` (string, required) — Safe description; no provider payload or submitted secrets.
- `details` (map from string to any, optional) — Safe code-specific details. The public fields for each code are listed in components.x-error-catalog. Details never contain a request fingerprint, a hash of a request, secret material or another tenant's data. forbidden may include required_capabilities (a list).
- `request_id` (string, optional) — Same value as the X-Request-Id header. Quote it to support.
- `retry` (string, optional) — Retry class of this code: no (fix the request or state first), yes (retry the same request and the same Idempotency-Key), retry_after (wait for Retry-After, then retry the same request). Open response vocabulary. Preserve unknown values; never assume they mean success.

## Examples

**Request**

```json
{
  "agent_id": "agent_01j00000000000000000000000",
  "direction": "inbound",
  "media": {
    "transport": "binary",
    "encoding": "pcm16",
    "sample_rate": 8000
  },
  "metadata": {
    "external_call_id": "pbx-4411"
  }
}
```

**Response**

```json
{
  "call_id": "call_01j00000000000000000000000",
  "agent_id": "agent_01j00000000000000000000000",
  "published_version_id": "pub_01j00000000000000000000000",
  "state": "admitted",
  "direction": "inbound",
  "media": {
    "transport": "binary",
    "encoding": "pcm16",
    "sample_rate": 8000
  },
  "websocket_url": "wss://media.example.invalid/calls/call_01j00000000000000000000000/ws/<token>",
  "expires_at": "2026-10-14T10:02:00Z"
}
```

**SDK Code**

```python calls_createAgentSession_example
import requests

url = "https://api.rasen.ai/v1/agent-sessions"

payload = {
    "agent_id": "agent_01j00000000000000000000000",
    "direction": "inbound",
    "media": {
        "transport": "binary",
        "encoding": "pcm16",
        "sample_rate": 8000
    },
    "metadata": { "external_call_id": "pbx-4411" }
}
headers = {
    "Idempotency-Key": "intent-appointment-001",
    "Authorization": "Bearer <api_key>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript calls_createAgentSession_example
const url = 'https://api.rasen.ai/v1/agent-sessions';
const options = {
  method: 'POST',
  headers: {
    'Idempotency-Key': 'intent-appointment-001',
    Authorization: 'Bearer <api_key>',
    'Content-Type': 'application/json'
  },
  body: '{"agent_id":"agent_01j00000000000000000000000","direction":"inbound","media":{"transport":"binary","encoding":"pcm16","sample_rate":8000},"metadata":{"external_call_id":"pbx-4411"}}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```ruby calls_createAgentSession_example
require 'uri'
require 'net/http'

url = URI("https://api.rasen.ai/v1/agent-sessions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Idempotency-Key"] = 'intent-appointment-001'
request["Authorization"] = 'Bearer <api_key>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"agent_id\": \"agent_01j00000000000000000000000\",\n  \"direction\": \"inbound\",\n  \"media\": {\n    \"transport\": \"binary\",\n    \"encoding\": \"pcm16\",\n    \"sample_rate\": 8000\n  },\n  \"metadata\": {\n    \"external_call_id\": \"pbx-4411\"\n  }\n}"

response = http.request(request)
puts response.read_body
```

```java calls_createAgentSession_example
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.rasen.ai/v1/agent-sessions")
  .header("Idempotency-Key", "intent-appointment-001")
  .header("Authorization", "Bearer <api_key>")
  .header("Content-Type", "application/json")
  .body("{\n  \"agent_id\": \"agent_01j00000000000000000000000\",\n  \"direction\": \"inbound\",\n  \"media\": {\n    \"transport\": \"binary\",\n    \"encoding\": \"pcm16\",\n    \"sample_rate\": 8000\n  },\n  \"metadata\": {\n    \"external_call_id\": \"pbx-4411\"\n  }\n}")
  .asString();
```

```php calls_createAgentSession_example
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.rasen.ai/v1/agent-sessions', [
  'body' => '{
  "agent_id": "agent_01j00000000000000000000000",
  "direction": "inbound",
  "media": {
    "transport": "binary",
    "encoding": "pcm16",
    "sample_rate": 8000
  },
  "metadata": {
    "external_call_id": "pbx-4411"
  }
}',
  'headers' => [
    'Authorization' => 'Bearer <api_key>',
    'Content-Type' => 'application/json',
    'Idempotency-Key' => 'intent-appointment-001',
  ],
]);

echo $response->getBody();
```

```csharp calls_createAgentSession_example
using RestSharp;

var client = new RestClient("https://api.rasen.ai/v1/agent-sessions");
var request = new RestRequest(Method.POST);
request.AddHeader("Idempotency-Key", "intent-appointment-001");
request.AddHeader("Authorization", "Bearer <api_key>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"agent_id\": \"agent_01j00000000000000000000000\",\n  \"direction\": \"inbound\",\n  \"media\": {\n    \"transport\": \"binary\",\n    \"encoding\": \"pcm16\",\n    \"sample_rate\": 8000\n  },\n  \"metadata\": {\n    \"external_call_id\": \"pbx-4411\"\n  }\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift calls_createAgentSession_example
import Foundation

let headers = [
  "Idempotency-Key": "intent-appointment-001",
  "Authorization": "Bearer <api_key>",
  "Content-Type": "application/json"
]
let parameters = [
  "agent_id": "agent_01j00000000000000000000000",
  "direction": "inbound",
  "media": [
    "transport": "binary",
    "encoding": "pcm16",
    "sample_rate": 8000
  ],
  "metadata": ["external_call_id": "pbx-4411"]
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.rasen.ai/v1/agent-sessions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```