Send OTP via SMS from Java and Spring Boot
Spring Boot 3 + RestClient calling a TRAI-compliant OTP SMS API: JSON, env-based keys, and patterns for DLT-backed transactional SMS from JVM backends.
StartMessaging Team
Engineering
We already published hands-on guides for Node.js, Python, and PHP/Laravel. This article is the Java / Spring Boot counterpart—new code samples, not copy-pasted explanations of DLT or pricing.
Why This Guide Exists
Enterprise and fintech teams in India often standardize on JVM backends. They need a Spring-friendly shape: beans, properties, and HTTP clients that fit existing patterns. See API documentation for exact paths and headers.
Configuration and Secrets
Store the API key in environment variables or Spring Cloud Secret—not in source control. Align with key rotation practices.
# application.yml (example)
startmessaging:
api-key: ${STARTMESSAGING_API_KEY}
base-url: https://api.startmessaging.comSend OTP Request
Use Spring 6.1+ RestClient to POST JSON matching your template. Replace placeholders with your template ID and variables.
record OtpSendRequest(
String phoneNumber,
String templateId,
Map<String, String> variables
) {}
// RestClient bean
public OtpSendResponse sendOtp(OtpSendRequest body) {
return restClient.post()
.uri("/otp/send")
.header("X-API-Key", apiKey)
.body(body)
.retrieve()
.body(OtpSendResponse.class);
}Map response fields to your persistence layer the same way you would in Node or Python: store requestId for verification and status polling.
Verify and Status
Follow the same verification sequence as in the OTP verification flow. Use idempotency keys on send if your client retries POST requests.
Next Steps
Add integration tests with mocks per staging and testing OTP, then wire Spring Security so only authenticated admins can trigger bulk test sends.
FAQ
See FAQ above.
Related Articles
Step-by-step Node.js tutorial to send and verify OTP via SMS using the StartMessaging API. Includes fetch examples, error handling, and verification flow.
Learn what idempotency keys are, why they matter for OTP APIs, and how to implement them correctly to prevent duplicate SMS charges and improve reliability.
Key lifecycle for SMS OTP APIs: dual-key cutover, secrets storage, incident response, and protecting credentials used for TRAI DLT-compliant sends.
Ready to Send OTPs?
Integrate StartMessaging in 5 minutes. No DLT registration required.