3CX integration
The 3CX integration connects a 3CX SMS workflow to HTTPSMS in two directions. An authenticated 3CX request can start an HTTPSMS message send. HTTPSMS can also translate message events into the JSON shape expected by a 3CX webhook, while keeping the connection state tied to the HTTPSMS user. This page is part of Integrations and automation; compare Webhooks for the general event delivery model and SMS message lifecycle for the source message states.
API boundary
Inbound message endpoint
The inbound API gives 3CX a narrow entry point into the normal HTTPSMS send flow. POST /integration/3cx/messages accepts a JSON body with from, to, and text. The route uses the Bearer API key to identify the HTTPSMS user, then checks that the user can send messages before it passes the request to the shared message service. api/…/integration_3cx_handler.go• L39-57 api/…/container.go• L1345-1349
The request normalizes the two phone numbers before the send begins. It maps the 3CX sender to the message owner and the recipient to the message contact. It also marks the request source as the 3CX integration so that the created message follows the same persistence, phone delivery, retry, and event rules as other HTTPSMS sends. api/…/integration_3cx_message_request.go• L13-39 api/…/message_service.go• L446-504
The common send path also keeps the activity in the HTTPSMS conversation model. The message thread service groups activity by user, owner phone, and contact. It creates a missing thread or updates the latest message state in the existing thread, so 3CX traffic remains part of the user’s normal message history. api/…/message_thread_service.go• L87-140 api/…/message_thread_service.go• L209-245
On success, the route returns the created message ID inside a JSON response. The handler currently returns HTTP 200. The generated API annotation describes a 204 response, so clients should use the implemented response contract rather than assume that a successful request has no body. api/…/integration_3cx_handler.go• L45-56 api/…/integration_3cx_handler.go• L74-88
References: api/…/integration_3cx_handler.go• L39-88 api/…/integration_3cx_message_request.go• L13-39 api/…/request.go• L24-35 api/…/message_thread_service.go• L87-140 api/…/message_thread_service.go• L209-245
Event pathway
Event fan-out
HTTPSMS message work produces CloudEvents, which are events with a standard envelope and typed message data. The event dispatcher validates an event, places asynchronous work on the event queue, and calls every listener registered for that event type. This shared path lets 3CX use the same message events as webhooks, Discord, and notification services without changing the message producer. See Webhooks and Discord and email notifications for the other event delivery paths. api/…/event_dispatcher_service.go• L50-60 api/…/event_dispatcher_service.go• L99-142
The 3CX listener is created during application setup and subscribes its routes to the dispatcher. In the current registration map, only user.account.deleted is active. The four message handlers remain defined but their subscriptions are commented out. Therefore, the outbound message mapping is available in the service but message events do not currently reach the 3CX service through the active listener registration. api/…/integration_3cx_listener.go• L21-39 api/…/container.go• L1481-1492
The diagram shows both directions and marks the current outbound message boundary.
flowchart LR
A[3CX message request] --> B[Authenticated API route]
B --> C[HTTPSMS message service]
C --> D[Message record and phone delivery]
C --> T[Conversation thread state]
D --> E[CloudEvent]
E --> F[Event dispatcher]
F -. message subscriptions inactive .-> G[3CX listener]
G --> H[3CX webhook]
I[Account deletion event] --> F
F --> J[3CX integration cleanup]
J --> K[Connection record removal]
References: api/…/event_dispatcher_service.go• L22-29 api/…/event_dispatcher_service.go• L63-105 api/…/event_dispatcher_service.go• L116-152 api/…/integration_3cx_listener.go• L33-39
3CX payload mapping
The integration service converts selected HTTPSMS message events into a 3CX event envelope. Each outbound body uses data.record_type: "event" and payload.type: "SMS". It carries the message ID, phone numbers, event time, and a destination status that describes the message state. api/…/integration_3cx_service.go• L127-140 api/…/integration_3cx_service.go• L142-169
The mapping is intentionally destination-specific. The received event uses the contact as from and the HTTPSMS phone as to. Outbound events reverse those roles. A failed send uses the message.sent event name with the sending_failed status, while a delivered send uses message.finalized with the delivered status.
| HTTPSMS event | 3CX event type | Destination status | Main payload meaning |
|---|---|---|---|
message.phone.received | message.received | No per-recipient status | Incoming text from the contact to the HTTPSMS phone. |
message.phone.sent | message.sent | sent | Outbound text accepted as sent by the phone. |
message.phone.delivered | message.finalized | delivered | Outbound text delivered to the contact. |
message.send.failed | message.sent | sending_failed | Outbound text could not be sent. |
References: api/…/integration_3cx_service.go• L127-169 api/…/integration_3cx_service.go• L172-229 api/…/integration_3cx_service.go• L232-258 api/…/message_phone_received_event.go• L11-25 api/…/message_phone_sent_event.go• L11-25 api/…/message_phone_delivered_event.go• L11-25 api/…/message_send_failed_event.go• L10-24
Webhook delivery service
When an active 3CX message listener passes an event to the service, the service loads the user’s saved connection and sends one JSON POST to its webhook URL. The request has a five-second context limit and a JSON content type. A missing connection is a normal no-op, so users without a 3CX connection do not turn an otherwise valid message event into an error. api/…/integration_3cx_service.go• L59-75 api/…/integration_3cx_service.go• L78-124
The service logs request and response details but does not define the retry and failure-event behavior used by general HTTPSMS webhooks. A 3CX destination therefore depends on the HTTP client and the remote endpoint for delivery handling. Webhooks describes the separate webhook subscription path and its retry and failure notifications.
References: api/…/integration_3cx_service.go• L59-124 api/…/container.go• L971-979 api/…/container.go• L982-1007
Persistent connection state
Conversation thread state
3CX message activity uses the same persistent conversation state as other HTTPSMS messages. A thread belongs to one HTTPSMS user and one owner-contact pair. The service stores the latest message content, status, time, and read state so the activity can appear as one ongoing conversation. api/…/message_thread_service.go• L87-140 api/…/message_thread_service.go• L209-245
Received activity can mark a thread as unread. If the thread is archived, the owner phone’s unarchive setting can reopen it. Deleting a thread removes its stored state and emits a thread-deleted event for the shared event system. api/…/message_thread_service.go• L66-72 api/…/message_thread_service.go• L114-140 api/…/message_thread_service.go• L326-355
References: api/…/message_thread_service.go• L66-72 api/…/message_thread_service.go• L87-140 api/…/message_thread_service.go• L209-245 api/…/message_thread_service.go• L326-355
User-owned connection
The 3CX connection is persistent account state. Each record links an HTTPSMS user to a webhook URL and records its creation and update times. Message records and connection records are separate, so changing or removing the 3CX connection does not remove the user’s SMS history. api/…/integration_3cx.go• L9-20
The repository gives the integration service three account-level operations: save a connection, load a connection by user ID, and delete all connections for a user. The database layer uses the integration_3cx table and treats a missing record as a not-found result. The 3CX message route is a send endpoint, not a connection-management endpoint; the inspected 3CX service path uses load and delete while the repository contract also supports save. api/…/integration_3cx_repository.go• L9-18 api/…/gorm_integration_3cx_repository.go• L34-72 api/…/container.go• L405-411
References: api/…/integration_3cx.go• L9-20 api/…/integration_3cx_repository.go• L9-18 api/…/gorm_integration_3cx_repository.go• L34-72 api/…/container.go• L405-411
Account deletion cleanup
Account deletion removes the user’s 3CX connection through the same event system used for message activity. The 3CX listener decodes user.account.deleted, passes the user ID to the integration service, and deletes all matching connection records. This cleanup is the one 3CX listener subscription that is active today. api/…/user_account_deleted_event.go• L9-16 api/…/integration_3cx_listener.go• L110-123
The cleanup path removes integration state only. It does not remove message data, and it prevents later event handling from finding a saved 3CX destination for the deleted user. This follows the account cleanup pattern used by other user-owned connections.
References: api/…/integration_3cx_listener.go• L33-39 api/…/integration_3cx_listener.go• L110-123 api/…/integration_3cx_service.go• L46-57 api/…/gorm_integration_3cx_repository.go• L34-42
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the 3CX request fields and the phone-number normalization rules aligned with the common message-send parameters.
- Keep the route’s authentication and entitlement checks before the common message-send call. A bypass can create messages outside the intended account and billing boundary.
- If outbound 3CX delivery becomes active, enable the four message subscriptions and update the event-to-3CX mapping as one contract.
- Keep 3CX status names aligned with the receiving 3CX webhook contract. In particular, failed sends use
message.sentwithsending_failed. - Keep the user ID consistent across event payloads, connection lookup, and account-deletion cleanup.
- Keep the implemented success response and generated API description aligned so 3CX clients do not wait for a different status or body shape.
- Preserve the owner-contact and user keys when the shared thread service changes. A mismatch can split 3CX activity across separate conversation records.
- Preserve the timestamp and status ordering rules when message activity updates a thread. Stale events must not replace newer conversation state.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The Bearer API key is absent or cannot identify a user | The request is rejected and no message enters the HTTPSMS send flow | Send a valid user API key through the route’s authentication boundary. |
| The user has no send entitlement | The 3CX request is rejected before a message is created | Check billing entitlement before calling the message service. |
| A message event is not subscribed by the active 3CX listener | The 3CX webhook receives no outbound state update | Keep listener registration and payload mapping aligned. |
| No 3CX connection record exists for the event’s user | The service skips the outbound request without an integration error | Save a user-owned connection before enabling event delivery. |
| Event data cannot be decoded into its expected message payload | No 3CX body is created for that event | Keep event producers and destination payload decoders on the same event contract. |
| The 3CX endpoint times out or the HTTP client cannot reach it | Delivery is logged as failed by the service, with no 3CX-specific retry or failure event | Monitor the destination and keep the endpoint within the request time limit. |
| A 3CX client expects the documented 204 response | The client can treat a successful HTTP 200 response as an error or ignore the returned message ID | Use the handler’s current 200 JSON response and align API documentation when the contract changes. |
| Thread activity uses a different user, owner, contact, timestamp, or status contract | 3CX activity can appear in the wrong conversation or stale state can replace current state | Keep shared thread keys and event ordering aligned with message activity. |
| Account deletion cleanup does not run | The user’s 3CX connection remains in persistent state | Preserve the user.account.deleted subscription and user-scoped delete. |
Key files
| File | Why to read it |
|---|---|
| api/…/integration_3cx_handler.go | Defines the authenticated inbound 3CX SMS endpoint and its response. |
| api/…/integration_3cx_message_request.go | Defines the 3CX input shape and its conversion into the common send flow. |
| api/…/message_service.go | Shows how an integration-originated send becomes a stored message and message events. |
| api/…/message_thread_service.go | Shows how shared 3CX message activity becomes persistent conversation state and thread events. |
| api/…/message_phone_received_event.go | Defines the incoming message event data used by the 3CX mapping. |
| api/…/message_phone_sent_event.go | Defines the sent message event data used by the 3CX mapping. |
| api/…/message_phone_delivered_event.go | Defines the delivered message event data used by the 3CX mapping. |
| api/…/message_send_failed_event.go | Defines the failed-send event data used by the 3CX mapping. |
| api/…/integration_3cx_service.go | Defines connection lookup, outbound HTTP delivery, and 3CX event shaping. |
| api/…/integration_3cx_listener.go | Shows the active event subscriptions and account-deletion cleanup. |
| api/…/event_dispatcher_service.go | Explains the shared asynchronous event and listener pathway. |
| api/…/integration_3cx.go | Defines the user-owned persistent connection state. |
| api/…/gorm_integration_3cx_repository.go | Shows account-scoped load, save, and delete behavior. |
| api/…/container.go | Shows route, service, database, and listener wiring for the integration. |