Introducing a new way to review your PRs.

Read the announcement

ndolestudio / httpsms

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API with end to end encryption supported.

View on GitHub

22 documents · Updated Nov 18, 2026

docs / messaging-platform

Messaging platform

The messaging platform accepts SMS and MMS activity from API clients and connected Android phones. It stores message history, moves outbound work to the right phone, exposes delivery activity, and links conversation phone numbers to each user’s saved contacts.

System boundary

The platform sits between API clients, connected Android phones, and user-facing message views. A message belongs to a user and a pair of phone numbers: the owner is the connected phone and the contact is the other party.

Outbound message intake

Authenticated clients submit one message or a group of messages through the message API. The platform checks the request and the user’s sending entitlement, creates a durable pending message, and dispatches work for a connected phone; message content can include attachment URLs and an encryption flag. api/…/message_handler.go• L51-59, api/…/message_handler.go• L83-112, api/…/message_service.go• L461-503

An outbound request can carry a client request ID. Bulk requests use one request ID for the batch so later views can group its message records into one delivery summary. api/…/message_send_request.go• L14-29, api/…/message_bulk_send_request.go• L14-29, api/…/gorm_message_repository.go• L172-199

References: api/…/message_handler.go• L51-59 api/…/message_handler.go• L83-112 api/…/message_service.go• L461-503 api/…/message_send_request.go• L14-29 api/…/gorm_message_repository.go• L172-199

Inbound message intake

The connected phone reports a received message through a phone-authenticated endpoint. The platform records the phone, contact, content, time, SIM, encryption state, and any MMS attachments, then makes the activity available to the other event consumers. A missed call follows the same activity model and can create an automatic SMS reply when the phone has one configured. api/…/message_handler.go• L61-67, api/…/message_handler.go• L327-373, api/…/message_service.go• L319-363, api/…/message_service.go• L199-232

Received attachment data is accepted as encoded content and exposed through attachment URLs after it is stored. This keeps the message record small while allowing clients to retrieve the related media. api/…/message_receive_request.go• L14-22, api/…/message_service.go• L605-651

References: api/…/message_handler.go• L61-67 api/…/message_handler.go• L327-373 api/…/message_service.go• L199-232 api/…/message_service.go• L319-363 api/…/message_receive_request.go• L14-22 api/…/message_service.go• L605-651

Phone delivery handoff

The phone does not receive a message by reading the database directly. The platform emits a typed event, and a phone notification consumer turns that event into a notification for the selected phone. The phone then asks for its outstanding message and reports sending, sent, delivered, or failed activity back to the API. api/…/phone_notification_listener.go• L35-72, api/…/message_handler.go• L61-67, api/…/message_service.go• L73-106

This separation lets the API accept work without waiting for radio delivery. It also gives the platform one place to apply phone limits, SIM choice, send timing, and retry rules before the phone acts. api/…/message_service.go• L446-503, api/…/message_api_sent_event.go• L11-29

References: api/…/phone_notification_listener.go• L35-72 api/…/message_handler.go• L61-67 api/…/message_service.go• L73-106 api/…/message_service.go• L446-503 api/…/message_api_sent_event.go• L11-29

Message lifecycle coordination

The platform uses a message record as the durable view of delivery and uses events to move that record through the delivery process. A CloudEvent is a typed event envelope that carries the message ID, user, phone pair, time, and activity data to each interested consumer.

The main outbound path is shown below. Inbound activity starts at the phone and enters through the received-message path.

flowchart LR
    Client[API client] --> Accept[Accept outbound request]
    Accept --> Store[Persist pending message]
    Store --> Event[Dispatch message event]
    Event --> Notify[Notify connected phone]
    Notify --> Phone[Phone sends SMS or MMS]
    Phone --> Status[Phone reports status]
    Status --> Update[Update message record]
    Update --> Fanout[Notify threads, webhooks, live view, and billing]

For the full delivery sequence and state changes, read SMS message lifecycle.

Delivery state model

Message status tells clients where a message is in the platform-to-phone path. The record also keeps event times, send attempts, and a failure reason so a client can distinguish waiting, success, and terminal failure.

StatusMeaning
pendingAccepted and waiting for phone work.
scheduledWaiting for a requested send time or notification time.
sendingClaimed by a phone for delivery.
sentSent by the phone.
deliveredDelivery confirmation is known.
receivedIncoming phone activity is stored.
failedThe phone could not send the message.
expiredThe phone did not send the message within its allowed wait period.

Expiration can lead to another send attempt while the message still has attempts left. This makes expired an important part of the retry path rather than a simple synonym for failed. api/…/message.go• L24-54, api/…/message_service.go• L816-862

References: api/…/message.go• L24-54 api/…/message.go• L124-151 api/…/message_service.go• L662-862

Event fan-out

One message event can update several parts of the product at the same time. The event dispatcher validates events, places delayed work on the queue when needed, and publishes each event to all subscribed listeners.

ConsumerProduct effect
Phone notificationDelivers new or retried work to a connected phone.
Message listenerUpdates the durable message status and expiry state.
Thread listenerUpdates the latest activity shown in a conversation.
Webhook listenerSends message activity to a user’s external endpoint.
Websocket listenerSignals the live client view about phone activity.
Billing listenerRecords accepted sent and received usage.

The same event shape therefore connects delivery work with stored history and user notifications. A failure in one consumer is logged without changing the role of the other consumers. api/…/event_dispatcher_service.go• L50-60, api/…/event_dispatcher_service.go• L99-151, api/…/message_listener.go• L34-47, api/…/message_thread_listener.go• L35-47

References: api/…/event_dispatcher_service.go• L50-60 api/…/event_dispatcher_service.go• L63-117 api/…/event_dispatcher_service.go• L125-151 api/…/message_listener.go• L34-47 api/…/message_thread_listener.go• L35-47 api/…/webhook_listener.go• L33-43 api/…/websocket_listener.go• L34-40 api/…/billing_listener.go• L35-39

Message history and user views

The platform exposes both individual message history and a compact conversation view. Message records hold delivery facts; thread records hold the latest activity and user display state. This split lets the product show a fast inbox without losing the full message history.

Message records

Each record is scoped by user and stores the phone pair, content, attachments, direction, status, request ID, timestamps, and send-attempt data. Clients can fetch one message, list messages for one phone pair, search across a user’s messages, or delete a message. api/…/message.go• L85-122, api/…/message_handler.go• L221-263, api/…/message_handler.go• L421-459, api/…/message_handler.go• L507-550

The search view can filter by owner, message type, and status. It also searches message content, contact number, failure reason, request ID, and a message UUID when the query has that form. api/…/message_service.go• L957-979, api/…/gorm_message_repository.go• L127-169

References: api/…/message.go• L85-122 api/…/message_handler.go• L221-263 api/…/message_handler.go• L421-459 api/…/message_handler.go• L507-550 api/…/message_service.go• L957-979 api/…/gorm_message_repository.go• L127-169

Conversation threads

A message thread groups activity for one owner and one contact. It keeps the latest message, latest status, read state, archive state, display color, and display order so an inbox can show conversation summaries instead of loading every message first. api/…/message_thread.go• L9-26, api/…/message_thread_handler.go• L42-65

Thread updates come from message events. The thread listener creates a thread when needed and advances its latest activity as messages move through delivery states. Incoming messages mark the thread as unread, and a phone setting can unarchive it when new inbound activity arrives. api/…/message_thread_listener.go• L50-75, api/…/message_thread_listener.go• L202-229, api/…/message_thread_service.go• L80-133

Read and archive changes are user actions on the thread view. Deleting a thread removes its messages, while deleting only the latest message restores the prior message summary when one exists. For the full conversation model and search behavior, read Message threads and search. api/…/message_thread_handler.go• L94-179, api/…/message_thread_service.go• L144-199

References: api/…/message_thread.go• L9-26 api/…/message_thread_handler.go• L42-65 api/…/message_thread_handler.go• L94-179 api/…/message_thread_listener.go• L50-75 api/…/message_thread_listener.go• L202-229 api/…/message_thread_service.go• L80-133 api/…/message_thread_service.go• L144-199

Contact association

A saved contact is a user-owned record that gives a name, one or more phone numbers, optional email addresses, and free-form properties to an otherwise numeric conversation party. Threads keep the phone number as their stable link and can resolve matching contact details at read time; the contact data is not stored inside the thread. api/…/contact.go• L16-17, api/…/contact.go• L66-76, api/…/message_thread.go• L20-26

The thread list accepts a contacts option. When enabled, it looks up the thread contact numbers in one user-scoped contact map and adds matching details to the response. If the optional lookup fails, the thread list remains available without the extra contact data. api/…/message_thread_handler.go• L49-65, api/…/message_thread_index_request.go• L13-22, api/…/message_thread_service.go• L281-307

For the contact record model and its role in conversation recipients, read Contacts and conversation recipients.

References: api/…/contact.go• L16-17 api/…/contact.go• L66-76 api/…/message_thread.go• L20-26 api/…/message_thread_handler.go• L49-65 api/…/message_thread_index_request.go• L13-22 api/…/message_thread_service.go• L281-307

Activity exposure

Delivery events reach users through two complementary paths. Webhooks carry message events to configured external systems, while websocket signals let the live client refresh its view; API reads remain the source for message and thread details. api/…/webhook_listener.go• L33-129, api/…/websocket_listener.go• L34-107, api/…/message_handler.go• L239-263

This design keeps event delivery separate from query access. A client can react quickly to a signal and then read the current record when it needs complete content or status. api/…/websocket_listener.go• L58-106, api/…/webhook_listener.go• L46-129

References: api/…/webhook_listener.go• L33-129 api/…/websocket_listener.go• L34-107 api/…/message_handler.go• L239-263

Contact management

Contact management gives each user a searchable address book that can enrich conversations and provide stable recipient data for messaging. The API supports single or batch creation, CSV import, updates, deletion, and paginated search while keeping all contact access within the owning user’s scope.

Contact records

Each contact has a required name and at least one valid phone number. It can also hold normalized email addresses and a string-to-string properties map, which gives clients a small place for labels or other user data. api/…/contact_store.go• L13-19, api/…/contact_handler_validator.go• L170-205, api/…/contact.go• L66-76

Contact writes accept a JSON array or an object with a contacts array. CSV import uses the Name, Emails, and PhoneNumbers columns, with multiple values separated by commas or semicolons. The platform normalizes phone and email values before it validates and stores them, so JSON and CSV entry points produce the same contact form. api/…/contact_store.go• L21-55, api/…/contact_handler_validator.go• L116-167, api/…/contact_handler_validator.go• L334-350

References: api/…/contact_store.go• L13-19 api/…/contact_store.go• L21-55 api/…/contact_store.go• L81-96 api/…/contact_handler_validator.go• L116-205 api/…/contact_handler_validator.go• L334-350 api/…/contact.go• L66-76

Contact API

The authenticated contact API provides the following product actions:

RoutePurpose
GET /v1/contactsList contacts with search, sort, pagination, and a matching total.
POST /v1/contactsCreate one or many contacts from JSON.
POST /v1/contacts/uploadImport contacts from a CSV file.
PUT /v1/contacts/:contactIDReplace the details of one user-owned contact.
DELETE /v1/contacts/:contactIDRemove one user-owned contact.

List search checks the name, email addresses, and phone numbers. The list response reports the number of matching contacts separately from the current page, so clients can build server-side pagination. Contact creation also checks the user’s contact entitlement before it stores a batch. api/…/contact_handler.go• L46-53, api/…/contact_handler.go• L55-103, api/…/contact_handler.go• L106-206, api/…/contact_handler.go• L209-310, api/…/contact_handler.go• L313-321, api/…/contact_responses.go• L17-24

References: api/…/contact_handler.go• L46-53 api/…/contact_handler.go• L55-103 api/…/contact_handler.go• L106-206 api/…/contact_handler.go• L209-310 api/…/contact_handler.go• L313-321 api/…/contact_index.go• L9-43 api/…/contact_responses.go• L5-24

Contact storage and lookup

The contact service provides one user-scoped contract for storage and phone-number lookup. The running API can use the default relational store or a MongoDB store, while the rest of the messaging platform continues to use the same contact behavior. api/…/contact_repository.go• L10-36, api/…/container.go• L926-943

Phone-number lookups are optimized for the thread list and are refreshed when a contact is created, changed, or removed. This keeps read-time conversation enrichment aligned with the saved address book without making the thread record depend on a contact row. api/…/contact_service.go• L119-133, api/…/contact_service.go• L176-220, api/…/contact_service.go• L223-290

References: api/…/contact_repository.go• L10-36 api/…/gorm_contact_repository.go• L63-160 api/…/mongo_contact_repository.go• L75-156 api/…/container.go• L926-943 api/…/contact_service.go• L119-133 api/…/contact_service.go• L176-220 api/…/contact_service.go• L223-290

Contact lifecycle cleanup

Contacts follow the user lifecycle. When the platform receives a user-account-deleted event, the contact listener removes all contacts for that user, just as other user-owned messaging records remove their state. api/…/contact_listener.go• L14-17, api/…/contact_listener.go• L19-40, api/…/contact_listener.go• L43-56

The contact routes and listener are registered with the main application, and the thread service receives the same contact service used by those routes. This wiring makes contact reads and conversation enrichment use one user-scoped source. api/…/container.go• L120-130, api/…/container.go• L687-725, api/…/container.go• L1427-1453, api/…/container.go• L1739-1749

References: api/…/contact_listener.go• L14-17 api/…/contact_listener.go• L19-56 api/…/container.go• L120-130 api/…/container.go• L687-725 api/…/container.go• L1427-1453 api/…/container.go• L1739-1749

Bulk and scheduled delivery

The platform accepts both immediate and delayed delivery. A single request can provide an exact send time, a bulk request can give each row its own time, and a send schedule can define allowed windows in a time zone.

Bulk delivery

Bulk delivery accepts JSON recipient lists or CSV and Excel files. The platform validates the full input, checks entitlement for the requested count, creates one message record per recipient, and applies a per-phone rate delay to messages without an exact send time. api/…/message_handler.go• L114-172, api/…/bulk_message_handler.go• L47-144

Bulk history is not a separate message store. The platform groups message records by their bulk- request ID and returns counts for pending, scheduled, sent, delivered, failed, and expired records. For the bulk flow and file rules, read Bulk and scheduled messaging. api/…/bulk_message.go• L5-16, api/…/gorm_message_repository.go• L172-199

References: api/…/message_handler.go• L114-172 api/…/bulk_message_handler.go• L47-144 api/…/bulk_message.go• L5-16 api/…/gorm_message_repository.go• L172-199

Send schedules

Send schedules are user-owned named sets of weekly time windows with a time zone. The platform can resolve the next allowed send time from those windows, while an empty or invalid schedule falls back to immediate sending. api/…/message_send_schedule.go• L12-27, api/…/message_send_schedule.go• L30-90

Clients can list, create, update, and delete schedules through the send-schedule API. Schedule deletion emits an event so phone records that refer to the schedule can stop using it. api/…/message_send_schedule_handler.go• L45-50, api/…/message_send_schedule_handler.go• L79-224, api/…/message_send_schedule_service.go• L138-164, api/…/phone_listener.go• L34-54

References: api/…/message_send_schedule.go• L12-27 api/…/message_send_schedule.go• L30-90 api/…/message_send_schedule_handler.go• L45-50 api/…/message_send_schedule_handler.go• L79-224 api/…/message_send_schedule_service.go• L138-164 api/…/phone_listener.go• L34-54

Operational visibility

Operational visibility links logs, traces, metrics, and database activity so engineers can follow message work across API calls, queues, phones, and storage. The application creates trace spans for service and database work, adds trace context to structured logs, and exports metrics with exemplars that point only to the stable trace sample.

The telemetry path is shown below. The sampling processor makes the retention choice when a span ends, so it can keep failed, exceptional, truncated, explicitly retained, or slow database work even when ordinary spans are sampled.

flowchart LR
    Work[Message or database work] --> Span[Trace span]
    Span --> Log[Structured log with trace fields]
    Span --> Decide[Retention decision at span end]
    Decide --> TraceStore[Trace exporter]
    Work --> Metrics[Metrics]
    Metrics --> Exemplars[Exemplars for sampled traces]

Trace and log correlation

The shared telemetry contracts let handlers, services, repositories, and database operations carry one request context. Error logs mark the active span for retention, while normal logs keep the same trace and span identifiers without forcing every trace to be exported. api/…/logger.go• L7-37, api/…/otel_tracer.go• L27-69, api/…/zerolog_logger.go• L51-110, api/…/gorm_logger.go• L30-53

References: api/…/logger.go• L7-37 api/…/otel_tracer.go• L27-69 api/…/zerolog_logger.go• L51-110 api/…/gorm_logger.go• L30-53

Span retention

The trace processor keeps a stable half of traces and promotes spans that contain errors, exceptions, dropped data, an explicit retention flag, or database work longer than 200 milliseconds. Metrics use the same stable trace decision for exemplars, which keeps metric links aligned with exported traces. api/…/sampling_span_processor.go• L23-59, api/…/sampling_span_processor.go• L73-103

References: api/…/sampling_span_processor.go• L23-59 api/…/sampling_span_processor.go• L73-103 api/…/container.go• L1973-2004

Data ownership and cleanup

All message, thread, bulk, contact, and schedule activity is scoped to the authenticated user. Phone API access is further checked against the phone that owns the activity, which prevents one connected phone from reporting or fetching another phone’s work. api/…/message_handler.go• L281-324, api/…/message_handler.go• L340-373, api/…/gorm_message_repository.go• L214-229, api/…/gorm_contact_repository.go• L63-80

Account deletion is broadcast as an event. Message records, thread records, contacts, send schedules, phone notifications, and related webhook data then remove their user-owned state through their listeners. api/…/message_listener.go• L310-323, api/…/message_thread_listener.go• L312-325, api/…/contact_listener.go• L43-56, api/…/message_send_schedule_listener.go• L33-55

References: api/…/message_handler.go• L281-324 api/…/message_handler.go• L340-373 api/…/gorm_message_repository.go• L214-229 api/…/gorm_contact_repository.go• L63-80 api/…/message_listener.go• L310-323 api/…/message_thread_listener.go• L312-325 api/…/contact_listener.go• L43-56 api/…/message_send_schedule_listener.go• L33-55

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep message status names and event types aligned across the message entity, phone callbacks, message listeners, thread updates, webhooks, and websocket signals.
  • Preserve the message ID and user ID in every delivery event. They connect phone activity to the durable record and to each user-owned consumer.
  • If bulk request grouping changes, update both the request ID creation rule and the bulk summary query. A mismatch can hide an otherwise valid batch.
  • If send-time rules change, check exact per-message times, bulk row times, rate delays, and time-zone window resolution together.
  • If contact fields or phone normalization changes, update JSON creation, CSV import, updates, validation, both contact storage backends, and thread contact lookup together.
  • If the thread contact option changes, keep the query flag, read-time ContactDetails enrichment, and the contact phone-number lookup contract aligned.
  • If thread display fields change, keep the thread summary response and any user actions that read or update those fields aligned with the persisted thread record.
  • If message or thread deletion changes, check the event that repairs the thread summary and the listener that removes phone notifications.
  • If account deletion changes, keep cleanup listeners aligned for messages, threads, contacts, schedules, phone notifications, and webhooks.
  • If trace or log retention rules change, keep logger severity, span retention flags, database span rules, trace sampling, and metric exemplar filtering aligned.
  • If repository context handling changes, keep user or owner scoping and request deadlines on every heartbeat update that supports phone activity.

Important failure modes

TriggerConsequenceGuard
A phone requests an outstanding message with a wrong user, phone, or message ID.The message is not claimed and the phone has no work to send.Scope the outstanding lookup by user ID, message ID, and allowed phone numbers. api/…/gorm_message_repository.go• L244-278
A delivery event arrives after a message has reached a later state.A stale event can overwrite useful history or be rejected.Message state handlers accept only valid transitions and ignore already sent or delivered work where safe. api/…/message_service.go• L686-769
A message stays pending or sending until its expiry check runs.Delivery stops at expired, with a retry only when attempts remain.Schedule the expiry check after phone notification delivery and enforce the maximum attempt count. api/…/message_service.go• L816-862, api/…/message_service.go• L874-900
An event has no matching subscriber or one subscriber fails.That downstream view does not update even if other views continue.Keep listener registrations aligned with event names and monitor dispatcher errors. api/…/event_dispatcher_service.go• L116-151
A message is deleted while its thread still names it as the latest message.The thread can show content that no longer has a message record.Emit deletion activity and restore the prior message summary or remove the empty thread. api/…/message_service.go• L136-181, api/…/message_thread_service.go• L162-199
A contact uses a phone format that does not match the thread number.The thread remains visible but does not include the saved contact details.Apply the same phone normalization to contact writes and use the normalized number for thread lookup. api/…/contact_store.go• L81-96, api/…/contact_service.go• L223-269
Contact enrichment cannot load the contact store.The thread response lacks optional contact details, while the conversation list still returns.Keep contact details as read-time enrichment and handle lookup errors without failing the base thread query. api/…/message_thread_service.go• L291-307
A contact batch exceeds the user’s allowed contact count.The request returns payment required and no batch is stored.Count the user’s existing contacts and check the additional batch size before CreateMany. api/…/contact_handler.go• L137-153, api/…/contact_handler.go• L188-206
A user-account-deleted event is not delivered to the contact listener.Contacts remain after the rest of the user-owned messaging data is removed.Register the listener for UserAccountDeleted and delete all contacts by user ID. api/…/contact_listener.go• L38-54, api/…/container.go• L1441-1453
A send schedule has no usable window or time zone.Messages can send immediately instead of waiting for a window.Validate schedule input and treat the fallback as an explicit product rule. api/…/message_send_schedule.go• L30-90, api/…/message_send_schedule_handler_validator.go• L1-200
A warning or error is logged without the active span, or metric exemplars use a different sampling rule.Engineers lose the trace link for an important failure or metrics point to traces that were not exported.Attach the span to the logger and use the shared trace sampling decision for exemplar filtering. api/…/zerolog_logger.go• L61-110, api/…/sampling_span_processor.go• L32-47
A slow database span is not marked with a database system attribute.Slow storage work is sampled like ordinary work and can be harder to find.Preserve database span attributes and the 200 millisecond retention rule. api/…/sampling_span_processor.go• L73-85

Key files

FileWhy to read it
api/…/message_handler.goDefines the main send, receive, event, query, and delete API surface.
api/…/message_service.goShows how message intake, persistence, event dispatch, status updates, retries, and search fit together.
api/…/message.goDefines the durable message shape and delivery states.
api/…/event_dispatcher_service.goExplains how typed events move through the queue and fan out to consumers.
api/…/phone_notification_listener.goShows how accepted message work reaches a connected phone.
api/…/message_listener.goShows how phone events update message state and expiry.
api/…/message_thread.goDefines the summary record used by the conversation view, including its display color and optional contact details.
api/…/message_thread_service.goShows how message activity becomes thread activity and how contact details are added at read time.
api/…/message_thread_listener.goMaps message events to thread updates.
api/…/gorm_message_repository.goShows the durable message queries, outstanding-message claim, search, and bulk summaries.
api/…/contact_handler.goDefines contact creation, CSV import, listing, update, and deletion.
api/…/contact.goDefines the user-owned contact shape used by the address book and thread enrichment.
api/…/contact_service.goShows the shared contact lifecycle and phone-number lookup used by conversations.
api/…/contact_repository.goDefines the storage contract shared by the relational and MongoDB contact backends.
api/…/contact_listener.goShows how account deletion removes a user’s contacts.
api/…/bulk_message_handler.goDefines file-based bulk intake and batch-level response behavior.
api/…/message_send_schedule.goDefines time-zone windows and next-allowed-send behavior.
api/…/message_send_schedule_handler.goDefines the user-facing send schedule API.
api/…/sampling_span_processor.goDefines which traces remain available for failures, exceptional data, and slow database work.
api/…/zerolog_logger.goShows how structured logs carry trace context and promote important spans.
api/…/gorm_logger.goConnects database activity and query errors to the shared telemetry path.
api/…/container.goShows application wiring for message listeners, routes, trace export, metrics, and telemetry sampling.
api/…/gorm_heartbeat_monitor_repository.goShows the user-scoped persistence used to track connected-phone heartbeat state.