Phone registration and API keys
Phone registration connects an Android device or phone gateway to one user account and gives that endpoint the settings needed to send and receive messages. Phone API keys provide a separate device credential with a limited scope, while the account owner keeps control of phone configuration and access. See Phone connectivity for the wider device model, Android phone agent for the device client, Phone heartbeats and availability for liveness, and Push delivery and read receipts for delivery behavior.
Enrollment model
Phone records
A phone record represents one phone number and SIM endpoint owned by one user. It holds the normalized number, the selected SIM, the wake-up token or HTTPS callback, and the rules that control message sending. The record is matched by user and normalized phone number, so enrollment updates an existing endpoint instead of creating a duplicate. api/…/phone.go• L12-35 api/…/phone_service.go• L111-149
The owner can change delivery and sending behavior without enrolling the device again. New records use safe defaults for send rate, message expiry, and retry count. SIM1 and SIM2 are separate endpoint choices for dual-SIM devices. api/…/phone_service.go• L245-271 api/…/phone_service.go• L282-309 api/…/phone_handler_validator.go• L62-118
References: api/…/phone.go• L12-35 api/…/phone_service.go• L111-149 api/…/phone_service.go• L245-271 api/…/phone_service.go• L282-309 api/…/phone_handler_validator.go• L62-118
Phone enrollment
Enrollment is an authenticated request that identifies the phone number, SIM slot, and wake-up target. The API accepts either a Firebase token or a public HTTPS adapter callback URL. It normalizes the number before it finds or creates the phone record. api/…/phone_handler.go• L48-51 api/…/phone_fcm_token_request.go• L12-40 api/…/phone_handler_validator.go• L121-164
The service emits a phone update event after it saves the record. When a phone API key made the request, the authenticated context supplies the key identity to that event. The phone API key listener then adds the phone to the key scope. The route and listener are enabled together when the API starts. api/…/phone_service.go• L151-170 api/…/phone_api_key_listener.go• L37-64 api/…/container.go• L139-162 api/…/container.go• L1788-1793
This sequence separates phone storage from key-scope updates. A newly saved phone can therefore exist for a short time before the asynchronous association is complete.
References: api/…/phone_handler.go• L48-51 api/…/phone_fcm_token_request.go• L12-40 api/…/phone_handler_validator.go• L121-164 api/…/phone_service.go• L151-170 api/…/phone_api_key_listener.go• L37-64 api/…/container.go• L139-162 api/…/container.go• L1788-1793
Phone configuration
The account manages phone records through GET /v1/phones, PUT /v1/phones, and DELETE /v1/phones/:phoneID. The update request treats omitted fields as unchanged, so an owner can change one setting without resetting the other settings. The request can also select a send schedule that belongs to the same user. api/…/phone_handler.go• L41-46 api/…/phone_update_request.go• L42-106 api/…/phone_handler_validator.go• L91-118
Phone configuration accepts bounded values for send rate, retries, expiry, and SIM selection. A wake-up target is valid only as a Firebase token or an HTTPS URL. These checks protect later message and notification flows from unusable endpoint state. api/…/phone_handler_validator.go• L62-118 api/…/phone.go• L77-104
Deleting a phone removes the owned record and emits a deletion event. The phone API key listener removes that phone from key scopes, so a deleted endpoint does not remain authorized through an old association. api/…/phone_service.go• L173-206 api/…/phone_api_key_listener.go• L66-80
References: api/…/phone_handler.go• L41-46 api/…/phone_update_request.go• L42-106 api/…/phone_handler_validator.go• L62-118 api/…/phone.go• L77-104 api/…/phone_service.go• L173-206 api/…/phone_api_key_listener.go• L66-80
Phone API key lifecycle
Key creation
A phone API key is a named credential for one or more phone endpoints. An authenticated account creates it with POST /v1/phone-api-keys; the service generates a random credential with a pk_ prefix and starts it with an empty phone scope. Key creation is subject to the account entitlement. api/…/phone_api_key_handler.go• L93-142 api/…/phone_api_key_service.go• L65-92
The owner-facing client displays the credential and can show it as a QR code for transfer to the Android login flow. The key itself is the value used for device authentication. web/…/index.vue• L52-126 web/…/index.vue• L260-295
References: api/…/phone_api_key_handler.go• L45-52 api/…/phone_api_key_handler.go• L93-142 api/…/phone_api_key_service.go• L65-92 web/…/index.vue• L52-126 web/…/index.vue• L260-295
Key association
Each key has a phone scope made from phone IDs and normalized phone numbers. Enrollment adds the phone to the key through the phone update event. A phone can be associated with the account's key set, but moving it to a new key removes it from the other key scopes so one endpoint does not retain stale device access. api/…/phone_api_key.go• L13-23 api/…/phone_api_key_listener.go• L44-64 api/…/gorm_phone_api_key_repository.go• L172-230
The owner can remove one phone from a key or delete the key. Removing the association leaves the phone record in the account. Deleting the key invalidates the credential and removes its access. The affected Android device must enroll again with a valid key. api/…/phone_api_key_handler.go• L144-218 api/…/phone_api_key_service.go• L95-133
References: api/…/phone_api_key.go• L13-23 api/…/phone_api_key_listener.go• L44-80 api/…/gorm_phone_api_key_repository.go• L172-230 api/…/phone_api_key_handler.go• L144-218 api/…/phone_api_key_service.go• L95-133
Key authentication
Phone API key authentication reads the x-api-key header and creates an authentication context. The context contains the user identity, the key identity, and the phone numbers allowed by that key. This keeps device access separate from the user's account-wide access. api/…/phone_api_key_auth_middleware.go• L13-35 api/…/auth_context.go• L5-16 api/…/gorm_phone_api_key_repository.go• L109-138
The resolved context is cached for a short time. Phone association changes clear the relevant cache entry, and key deletion removes the credential from the cache. Access changes therefore take effect without waiting for normal cache expiry. api/…/gorm_phone_api_key_repository.go• L109-138 api/…/gorm_phone_api_key_repository.go• L159-169 api/…/gorm_phone_api_key_repository.go• L172-210
References: api/…/phone_api_key_auth_middleware.go• L13-35 api/…/auth_context.go• L5-16 api/…/gorm_phone_api_key_repository.go• L109-138 api/…/gorm_phone_api_key_repository.go• L159-169 api/…/gorm_phone_api_key_repository.go• L172-210
Device access boundary
Login flow
The Android login flow turns a phone API key into device access by pairing it with one or two phone numbers. It checks the key and server address, sends the wake-up target for each selected SIM, and stores the login data only after enrollment succeeds. Later device requests reuse the key in the x-api-key header. android/…/LoginViewModel.kt• L125-218 android/…/HttpSmsApiService.kt• L22-38 android/…/HttpSmsApiService.kt• L260-294
The enrollment request carries the key identity through the authenticated context and into the phone update event. This lets the server build the key scope as part of enrollment instead of requiring a second device-side association step. api/…/phone_fcm_token_request.go• L30-40 api/…/auth_context.go• L5-10 api/…/phone_service.go• L151-162
The flow is organized as a credential check followed by endpoint enrollment and then normal device use.
sequenceDiagram
participant Owner as Account owner
participant API as API
participant Agent as Android agent
participant Events as Phone event listener
Owner->>API: Create phone API key
API-->>Owner: pk_ credential
Owner->>Agent: Transfer key
Agent->>API: Enroll number, SIM, and wake-up target
API->>API: Save or update phone record
API->>Events: Publish phone update
Events->>API: Add phone to key scope
Agent->>API: Send device request with x-api-key
API-->>Agent: Allow only scoped phone work
References: android/…/LoginViewModel.kt• L125-218 android/…/HttpSmsApiService.kt• L22-38 android/…/HttpSmsApiService.kt• L260-294 api/…/phone_fcm_token_request.go• L30-40 api/…/auth_context.go• L5-10 api/…/phone_service.go• L151-162
Scoped device operations
The key does not grant access to every phone owned by the user. Phone-key routes check the requested number against the key's phone scope before they accept device work. The account credential can work across the user's phones, while a device credential remains limited to its assigned endpoints. api/…/phone_api_key_auth_middleware.go• L13-35 api/…/handler.go• L147-153 api/…/heartbeat_handler.go• L126-134 api/…/message_handler.go• L313-365
The Android agent uses this boundary for outstanding messages, message receipt, heartbeats, missed calls, and delivery events. These operations connect directly to Phone heartbeats and availability and Push delivery and read receipts. android/…/HttpSmsApiService.kt• L41-159 android/…/HttpSmsApiService.kt• L221-257
References: api/…/phone_api_key_auth_middleware.go• L13-35 api/…/handler.go• L147-153 api/…/heartbeat_handler.go• L126-134 api/…/message_handler.go• L313-365 android/…/HttpSmsApiService.kt• L41-159 android/…/HttpSmsApiService.kt• L221-257
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
pk_credential contract aligned between key generation, header authentication, QR transfer, and Android login. - Keep phone number normalization aligned between enrollment, key scope storage, and device scope checks.
- Keep the phone update event's key identity aligned with the listener that adds phones to key scopes.
- Keep key-cache invalidation aligned with phone removal and key deletion so revoked device access does not persist.
- Keep the accepted wake-up target forms aligned between request validation and notification transport selection.
- Keep route registration for phone enrollment, phone-key authentication, and phone-key listeners enabled as one lifecycle.
- Keep partial phone updates distinct from omitted fields so a configuration change does not silently reset other settings.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A device sends a missing, malformed, or non-pk_ key | The API cannot create the device authentication context and rejects protected device work. | Validate the key before enrollment and send it in x-api-key. |
| Enrollment uses an invalid phone number or SIM value | The API cannot identify the intended endpoint. | Normalize numbers to E.164 and accept only SIM1 or SIM2. |
| A wake-up target uses an unsupported URL | Notification delivery cannot select a valid transport. | Use a Firebase token or an HTTPS URL with a host. |
| A phone update event has not yet reached the key listener | The phone record exists but the key may not yet authorize it. | Wait for key-scope association before treating enrollment as ready. |
| A phone is removed from a key | The phone remains owned by the account but the device credential cannot use it. | Re-enroll with a key that contains the phone. |
| A key is deleted | Every device using that credential loses access. | Create or select a valid key and log in again. |
| A request names a phone outside the key scope | Heartbeats or message results for that phone are denied even if the user owns it. | Add the endpoint to the intended key and re-enroll. |
Key files
| File | Why to read it |
|---|---|
| api/…/container.go | Shows the route, middleware, and listener wiring that makes registration and key-scoped access active. |
| api/…/phone_handler.go | Defines phone listing, enrollment, configuration, deletion, and wake-up-target routes. |
| api/…/phone_update_request.go | Shows the phone configuration contract and partial-update behavior. |
| api/…/phone_fcm_token_request.go | Shows the device enrollment payload and how key identity enters phone service parameters. |
| api/…/phone_handler_validator.go | Defines accepted phone, SIM, schedule, and wake-up target values. |
| api/…/phone.go | Defines the business state of a registered endpoint and its notification transport. |
| api/…/phone_service.go | Shows how the system creates, updates, deletes, and announces phone records. |
| api/…/phone_api_key_handler.go | Defines account actions for listing, creating, dissociating, and deleting keys. |
| api/…/phone_api_key_service.go | Shows key generation and the high-level association lifecycle. |
| api/…/phone_api_key_auth_middleware.go | Defines how a device credential becomes a scoped authentication context. |
| api/…/gorm_phone_api_key_repository.go | Shows key lookup, scope changes, and cache invalidation. |
| api/…/phone_api_key_listener.go | Connects phone lifecycle events to key scopes. |
| android/…/LoginViewModel.kt | Shows device enrollment checks and save-after-success login behavior. |
| android/…/HttpSmsApiService.kt | Shows how the Android agent sends the key with device requests. |
| web/…/index.vue | Shows owner-facing key creation, QR transfer, association, and revocation actions. |