Send OTP SMS in Java Bulk SMS API Documentation

Send OTP SMS in Java

Generate One Time Password Token to the Recipient in Java.

Application API URL:

https://www.smsgateway.center/OTPApi/send

 

Import Send OTP SMS API postman collection.

Postman Collection

 

 

Key Value Description
Login Credentials (Required Parameters)
Authenticate your API request using userId-password or apiKey. You should use either one to authenticate your login.
userId Your Registered Username The registered username parameter to be passed. You can use this if apiKey is not being used.
password Your password The password needs to be urlencoded if there are any special characters used in the password field. You can use this if apiKey is not being used.
apiKey Your unique apiKey apiKey needs to be sent as HTTP header when you are not using userId and password credentials. You can avail this from your user control panel and use instead of userId and password HTTP Request parameter. Please do not disclose this to anyone.
Required Parameters
mobile Mobile Number with Country Code Enter your Customer's mobile number which needs to be verified.

Comments: Keep number in International format (with country code).
senderId Approved Sender Name For Indian customers, sender name would be of 6 alpha characters. For International customers, sender name depends on country to country basis.
msgType text|unicode This is to define simple or UNICODE message. For simple English, you can give value as text or for non-English messages, give value as unicode.
sendMethod generate

Required: This value required for send OTP code to recipient's mobile number.

msg Your text message with $otp$ variable Message content to send. (Default Message Content: Your verification code is $otp$)

Comments: Include variable as {{OTP}}, rest content can be sent as per requirement.
Default Parameters (Optional)
codeExpiry Time in seconds Optional. Minimum 60 Seconds, Maximum 1800 Seconds, System default is 60 Seconds.

Comments: Expiry Time of OTP Token to Verify, in Seconds.
codeLength OTP Token Length Optional. Minimum Length 4, Maximum Length 10, System default Length is 4.

Comments: Number of Digits/Characters in Length for OTP Token.
codeType num|alpha|alphanum Optional. num for Numeric token, alpha for Alphabetic token and alphanum for AlphaNumeric token. System default is num.

Comments: Type of the OTP token to be generated.
retryExpiry Retry Time in seconds Optional. Minimum 15 Seconds, Maximum 1800 Seconds, System default is 60 Seconds.

Comments: Maximum time to allow your users to regenerate OTP. E.g. If you have set 60 seconds, then user cannot request OTP again for next 60 seconds.
renew true|false Optional. Set to True to resend/regenerate new OTP Token. System default is false.

Comments: If OTP token needs to be resent/regenerated.
medium sms|email Optional. Name of the medium to deliver OTP to. You can deliver on 2 medium simultaneously. System default is sms.

Comments: You can use comma separated with maximum 2 mediums, that is sms,email. OTP token will be delivered to both sms and email.
emailId valid Email ID Optional. Give valid email id to deliver OTP code. Use only if OTP needs to be delivered to customer's Email ID.System default is null.

Comments: From Email ID will be shown as registered Email ID and From Name will be shown as Registered Full Name as per your account settings.
callback Valid callback script URL with http or https protocol Optional. To save data on your database or by capturing the response you can further email or use voice call to send OTP token.

Comments: To receive generated OTP token details, you can set a callback URL. Response Parameters will be sent to your url as status=success&type=new&generationTime=2017-12-06%2023%3A18%3A15&validity=120%20secs&otp=6754
format plain|json|xml Value for response format.

System default is plain. If you need responses in JSON or XML then you have to give value as JSON or XML.

Save this Page on your Favorite Social Media

Sample Request Code

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


public class SMSGatewayCenter{

public static void main(String[] args)
{
//Your apikey key
String apiKey = "YourApiKey";
//OR
String userId = "YourUserName";
String password = "YourPassword";

//Send Method (generate|verify)
String sendMethod = "generate";

//Message type text/unicode
String msgType = "text";

//Multiple mobiles numbers separated by comma
String mobile = "919999999999";
//Your approved sender id
String senderId = "SGCSMS";
//Your message to terminate, URLEncode the content
String msg = "Your SMSGatewayCenter OTP Code is $otp$";

//Final OTP Code Expiry
String codeExpiry = "600";

//OTP Code Length
String codeLength = "6";

//OTP Code Type
String codeType = "num";

//Retry After time
String retryExpiry = "60";

//set to true if you need to regenerate new OTP while resending OTP code
String renew = "false";

//Valid http url to receive response on your server. E.g http://example.com/getOTPResponse.php
String callback = "";

//use comma separated for multiple. E.g. sms,email
String medium = "sms";

//if medium is used with comma separated for multiple. E.g. sms,email then use valid user's email address
String emailId = "";

//response format
String format = "json";

//Prepare Url
URLConnection myURLConnection=null;
URL myURL=null;
BufferedReader reader=null;

//URL encode message
String urlencodedmsg=URLEncoder.encode(msg);

//API End Point
String mainUrl="https://www.smsgateway.center/OTPApi/send?";

//API Paramters
StringBuilder sendSmsData= new StringBuilder(mainUrl);
sendSmsData.append("apiKey="+apiKey);
sendSmsData.append("&userId="+userId);
sendSmsData.append("&password="+password);
sendSmsData.append("&sendMethod="+sendMethod);
sendSmsData.append("&msgType="+msgType);
sendSmsData.append("&mobile="+mobile);
sendSmsData.append("&senderId="+senderId);
sendSmsData.append("&msg="+urlencodedmsg);
sendSmsData.append("&codeExpiry="+codeExpiry);
sendSmsData.append("&codeLength="+codeLength);
sendSmsData.append("&codeType="+codeType);
sendSmsData.append("&retryExpiry="+retryExpiry);
sendSmsData.append("&medium="+medium);
sendSmsData.append("&emailId="+emailId);
sendSmsData.append("&format="+format);
//final string
mainUrl = sendSmsData.toString();
try
{
//prepare connection
myURL = new URL(mainUrl);
myURLConnection = myURL.openConnection();
myURLConnection.connect();
reader= new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
//reading response
String response;
while ((response = reader.readLine()) != null)
//print response
System.out.println(response);

//finally close connection
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Response - Success

{
    "status": "success",
    "mobile": "91xxxxxxxxxx",
    "transactionId": "aa40b4b8-7bb8-4bf6-b94a-fa5b82df4511",
    "statusCode": "900",
    "type": "new",
    "reason": "OTP successfully generated.",
    "createTime": "1513772384340",
    "expiryTime": "1513772444340",
    "retryAfter": "1513772444340"
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<simpleMessageSuccessResponse>
    <mobile>91xxxxxxxxxx</mobile>
    <status>success</status>
    <statusCode>900</statusCode>
    <reason>OTP successfully generated.</reason>
    <transactionId>9754e2de-5306-41cb-8602-836c5e7bb065</transactionId>
    <createTime>1513772182439</createTime>
    <expiryTime>1513772242439</expiryTime>
    <retryAfter>1513772242439</retryAfter>
</simpleMessageSuccessResponse>
status=success | mobile=91xxxxxxxxxx | reason=OTP successfully generated. | transactionId=975d55f6-f983-4a79-907e-71f69a62154b | errorCode=900 | type=new | createTime=1513772280207 | expiryTime=1513772340207 | retryExpiry=1513772340207

Response - Error

{
    "status": "error",
    "statusCode": "922",
    "reason": "Invalid mobile numbers."
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorResponse>
    <status>error</status>
    <statusCode>922</statusCode>
    <reason>Invalid mobile numbers.</reason>
</errorResponse>
status=error | errorCode=922 | reason=Invalid mobile numbers.