Code example in programming language JAVA for SMS API integration

Below you will find the examples of programming code in the language JAVA to send SMS messages through the API of the LabsMobile platform.

You also have code examples in JavaScript, Node.js, jQuery and other programming languages.

We recommend you to consult and take into account the following resources and help in your integration:

Send SMS - REST API - JSON

This is the sample code in JAVA to send SMS messages with the LabsMobile SMS API in the JSON version. As you can see, you must create a JSON structure with all the sending parameters and make an HTTP/POST call with the JSON data in the body of the call.

It is essential to make the call with the authentication of the username (registration email) and the password (or token API) of the account.

            
HttpResponse<String> response = Unirest.post("https://api.labsmobile.com/json/send")
  .header("Content-Type", "application/json")
  .basicAuth("myusername","mypassword")
  .header("Cache-Control", "no-cache")
  .body("{\"message\":\"Text of the SMS message\", \"tpoa\":\"Sender\",\"recipient\":[{\"msisdn\":\"12015550123\"},{\"msisdn\":\"447400123456\"},{\"msisdn\":\"5212221234567\"}]}")
  .asString();

You can check all available JSON parameters, configuration options, manuals and specifications in the following url: https://apidocs.labsmobile.com/?java#send-sms

Credit inquiry - REST API - JSON

Example of code in JAVA for consulting the balance of an account. The result is always obtained in internal credits of the LabsMobile platform.

            
HttpResponse<String> response = Unirest.get("https://api.labsmobile.com/json/balance")
  .basicAuth("myusername","mypassword")
  .header("Cache-Control", "no-cache")
  .asString();

Send SMS - HTTP/GET

Example of sending for the GET version of the LabsMobile SMS API. This is a basic and simple method of sending SMS messages from an application or software created in JAVA passing all the parameters in the same url.

It is important to encode all the values ​​as url (with the function urlencode() for example).

You can see and download the GET API manual of LabsMobile in the following URL: https://www.labsmobile.com/en/api-sms/api-versions/http-get

                
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

...

HttpURLConnectionExample http = new HttpURLConnectionExample();

String url = "http://api.labsmobile.com/get/send.php?username=[X]&password=[X]&msisdn=34609036253&sender=SENDER&message=This+is+the+message";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
	response.append(inputLine);
}
in.close();
System.out.println(response.toString());

Credit inquiry - HTTP/GET

                
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

...

HttpURLConnectionExample http = new HttpURLConnectionExample();

String url = "http://api.labsmobile.com/get/balance.php?username=[X]&password=[X]";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
	response.append(inputLine);
}
in.close();
System.out.println(response.toString());
  • contact form support

    Sign up!

    Create a user account and send your SMS messages in seconds. You will have all the functionalities and benefits in the same platform.

    Send from API and manage your account with our online application WebSMS.

    Create new account
  • Marketing agency, Events and SMS marketing campaigns recommends LabsMobile

    Bulk SMSLanding messaging campaigns for large brands, and to hold and manage sporting events.

    Watch more reviews
  • dashboard aplicación online

    Maximum reliability at the best price

    At LabsMobile we only offer direct routes of maximum reliability and quality. Enjoy our platform and all our services for the price of an SMS.

    Pay ONLY for sent messages.

    Check our rates
  • dashboard aplicación online

    Need more info? Contact us!

    Our technical department has professionals with years of experience and we have made multiple integrations.

    We guide and help you through the process.

    Request technical support
  • Manage and update your mobile phone database

    This tutorial explains how to maintain a list of mobile phones in the WebSMS application over time so that sendings have a higher percentage of delivery and success.

    Ir al tutorial
loader