123eworld Knowledge Hub → Transactional SMS → Page 83
Transactional SMS API Request Validation and Input Sanitization
A developer guide to validating transactional SMS API requests safely, covering phone-number validation, templates, variables, sender authorization, message length, scheduling, Unicode, injection risks and defensive API design.
Why validation belongs at the API boundary
Invalid requests are cheaper to reject before they reach queues, databases and providers. Validation also protects downstream systems from malformed or malicious data.
However, validation is not authorization. A syntactically valid sender ID may still belong to another tenant.
Phone-number validation
Normalize phone numbers into a consistent internal representation before provider submission. Validate country and numbering rules according to the service's supported destinations.
Avoid writing a simplistic regular expression that assumes every international number follows the same local format.
Template validation
If the API uses templates, verify that the requested template exists, is active and is authorized for the tenant and message type.
The renderer should validate required variables and reject unknown or invalid values according to the template schema.
Variable validation
Variables should have defined types, maximum lengths and required status. A customer name may have a length limit, while an appointment date may require a specific representation.
Validation prevents unexpectedly large or malformed messages.
Sender validation
Validate sender ownership and destination compatibility before queueing. Do not allow an arbitrary sender value to bypass the sender registry.
Message length
Calculate length after rendering the final content. The encoding and segmentation rules can change when dynamic values are inserted.
If the business requires one-segment messages, enforce that policy before submission.
Scheduling validation
Scheduled timestamps should be validated for the supported range, time zone and business rules. Reject impossible or ambiguous values instead of allowing workers to interpret them differently.
Unicode and control characters
Validate unexpected control characters and handle Unicode consistently. A message containing an invisible or unsupported character can create encoding or delivery problems.
Injection risks
Template variables should not be allowed to alter template structure or administrative commands. Separate template syntax from variable values and escape or validate according to the rendering engine.
Request size limits
Set reasonable limits on JSON payload size, variable count and message metadata. Large requests can consume resources before the SMS itself is processed.
Validation versus business rules
Some rules are technical, while others are commercial or business-specific. Keep validation modules understandable so a change in one business workflow does not accidentally alter every message type.
Implementation checklist
Validate destination, tenant ownership, template, variables, sender, length, encoding, schedule and request size before queueing. Log rejection reasons safely and consistently.
Validation order
A sensible validation sequence is authentication, tenant authorization, schema validation, resource authorization, business validation, rendering and encoding validation, then queue creation.
This prevents expensive processing of requests that should have been rejected earlier.
Canonical phone-number storage
Store a canonical destination representation for internal processing while preserving any display formatting only where needed. This makes deduplication and routing more reliable.
Validation error privacy
An error should not reveal whether another tenant owns a sender or template. Authorization failures should avoid turning the API into a resource-discovery mechanism.
Business-rule validation
A message can be technically valid but commercially or operationally invalid—for example, an application attempting to send an appointment reminder after the appointment was cancelled. Validate business state before submission where the messaging service owns that responsibility.
Provider preflight
If the platform knows a sender is incompatible with a destination, reject before provider submission. This reduces avoidable provider errors and makes customer feedback faster.
Operational example
A client submits a payment template with a missing amount variable. The API authenticates the client, verifies template access, detects the missing required variable and returns a stable validation error. No queue job or provider request is created.
Final developer checklist
Validate identity, ownership, destination, template, variables, sender, encoding, length, schedule and business eligibility before creating downstream work.
Schema validation tools
Use a well-defined schema validation library and keep validation rules close to the API contract. This reduces differences between documentation and actual runtime behaviour.
However, schema validation should not attempt to encode every business rule into a single JSON schema. Resource ownership and business state often require application logic.
Canonicalization
Normalize values before comparison where appropriate. Phone numbers, identifiers and enumerated fields should have predictable representations.
Do not normalize data in a way that changes customer-visible content without an explicit rule.
Duplicate validation
Validation should work with idempotency rather than against it. A repeated valid request should resolve to the existing message when its idempotency key and payload match, not fail as a duplicate after the first request has completed.
Batch validation
For batch APIs, define whether validation is atomic or per-item. Per-item results are often more useful because one invalid recipient does not necessarily prevent valid notifications from being processed.
The response should clearly identify which items were accepted, rejected or already existed idempotently.
Validation performance
Avoid expensive external validation calls for every request unless necessary. Basic schema, ownership and formatting checks should happen locally; downstream provider checks should be reserved for rules the platform cannot determine itself.
Security validation
Reject unexpected object fields where they could create ambiguity, or explicitly document whether unknown fields are ignored. In security-sensitive APIs, strict schemas can reduce hidden behaviour.
Validation telemetry
Track validation failures by stable code and endpoint. A sudden rise in one validation error can indicate a broken client deployment or a changed template rather than customer behaviour.
Operational scenario
An ERP integration starts sending a new field type for appointment_date. The API schema rejects it with INVALID_DATE_FORMAT and a correlation ID. The ERP team can correct the integration without any message reaching the provider.
Final production checklist
Test schema boundaries, authorization, canonicalization, duplicate requests, batch semantics, Unicode, request size, validation performance and stable error reporting.
Validation and authorization boundary
A request may contain a syntactically valid template ID but still be unauthorized. The platform should avoid revealing resource existence to unauthorized tenants.
For example, returning TEMPLATE_NOT_FOUND for a template owned by another tenant can leak information. A controlled authorization response may be more appropriate depending on the security model.
Validation order and cost
Cheap deterministic validation should happen before expensive operations. There is little value in contacting a provider to discover that a required template variable was missing.
This also reduces provider traffic and makes error response faster for developers.
Validation regression tests
Maintain tests for every important validation rule, including boundary lengths, supported destinations, sender ownership, template status and scheduling limits. When a rule changes, review whether existing customers depend on the previous behaviour.
Final decision guide
Validate what the API can prove locally, authorize every resource access, render and measure the final message, and only then create downstream work. Keep business-state checks close to the component that owns the business state.
Validation and queues
Do not treat the queue as a validation boundary. Invalid work should normally be rejected before queue insertion. The worker should still perform critical authorization and business-state checks because queued data can become stale between API acceptance and processing.
Validation observability
Record validation error codes and endpoint information so repeated integration defects can be identified. A spike in INVALID_TEMPLATE_VARIABLE after a template deployment may indicate a mismatch between the template version and application payload.
Validation rule
The final rendered message is the authoritative validation target for length and encoding. Validating only the original template or raw variables can miss problems introduced during rendering.
Final note
Good validation reduces provider failures, queue pollution and support work while giving developers a predictable integration contract.
Implementation scenario
A customer name contains Unicode characters and a long order reference. The API validates ownership and variables, renders the final message, calculates encoding and segment count, applies the configured length policy and only then queues the notification.
Developer takeaway
Validation should be deterministic wherever possible, authorization should remain server-side, and the queue should receive only work that has passed the gateway's essential correctness checks.
Final validation reminder
Validate before queueing, and revalidate critical state before sending.
Quality control
Before release, compare validation behaviour against the documented API contract and run boundary-value tests for every critical field.
Need transactional SMS integration?
123eworld.com provides Bulk SMS and API-based business communication solutions for enterprises and software applications.