> For a complete page index, fetch https://docs.synthflow.ai/llms.txt. For full documentation content, fetch https://docs.synthflow.ai/llms-full.txt.

# Create a chat

POST https://api.synthflow.ai/v2/chat/{chat_id}
Content-Type: application/json

Create a new chat session with an agent.

Reference: https://docs.synthflow.ai/api-reference/platform-api/chat/create-chat

## Servers

- `https://api.synthflow.ai/v2` (Global, default)
- `https://api.us.synthflow.ai/v2` (United States)
- `https://api.eu.synthflow.ai/v2` (European Union)

## Request

### Path parameters

- `chat_id` (string, required)

### Body (application/json)

This endpoint expects an object.

- `model_id` (string, required) — The ID of the agent to chat with.
- `metadata` (map from string to any, optional) — Optional metadata to attach to the chat.

## Response

### 200

200

- `status` (string, optional) — Whether the request was successful.
- `response` (ChatResponse, optional) — Chat session details.

## Errors

### 400 Bad Request Error

400

## Types

### ChatResponse

Chat session details.

- `chat_id` (string, required) — Unique identifier for the chat session.
- `chat_status` (enum, required) — Current status of the chat session.
  - Allowed values: `ongoing`, `completed`, `cancelled`
- `conversation_history` (list of ChatMessage, required) — List of messages in the conversation.
- `start_timestamp` (datetime, required) — ISO 8601 timestamp of when the chat session started.
- `transcript` (string, required) — Full transcript of the conversation.
- `initial_message` (ChatResponseInitialMessage, optional, nullable) — The initial message sent by the agent when the chat was created.
- `fsm_log` (map from string to FSMAgentLog, optional, nullable) — Finite State Machine (FSM) execution log for the chat session.

### ChatMessage

A single message in a chat conversation.

- `agent_id` (string, required) — ID of the agent (or 'user' for user messages).
- `current_state` (string, required) — The current state of the agent when this message was sent.
- `message` (string, required) — The message content.
- `timestamp` (datetime, required) — ISO 8601 timestamp of when the message was sent.
- `turn_number` (integer, required) — The turn number in the conversation.

### ChatResponseInitialMessage

The initial message sent by the agent when the chat was created.

- `agent_message` (string, optional) — The message content from the agent.
- `current_state` (string, optional) — The current state of the agent.
- `timestamp` (datetime, optional) — ISO 8601 timestamp of when the message was sent.
- `turn_number` (integer, optional) — The turn number in the conversation.

### FSMAgentLog

FSM execution log for a specific agent in the chat session.

- `current_state` (string, required) — The current state of the FSM.
- `current_turn` (string, required) — Identifier for the current turn in the conversation.
- `event_summary` (map from string to integer, required) — Summary of event counts by type.
- `events` (list of FSMEvent, required) — List of FSM events that occurred during execution.
- `execution_stats` (FsmAgentLogExecutionStats, required) — Statistics about FSM execution.
- `fsm_metadata` (FsmAgentLogFsmMetadata, required) — Metadata about the FSM.
- `variables` (map from string to FsmAgentLogVariables, required) — Collected variables (slots) from the conversation flow. Contains variable names as keys with their values and collection status.
- `state_path` (list of string, optional) — Path of states visited during FSM execution.

### FSMEvent

A single FSM event.

- `event_type` (enum, required) — Type of the event.
  - Allowed values: `fsm_started`, `state_executed`, `utterance_sent`, `state_transition`, `turn_changed`, `user_input`, `command_executed`, `command_received`, `command_failed`, `action_executed`, `action_failed`, `transition`, `error`
- `state_id` (string, required) — ID of the state where the event occurred.
- `timestamp` (integer, required) — Unix timestamp in milliseconds when the event occurred.
- `details` (map from string to any, required) — Additional details specific to the event type.

### FsmAgentLogExecutionStats

Statistics about FSM execution.

- `commands_executed` (integer, optional) — Number of commands executed.
- `state_transitions` (integer, optional) — Number of state transitions.
- `total_events` (integer, optional) — Total number of events executed.

### FsmAgentLogFsmMetadata

Metadata about the FSM.

- `name` (string, optional) — Name of the FSM.

### FsmAgentLogVariables

- `value` (FsmAgentLogVariablesValue, optional) — The collected value of the variable.
- `collected` (boolean, optional) — Whether this variable was successfully collected during the conversation.

### FsmAgentLogVariablesValue

The collected value of the variable.

- string
- double
- boolean
- map from string to any

## Examples

**Request**

```json
{
  "model_id": "893195c1-c640-438b-9761-2e5357b01cb8"
}
```

**Response**

```json
{
  "status": "ok",
  "response": {
    "chat_id": "a830288c-88df-4611-8db2-6db87b4fbb41",
    "chat_status": "ongoing",
    "conversation_history": [],
    "start_timestamp": {},
    "transcript": "Agent: Hi there!"
  }
}
```

**SDK Code**

```python
import requests

url = "https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41"

payload = { "model_id": "893195c1-c640-438b-9761-2e5357b01cb8" }
headers = {"Content-Type": "application/json"}

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

print(response.json())
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41"

	payload := strings.NewReader("{\n  \"model_id\": \"893195c1-c640-438b-9761-2e5357b01cb8\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

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

url = URI("https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41")

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

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"model_id\": \"893195c1-c640-438b-9761-2e5357b01cb8\"\n}"

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

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

HttpResponse<String> response = Unirest.post("https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41")
  .header("Content-Type", "application/json")
  .body("{\n  \"model_id\": \"893195c1-c640-438b-9761-2e5357b01cb8\"\n}")
  .asString();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"model_id\": \"893195c1-c640-438b-9761-2e5357b01cb8\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = ["model_id": "893195c1-c640-438b-9761-2e5357b01cb8"] as [String : Any]

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

let request = NSMutableURLRequest(url: NSURL(string: "https://api.synthflow.ai/v2/chat/a830288c-88df-4611-8db2-6db87b4fbb41")! 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()
```