Agent chat endpoints
After the patient checks out, a board-certified physician reviews the submission and may reply in chat. These endpoints let an AI agent poll for messages and reply on the patient's behalf.
Authentication
Send one of the following in the Authorization header. Clerk session JWTs are not accepted on these routes.
- •
Bearer <encounter_token>— the 64-char hex token returned fromPOST /api/v1/agent/query. Scoped to a single encounter. Expires 72 hours after the encounter was created. - •
Bearer ak_<your-api-key>— the patient's personal API key. Must belong to the encounter's owner.
Poll for messages
/api/v1/agent/encounters/{encounter_id}/eventsRate-limited to 12 polls/minute per encounter. Returns 429 with a Retry-After header when exceeded.
Query parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
since | string | Optional | RFC3339 timestamp. Return only messages created after this time. |
limit | integer | Optional | Maximum number of messages to return. Returns the most recent N. |
Example
curl "$API/api/v1/agent/encounters/$ENCOUNTER_ID/events?since=2026-03-01T00:00:00Z" \
-H "Authorization: Bearer $ENCOUNTER_TOKEN"Response
{
"encounterId": "a1b2c3d4-...",
"state": "waitingForPatientReply",
"events": [
{
"eventId": "...",
"authorId": "...",
"author": { "id": "...", "email": "dr.smith@appendix.com", "clinician": { "firstName": "Anna", "lastName": "Smith", "credentials": "MD" } },
"type": "chat",
"message": "How has the cough progressed since last week?",
"createdAt": "2026-03-02T10:15:00Z",
"source": "web",
"isOwn": false
}
]
}source is "web" when a human typed the message, or "api" when it was posted via this API. isOwn is true when the message was authored by the patient — via either the web UI or this API.
Reply on the patient's behalf
/api/v1/agent/encounters/{encounter_id}/eventsPosts a chat message to the clinician. Returns 409 if the encounter hasn't been claimed yet (the patient must complete checkout first).
Request body
| Parameter | Type | Required | Description |
|---|---|---|---|
message | string | Required | The message text. |
Example
curl -X POST "$API/api/v1/agent/encounters/$ENCOUNTER_ID/events" \
-H "Authorization: Bearer $ENCOUNTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"message": "Thanks, I will pick up the prescription today."}'Mark messages read
/api/v1/agent/encounters/{encounter_id}/events/readRecord a read receipt on behalf of the patient. Once recorded, the patient will not receive an email reminder about that clinician message.
Request body
| Parameter | Type | Required | Description |
|---|---|---|---|
eventIds | string[] | Required | IDs of the events to mark read. |
Errors
| Code | Meaning |
|---|---|
401 | Missing or invalid token / API key. |
404 | Encounter not found, or API key does not belong to the encounter owner. |
409 | POSTing a chat message before the encounter has been claimed (the patient hasn't completed checkout yet). |
410 | Encounter older than 72 hours. The token window has closed. |
429 | Polling too fast. Retry after 5 seconds. |