Skip to Content
Clinic APIECM (Enhanced Care Management)Communications (SMS & Calls)

ECM Communications API

Clinic endpoints for sending SMS messages and initiating phone calls to ECM patients. Communications are automatically tracked as ECM activities and events for PRTF reporting.

Overview

The ECM Communications API provides endpoints for care coordinators to communicate with patients via SMS and phone calls. All communications are integrated with Twilio for delivery and are automatically tracked as ECM activities for compliance reporting.

Communication Flow

When a communication is initiated:

  1. Request Received: API receives the communication request
  2. Twilio Integration: Message/call is sent via Twilio
  3. Activity Created: An ECM activity is automatically created
  4. Event Generated: An ECM event is generated for PRTF reporting
  5. Response Returned: Success/failure response is returned

Send SMS

Send an SMS message to an ECM patient. The message is delivered via Twilio and automatically tracked as an ECM activity.

Endpoint: POST /v1/clinic/ecm/communications/patients/{patient_id}/sms
Authentication: Required

Path Parameters

ParameterTypeRequiredDescription
patient_idintegerYesPatient ID

Request Body Parameters

ParameterTypeRequiredDescription
smsobjectYesSMS data object
sms.phone_numberstringYesRecipient phone number (E.164 format recommended)
sms.bodystringYesMessage content

Example Request

POST /v1/clinic/ecm/communications/patients/123/sms HTTP/1.1 Content-Type: application/json X-User-Token: your_token_here { "sms": { "phone_number": "+18051234567", "body": "Hello! This is a reminder about your upcoming appointment. Please reply to confirm." } }

Success Response

{ "message": "SMS sent successfully", "sms": { "id": 456, "twilio_sid": "SM1234567890abcdef", "to": "+18051234567", "from": "+18059876543", "body": "Hello! This is a reminder about your upcoming appointment. Please reply to confirm.", "status": "queued", "direction": "outbound", "created_at": "2024-11-27T10:00:00.000Z" }, "activity": { "id": 789, "activity_type": "sms", "status": "created", "patient_id": 123, "performed_by_id": 101, "assigned_to_id": 101 } }

Error Response - Invalid Phone Number

{ "errors": ["Phone number is required"] }

Error Response - Twilio Error

{ "errors": ["Failed to send SMS: The 'To' number is not a valid phone number"] }

Initiate Call

Initiate an outbound phone call to an ECM patient. The call is placed via Twilio and automatically tracked as an ECM activity.

Endpoint: POST /v1/clinic/ecm/communications/patients/{patient_id}/call
Authentication: Required

Path Parameters

ParameterTypeRequiredDescription
patient_idintegerYesPatient ID

Request Body Parameters

ParameterTypeRequiredDescription
callobjectYesCall data object
call.phone_numberstringYesPhone number to call (E.164 format recommended)

Example Request

POST /v1/clinic/ecm/communications/patients/123/call HTTP/1.1 Content-Type: application/json X-User-Token: your_token_here { "call": { "phone_number": "+18051234567" } }

Success Response

{ "message": "Call initiated successfully", "call": { "id": 457, "twilio_sid": "CA1234567890abcdef", "to": "+18051234567", "from": "+18059876543", "status": "queued", "direction": "outbound", "created_at": "2024-11-27T10:00:00.000Z" }, "activity": { "id": 790, "activity_type": "call", "status": "created", "patient_id": 123, "performed_by_id": 101, "assigned_to_id": 101 } }

Error Response - Invalid Phone Number

{ "errors": ["Phone number is required"] }

Error Response - Twilio Error

{ "errors": ["Failed to initiate call: The 'To' number is not a valid phone number"] }

Phone Number Format

Phone numbers should be provided in E.164 format for best results:

FormatExampleRecommended
E.164+18051234567Yes
National8051234567Supported
With dashes805-123-4567Supported
With parentheses(805) 123-4567Supported

Note: The system will attempt to normalize phone numbers, but E.164 format is recommended for reliability.

Inbound Communications

Inbound SMS messages and calls are automatically tracked via Twilio webhooks. When a patient responds to an SMS or calls back:

  1. Webhook Received: Twilio sends a webhook to the API
  2. Patient Matched: The system matches the phone number to a patient
  3. Activity Created: An inbound activity is automatically created
  4. Event Generated: An event is generated for PRTF reporting

Inbound SMS Webhook

Endpoint: POST /v1/clinic/webhooks/twilio/sms
Authentication: Twilio signature validation

This endpoint is called by Twilio when an inbound SMS is received. The webhook creates an inbound SMS activity for the matched patient.

Inbound Call Webhook

Endpoint: POST /v1/clinic/webhooks/twilio/voice
Authentication: Twilio signature validation

This endpoint is called by Twilio when an inbound call is received. The webhook creates an inbound call activity for the matched patient.

SMS Status Tracking

SMS messages go through several status stages:

StatusDescription
queuedMessage is queued for delivery
sendingMessage is being sent
sentMessage has been sent to carrier
deliveredMessage has been delivered to recipient
undeliveredMessage could not be delivered
failedMessage failed to send

SMS Status Webhook

Endpoint: POST /v1/clinic/webhooks/twilio/sms_status
Authentication: Twilio signature validation

This endpoint receives status updates from Twilio as SMS messages progress through delivery stages.

Call Status Tracking

Phone calls go through several status stages:

StatusDescription
queuedCall is queued
ringingCall is ringing
in-progressCall is in progress
completedCall completed successfully
busyRecipient was busy
no-answerNo answer
failedCall failed
canceledCall was canceled

Call Status Webhook

Endpoint: POST /v1/clinic/webhooks/twilio/voice_status
Authentication: Twilio signature validation

This endpoint receives status updates from Twilio as calls progress through their lifecycle.

Activity and Event Creation

When a communication is sent or received, the system automatically creates:

Activity Record

FieldValue
activity_typesms or call
statuscreated
patient_idPatient ID
performed_by_idCurrent user ID (outbound) or system (inbound)
assigned_to_idCurrent user ID
text_message_idTwilio SMS record ID (for SMS)
voice_call_idTwilio call record ID (for calls)

Event Record

FieldValue
event_typesms or call
occurred_atTimestamp of communication
patient_idPatient ID
activity_idAssociated activity ID

JavaScript Example

// Send SMS const sendSms = async (patientId, phoneNumber, body) => { const response = await fetch(`/v1/clinic/ecm/communications/patients/${patientId}/sms`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-User-Token': 'YOUR_TOKEN' }, body: JSON.stringify({ sms: { phone_number: phoneNumber, body: body } }) }); return response.json(); }; // Initiate call const initiateCall = async (patientId, phoneNumber) => { const response = await fetch(`/v1/clinic/ecm/communications/patients/${patientId}/call`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-User-Token': 'YOUR_TOKEN' }, body: JSON.stringify({ call: { phone_number: phoneNumber } }) }); return response.json(); }; // Usage await sendSms(123, '+18051234567', 'Hello! This is a reminder about your appointment.'); await initiateCall(123, '+18051234567');

TypeScript/React Example

import { useMutation } from '@tanstack/react-query'; interface SmsRequest { patientId: number; phoneNumber: string; body: string; } interface CallRequest { patientId: number; phoneNumber: string; } // Send SMS mutation const useSendSms = () => { return useMutation({ mutationFn: async ({ patientId, phoneNumber, body }: SmsRequest) => { const response = await fetch( `/v1/clinic/ecm/communications/patients/${patientId}/sms`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-User-Token': token }, body: JSON.stringify({ sms: { phone_number: phoneNumber, body } }) } ); return response.json(); } }); }; // Initiate call mutation const useInitiateCall = () => { return useMutation({ mutationFn: async ({ patientId, phoneNumber }: CallRequest) => { const response = await fetch( `/v1/clinic/ecm/communications/patients/${patientId}/call`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-User-Token': token }, body: JSON.stringify({ call: { phone_number: phoneNumber } }) } ); return response.json(); } }); };

cURL Examples

Send SMS

curl -X POST "https://your-api-domain.com/v1/clinic/ecm/communications/patients/123/sms" \ -H "Content-Type: application/json" \ -H "X-User-Token: YOUR_TOKEN" \ -d '{ "sms": { "phone_number": "+18051234567", "body": "Hello! This is a reminder about your appointment." } }'

Initiate Call

curl -X POST "https://your-api-domain.com/v1/clinic/ecm/communications/patients/123/call" \ -H "Content-Type: application/json" \ -H "X-User-Token: YOUR_TOKEN" \ -d '{ "call": { "phone_number": "+18051234567" } }'

Error Handling

Common Errors

ErrorCauseSolution
Phone number is requiredMissing phone numberProvide a valid phone number
Patient not foundInvalid patient IDVerify patient ID exists
Invalid phone numberMalformed phone numberUse E.164 format
Twilio errorTwilio service issueCheck Twilio status, retry

Twilio Error Codes

Common Twilio error codes you may encounter:

CodeDescription
21211Invalid ‘To’ phone number
21214’To’ phone number cannot be reached
21610Message blocked (opt-out)
21408Permission to send SMS not enabled

Best Practices

  1. Phone Number Format: Use E.164 format (+1XXXXXXXXXX) for reliability
  2. Message Length: Keep SMS messages under 160 characters when possible
  3. Opt-Out Compliance: Respect patient opt-out requests
  4. Error Handling: Implement retry logic for transient failures
  5. Activity Tracking: Verify activities are created for all communications
  6. PRTF Reporting: Ensure all communications are properly tracked for compliance