# Upsert a message

Operation ID: `patient.commsUpsertMessage`

Create or update a CareConnect chat message, keyed by client-supplied `id`. Idempotent; the server always confirms `sent` status on success. Requires a Bearer JWT (any authenticated pool type).

## Public method

`upsertMessage`

Signature: `patient.upsertMessage(request)`

Return type: `Promise<MessageRecord>`

## Authentication

Classification: **AUTHENTICATED**

Schemes: `bearerAuth`

## Prerequisites

None documented.

## HTTP

`POST /communications/messages`

## Path parameters

None.

## Query parameters

None.

## Body parameters

| Name | Type | Required | Format | Allowed values | Default | Nullable | Description |
|---|---|---:|---|---|---|---:|---|
| `body` | [`UpsertMessageRequest`](../models/UpsertMessageRequest.md) | Yes |  |  |  | No |  |

Request model: [`UpsertMessageRequest`](../models/UpsertMessageRequest.md)

## Request example

```json
{
  "body": "SDK live test",
  "conversationId": "sdk-conversation-20260811-013559",
  "createdAt": "2026-08-10T23:37:16.608014Z",
  "id": "sdk-message-20260811-013559",
  "kind": "text",
  "senderId": "sdk-contact-20260811-013559",
  "status": "sent"
}
```

## Success responses

| Status | Shape | Content type | Description |
|---|---|---|---|
| `201` | [`ChatMessageRecord`](../models/ChatMessageRecord.md) | application/json | Message upserted |

## Success response examples

### 201

```json
{
  "body": "SDK live test",
  "conversationId": "sdk-conversation-20260811-013559",
  "createdAt": "2026-08-10T23:37:16.608014Z",
  "id": "sdk-message-20260811-013559",
  "kind": "text",
  "senderId": "sdk-contact-20260811-013559",
  "status": "sent"
}
```

## Common errors

| Status | Shape | Content type | Description |
|---|---|---|---|
| `400` | [`patient.ErrorResponse`](../models/patient.ErrorResponse.md) | application/json | The request body failed schema validation |
| `401` | [`patient.ErrorResponse`](../models/patient.ErrorResponse.md) | application/json | Authorization required — missing or invalid Bearer JWT |
| `403` | [`patient.ErrorResponse`](../models/patient.ErrorResponse.md) | application/json | The caller's token carries no tenant context |

## Error examples

### 400 — The request body failed validation

```json
{
  "details": [
    {
      "loc": [
        "conversationId"
      ],
      "msg": "Field required",
      "type": "missing"
    }
  ],
  "error": "Invalid request body"
}
```

### 401 — Missing or invalid access token

```json
{
  "error": "Authorization required"
}
```

### 403 — Tenant context unavailable

```json
{
  "error": "Tenant context unavailable"
}
```

## NodeJS / TypeScript implementation

```ts
import { HCSDK } from "@healthcloudai/hc-sdk";
import type { UpsertMessageRequest } from "@healthcloudai/hc-sdk";
```

```ts
const request = {
  "body": "SDK live test",
  "conversationId": "sdk-conversation-20260811-013559",
  "createdAt": "2026-08-10T23:37:16.608014Z",
  "id": "sdk-message-20260811-013559",
  "kind": "text",
  "senderId": "sdk-contact-20260811-013559",
  "status": "sent"
};

const result = await patient.upsertMessage(request);
```

## cURL

```bash
curl -X POST \
  -H 'Authorization: Bearer <ACCESS_TOKEN>' \
  -H 'Content-Type: application/json' \
  -d '{"body":"SDK live test","conversationId":"sdk-conversation-20260811-013559","createdAt":"2026-08-10T23:37:16.608014Z","id":"sdk-message-20260811-013559","kind":"text","senderId":"sdk-contact-20260811-013559","status":"sent"}' \
  'https://dev-api-patient.health.cloud/communications/messages'
```

## Notes

None.

## Prepared Test Console scenario

No canonical scenario is currently associated.

## Real response

No approved real integration response is currently published. Unapproved candidates are never rendered as examples.
