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 / accounts-and-billing

Accounts and billing

Accounts and billing connect an authenticated person to a user account, a subscription plan, and a record of message use. The system creates account state on first authenticated access, keeps subscription state in step with the payment provider, checks plan limits before protected actions, and records sent and received messages for each billing cycle.

Account lifecycle

An account starts with an authentication identity and becomes a local user record when the API first needs it. The local record holds the account profile, access keys, active phone choice, notification settings, and subscription state. See Identity and user preferences for the user-facing identity and preference details.

Authentication context

An authentication context is the small identity record attached to a request after the API accepts a credential. Firebase bearer tokens and the user API key both resolve to the same user ID and email, so later account and billing actions use one account boundary.

The API creates a local user lazily when an authenticated request first loads the account. A new account starts on the free plan and receives a generated user API key. A user-created event then lets other account systems react without coupling them to the request that created the account.

References: api/…/auth_context.go• L5-16 api/…/bearer_auth_middleware.go• L15-48 api/…/api_key_auth_middleware.go• L13-37 api/…/gorm_user_repository.go• L157-189 api/…/gorm_user_repository.go• L209-247

Account deletion

An authenticated user can delete the account and its stored data. The system blocks deletion when a paid subscription still has a future renewal date. After deletion succeeds, an account-deleted event removes the user's billing usage and deletes the matching Firebase authentication identity.

The deletion event is therefore the boundary between the local account action and cleanup in other systems. A failure in either event listener can leave related records out of step with the deleted local user.

References: api/…/user_service.go• L273-307 api/…/user_account_deleted_event.go• L9-17 api/…/billing_listener.go• L76-89 api/…/user_listener.go• L155-169

Authenticated user configuration

The authenticated user record is the source for settings that affect API behavior and user messages. The account API reads and updates the current user, while notification settings use the authenticated user's ID to prevent changes to another account.

User preferences

The user can set a time zone and an active phone. The time zone controls how account times are presented and is also retained with account-related messages. The active phone identifies the phone that the account uses for its normal phone path.

The user can independently enable or disable message status, webhook, heartbeat, and newsletter notifications. These settings are checked when the system decides whether to send some account notifications. See Identity and user preferences for the detailed preference flow.

References: api/…/user.go• L73-90 api/…/user_service.go• L178-239 api/…/user_update_request.go• L12-42 api/…/user_notification_update_request.go• L7-24

Account interfaces

Authenticated account actions are exposed under the /v1/users API. The current-user endpoints return account state, update the time zone and active phone, and delete the account. Related endpoints manage notification settings, subscription actions, and subscription payment records.

The user API key is a separate account credential used by API clients. Rotating it changes the stored key and emits an event for the key-rotation notification. Phone login keys have a different scope and are covered by Phone registration and API keys.

References: api/…/user_handler.go• L41-52 api/…/user_handler.go• L54-78 api/…/user_handler.go• L81-119 api/…/user_handler.go• L234-265 api/…/user_service.go• L242-270

Subscription state

Subscription state represents the plan that controls account access and message limits. Lemon Squeezy is the system that reports subscription changes; the API converts those reports into account events and stores the resulting plan, provider ID, status, renewal time, and end time on the user.

Subscription lifecycle

Subscription changes move through a provider webhook and an internal event before they update the user record. This keeps the account model as the local source for entitlement checks while the payment provider remains the source for payment and subscription changes.

The main lifecycle states have these effects:

Provider eventAccount effect
CreatedStores the subscription ID, plan, active status, and renewal time.
UpdatedRefreshes active subscription details and any end time. Non-active updates do not replace the stored state.
CancelledKeeps the plan and provider ID, clears the renewal time, and stores the planned end time and status.
ExpiredClears the provider ID and subscription dates and returns the user to the free plan.

The provider maps variant names to the supported monthly, yearly, lifetime, and message-volume plans. An unknown variant falls back to the free plan, which protects the local account from granting an unknown entitlement.

References: api/…/lemonsqueezy_service.go• L61-89 api/…/lemonsqueezy_service.go• L92-120 api/…/lemonsqueezy_service.go• L123-158 api/…/lemonsqueezy_service.go• L160-189 api/…/lemonsqueezy_service.go• L192-234 api/…/user_service.go• L367-387 api/…/user_service.go• L426-469 api/…/user_service.go• L472-499

Subscription management

Users can cancel a subscription, open the payment provider's customer portal to change it, view subscription payments, and request a PDF invoice. These actions use the stored provider subscription ID. A free account has no subscription ID, so its payment list is empty rather than a local invoice record.

See Subscription billing and entitlements for the detailed provider flow, plan behavior, and usage rules.

References: api/…/user_handler.go• L178-231 api/…/user_handler.go• L267-339 api/…/user_service.go• L61-83 api/…/user_service.go• L390-424 web/…/billing.ts• L39-86

Entitlement evaluation

An entitlement is the decision that an account may perform an action under its current plan. The system evaluates message volume and the number of selected account resources. Contact creation uses the plan's message limit as its resource limit.

Message limits

Message entitlement compares the current billing-cycle total with the limit for the user's plan. The total includes both sent and received messages. A single-message request checks one message, while a bulk request checks the full requested count before work starts.

The plan limits are:

Plan groupMessage limit per cycle
Free200
Pro monthly, Pro yearly, Pro lifetime5,000
Ultra monthly, Ultra yearly10,000
20K monthly, 20K yearly20,000
50K monthly50,000
100K monthly100,000
200K monthly200,000

When a request would exceed its limit, the API returns payment-required feedback and sends a limit email at most once during a 12-hour window. Near-limit alerts are also sent at plan-specific thresholds for the free, Pro, Ultra, and 20K plans.

References: api/…/user.go• L17-37 api/…/billing_usage.go• L22-30 api/…/billing_service.go• L51-117 api/…/message_handler.go• L83-111 api/…/message_handler.go• L128-148

Resource limits

Resource entitlement checks limit creation of selected account resources. The free plan allows one message send schedule and one phone API key. Contact creation uses the plan's message limit, so its allowed count grows with the subscription plan. Other resources have no configured limit. Checks can be disabled by the entitlement setting, in which case the action is allowed.

The check runs before the resource is created. It loads the user's plan, counts the resource for that user, and returns payment-required feedback when the count reaches the plan limit. See Phone registration and API keys for the phone API key lifecycle.

References: api/…/entitlement_service.go• L16-25 api/…/entitlement_service.go• L59-127 api/…/contact_handler.go• L304-318 api/…/phone_api_key_handler.go• L93-122 api/…/message_send_schedule_handler.go• L80-110

Protected actions

Message sending checks message entitlement before it adds work to the send path. The same check covers single sends, bulk sends, and message paths that can create a billable message. Contact creation and selected resource creation use the entitlement service to count the requested objects before they are stored.

This separation matters when a plan changes. Message access follows the stored subscription plan and current usage, while resource creation follows either the resource limit map or the plan's message limit for contacts.

References: api/…/message_handler.go• L83-111 api/…/message_handler.go• L128-148 api/…/message_handler.go• L340-370 api/…/bulk_message_handler.go• L90-117 api/…/contact_handler.go• L304-318 api/…/integration_3cx_handler.go• L55-76 api/…/discord_handler.go• L340-365

Recorded billing usage

Billing usage is the account's time-bounded record of message activity. Each record stores sent messages, received messages, total cost, the account ID, and the start and end of its billing cycle. The current record is created when needed, so an account can report zero use before its first message.

Usage cycle

The billing cycle is monthly and uses an anchor day. A paid account uses the day of its subscription renewal date. A free account uses the day of account creation. If an anchor day does not exist in a month, the cycle uses the last day of that month.

The following diagram shows how account state determines the cycle that receives each usage event.

flowchart LR
    E[Sent or received message event] --> U[Find current account usage]
    U -->|Record exists for event time| I[Increase sent or received count]
    U -->|No record| A[Read account billing anchor]
    A --> C[Create monthly cycle]
    C --> I
    I --> Q[Use total for entitlement checks]

References: api/…/user.go• L131-139 api/…/billing_usage.go• L9-30 api/…/gorm_billing_usage_repository.go• L103-139 api/…/gorm_billing_usage_repository.go• L165-211

Usage recording

The billing listener records usage from message events rather than from the HTTP request that starts a send. A sent-message event increases sent use, and a phone-received event increases received use. This lets the recorded count follow the message system's actual event flow.

The current usage endpoint returns the active cycle. The history endpoint returns completed cycles in reverse time order and supports bounded paging. Account deletion removes all usage records for that account.

References: api/…/billing_listener.go• L16-39 api/…/billing_listener.go• L42-74 api/…/billing_handler.go• L39-43 api/…/billing_handler.go• L45-112 api/…/gorm_billing_usage_repository.go• L49-100 api/…/gorm_billing_usage_repository.go• L142-162

Billing overview

The web billing store loads current use and use history from the billing API. It also exposes subscription management and invoice actions so the account view can connect usage feedback with the action needed to change a plan.

References: web/…/billing.ts• L19-86

Change checklist

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

  • Keep provider variant mapping aligned with the subscription names and message limits used by entitlement checks.
  • Keep subscription event payload fields aligned with the user fields that store provider ID, status, renewal time, and end time.
  • Keep sent and received message event types routed to billing when message flow names change.
  • Keep billing-cycle anchor rules aligned with subscription renewal updates and account creation defaults.
  • Keep account deletion listeners aligned so local user, Firebase identity, and usage records do not survive as separate account fragments.
  • Keep payment and usage API paths aligned with the web billing store.
  • Keep contact-count checks aligned with subscription message limits when plan limits change.

Important failure modes

TriggerConsequenceGuard
A provider sends an unknown subscription variantThe account falls back to the free plan and can lose paid access.Add the variant mapping before the provider uses the new plan.
A subscription update has a non-active statusThe local subscription record is not updated by that event.Confirm the provider event sequence includes the later cancellation or expiry event.
A message usage event is not delivered or cannot be storedUsage and message entitlement can be lower than actual activity.Treat message event delivery and billing usage writes as a linked contract.
Billing usage cannot be loaded during a message entitlement checkThe check allows the message after logging the billing lookup error.Monitor billing repository errors and repair usage records before limits are reached.
A contact count is read from an incomplete account viewContact creation can exceed the plan's message-based limit.Keep contact counting and subscription limit evaluation in the same entitlement contract.
A paid account has a future renewal date during deletionAccount deletion is rejected.Cancel the subscription through the subscription flow first.
Account deletion succeeds but a cleanup listener failsFirebase identity or billing usage can remain after local account deletion.Monitor account-deleted event handling and retry failed listeners.

Key files

FileWhy to read it
api/…/user.goDefines account state, plan names, plan message limits, and billing anchor behavior.
api/…/user_service.goCoordinates account creation, updates, deletion, subscription state, and payment actions.
api/…/lemonsqueezy_service.goTranslates payment-provider subscription changes into account events.
api/…/billing_service.goApplies message limits, records message use, and sends usage alerts.
api/…/entitlement_service.goApplies plan-based limits to contacts and selected resource creation.
api/…/billing_usage.goDefines the usage record and its total and entitlement rules.
api/…/gorm_billing_usage_repository.goShows how current and historical billing cycles are selected and updated.
api/…/billing_listener.goConnects message events and account deletion to billing records.
api/…/user_listener.goApplies subscription and account lifecycle events to user state.
api/…/user_handler.goExposes authenticated account and subscription actions.
api/…/billing_handler.goExposes current and historical usage to clients.
api/…/contact_handler.goApplies subscription-based limits before contact creation.
web/…/billing.tsConnects the web account view to usage, subscription, and invoice actions.
Identity and user preferencesChild page for authentication identity and account settings.
Subscription billing and entitlementsChild page for payment state, limits, and recorded usage.
Phone registration and API keysRelated page for phone credentials and their account-level limits.