Chat

Send messages to NPCs and receive AI-generated responses within a session.

Overview

The Chat API handles real-time conversations with published personas. Initialize a session, send messages, and receive NPC responses along with state transition data and scoring metadata.

Note
Web chat endpoints require a signed-in user. Players must create an account or sign in before starting a conversation. Each player gets their own independent session.

Initialize Session

POST /api/v1/chat/{persona_id}/init-session

Starts a new chat session with the specified persona. Returns the NPC's opening message and a session ID for subsequent requests.

Query Parameters

ParameterTypeRequiredDescription
machine_idstringNoTarget a specific state machine. Defaults to the persona's primary machine.

Request Body

FieldTypeRequiredDescription
user_session_idstringYesUnique identifier for the player's session
player_namestringYesThe player's display name, used in NPC responses
player_genderstringYesOne of female, male, or non-binary

Response

FieldTypeDescription
messagestringThe NPC's opening message
session_idstringServer-assigned session identifier
user_session_idstringEchoed player session identifier
persona_namestringName of the persona
persona_descriptionstring | nullDescription of the persona
player_namestringEchoed player name
player_genderstringEchoed player gender
machine_idstringThe active state machine ID

Send Message

POST /api/v1/chat/{persona_id}/message

Sends a player message to the NPC and returns the response along with state machine metadata including current state, score, and outcome information.

Query Parameters

ParameterTypeRequiredDescription
machine_idstringNoTarget a specific state machine

Request Body

FieldTypeRequiredDescription
messagestringYesThe player's message text
user_session_idstringYesSession identifier from init

Response

FieldTypeDescription
messagestringThe NPC's response text
current_statestringThe current state machine state key
scorenumberCurrent cumulative score for the session
outcomestring | nullOutcome label if a terminal state was reached
decisionstring | nullTransition decision made by the AI
follow_upbooleanWhether the NPC expects a follow-up message
redirect_countnumber | nullNumber of redirects used in the current state
session_idstringThe active session identifier
scoring_modestringThe scoring mode for this persona
machine_completedbooleanWhether the state machine has reached a terminal state
unlocked_machinesarrayList of newly unlocked machine IDs, if any

Get Chat History

GET /api/v1/chat/{persona_id}/history

Retrieves the full message history for a session.

Query Parameters

ParameterTypeRequiredDescription
user_session_idstringYesThe session to retrieve history for
machine_idstringNoFilter history to a specific state machine

Reset Session

POST /api/v1/chat/{persona_id}/reset

Resets a chat session, clearing all history and returning the state machine to its initial state.

Query Parameters

ParameterTypeRequiredDescription
user_session_idstringYesThe session to reset
machine_idstringNoReset only a specific state machine

Example: Full Conversation Flow

Here's a complete example of initializing a session and sending a message.

1. Initialize the session
curl -X POST https://api.foilengine.io/api/v1/chat/abc123/init-session \
  -H "Content-Type: application/json" \
  -d '{
    "user_session_id": "player-session-001",
    "player_name": "Alex",
    "player_gender": "non-binary"
  }'

Response:

{
  "message": "Well, well... another traveler. What brings you to the Hollow Market, Alex?",
  "session_id": "sess_7f8a9b2c",
  "player_name": "Alex",
  "player_gender": "non-binary",
  "machine_id": "machine_main"
}
2. Send a message
curl -X POST https://api.foilengine.io/api/v1/chat/abc123/message \
  -H "Content-Type: application/json" \
  -d '{
    "message": "I heard you have something rare for sale.",
    "user_session_id": "player-session-001"
  }'

Response:

{
  "message": "Rare? Everything here is rare, friend. But the real question is — can you afford it? Not everything costs gold.",
  "current_state": "negotiation",
  "score": 1,
  "outcome": null,
  "decision": "pass",
  "follow_up": true,
  "session_id": "sess_7f8a9b2c",
  "scoring_mode": "scoring",
  "machine_completed": false,
  "unlocked_machines": []
}