Skip to Content
Java

Java

Welcome to the ReSMS Java integration guide.

This guide explains how you can quickly and securely send SMS messages using the ReSMS API in your Java applications.

Features

  • Send SMS: Send text messages to phone numbers.
  • Send OTP: Generate and send one-time passwords (OTP) for user verification.
  • Verify OTP: Validate OTP codes entered by users.

Installation

First, install our official ReSMS Java SDK:

<dependency> <groupId>dev.resms</groupId> <artifactId>resms-java-sdk</artifactId> <version>1.1.0</version> </dependency>

Setup

You need to get an API key on ReSMS Dashboard. Then import the package and create a new instance of the ReSMS class with your API key.

import dev.resms.ReSMS; import dev.resms.core.exception.ReSMSException; import dev.resms.services.sms.model.SendSmsOptions; public class Main { public static void main(String[] args) { ReSMS resms = new ReSMS("re_12345"); // Replace with your actual API key } }

Send SMS

Here’s a minimal example to send an SMS:

import dev.resms.ReSMS; import dev.resms.core.exception.ReSMSException; import dev.resms.services.sms.model.SendSmsOptions; public class Main { public static void main(String[] args) { ReSMS resms = new ReSMS("re_12345"); SendSmsOptions smsOptions = SendSmsOptions.builder() .to("+33612345678") .message("Welcome to ReSMS!") .build(); try { resms.sms().send(smsOptions); } catch (ReSMSException e) { System.err.println("Error: " + e.getMessage()); } } }

Parameters

Possible parameters for sending an SMS:

FieldTypeRequiredDescription
toStringThe phone number of the recipient, in international format (E.164)
messageStringThe SMS content (160 characters max)
senderIdStringThe alphanumeric sender name displayed

Here is the parameter type in Java:

public class SendSmsOptions { private String to; private String message; private String senderId; }

Response

Success Response

The sms.send() method returns a response when the SMS has been successfully sent. The response type is:

public class SendSmsResponse extends Response { private SendSmsResponseData data; public static class SendSmsResponseData { private String messageId; } }

Error Response

In case of an error, a ReSMSException is thrown:

public class ReSMSException extends Exception { public ReSMSException(String message) { super(message); } }

The sms.send() method may return the following errors:

NameMessage
COUNTRY_DETECTION_FAILEDUnable to detect country code from phone number
PHONE_NUMBER_PARSING_FAILEDInvalid phone number
INSUFFICIENT_SMS_QUOTAFailed to update message status
MESSAGE_STATUS_UPDATE_FAILEDFailed to update message status

Send OTP

Here is a minimal example to send an OTP verification code to a phone number:

import dev.resms.ReSMS; import dev.resms.services.otp.model.SendOtpOptions; public class Main { public static void main(String[] args) { ReSMS resms = new ReSMS("re_12345"); SendOtpOptions otpOptions = SendOtpOptions.builder() .to("+33612345678") .message("Your OTP code is {CODE}") .build(); try { resms.otp().send(otpOptions); } catch (ReSMSException e) { System.err.println("Error: " + e.getMessage()); } } }

Parameters

Possible parameters for sending an OTP:

FieldTypeRequiredDescriptionDefault Value
toStringThe phone number of the recipient, in international format (E.164)
messageStringThe SMS content (160 characters max). It must contain {CODE}.
senderIdStringThe alphanumeric sender name displayed
codeTypeString ("NUMERIC"| "ALPHA")The type of OTP code to generate"NUMERIC"
codeLengthintThe length of the OTP code6
validityMinutesintThe validity period of the OTP in minutes.5

Response

Success Response

The otp.send() method returns a response when the OTP has been successfully sent. The response type is:

public class SendOtpResponse extends Response { private SendOtpResponseData data; @Getter public static class SendOtpResponseData { private String phoneNumber; private String expiresAt; } }

Error Response

In case of an error, a ReSMSException is thrown with the error details.

The otp.send() method may return the following errors:

CodeHTTP StatusMessage
INVALID_MESSAGE_TEMPLATE400Message template must contain {CODE} placeholder
INVALID_CODE_LENGTH400Code length must be between 4 and 16
INVALID_VALIDITY_DURATION400Validity duration must be between 1 minute and 1440 minutes (24 hours)
TOO_MANY_OTP_REQUESTS429Too many OTP requests for this phone number

Verify OTP

To verify an OTP code entered by the user:

import dev.resms.ReSMS; import dev.resms.services.otp.model.VerifyOtpOptions; public class Main { public static void main(String[] args) { ReSMS resms = new ReSMS("re_12345"); VerifyOtpOptions verifyOptions = VerifyOtpOptions.builder() .to("+33612345678") .code("123456") .build(); try { VerifyOtpResponse response = resms.otp().verify(verifyOptions); System.out.println("OTP verified: " + response.getData().getOtpId()); } catch (ReSMSException e) { System.err.println("Error: " + e.getMessage()); } } }

Parameters

Possible parameters for verifying an OTP:

FieldTypeRequiredDescription
toStringThe phone number the OTP was sent to, in international format (E.164)
codeStringThe verification code entered by the user

Here is the parameter type in Java:

public class VerifyOtpOptions { private String to; private String code; }

Response

Success Response

The otp.verify() method returns a response indicating whether the OTP is valid or not. The response type is:

@Getter public class VerifyOtpResponse extends Response { private VerifyOtpResponseData data; @Getter public static class VerifyOtpResponseData { private String otpId; private String phoneNumber; private String verifiedAt; } }

Error Response

In case of an error, a ReSMSException is thrown with the error details.

The otp.verify() method may return the following errors:

CodeHTTP StatusMessage
OTP_NOT_FOUND404OTP not found
OTP_INVALID400OTP is invalid
OTP_EXPIRED400OTP has expired
OTP_ALREADY_VERIFIED400OTP already verified
INVALID_OTP_CODE400Invalid OTP code

Have feedback, found a bug, or want to suggest a feature?

Last updated on