Get Started with Java
Sign up for a Plivo Account
You can sign up with Plivo and start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and buy a number by clicking here. Add a number and credits to your account to test the full range of our voice and SMS service features.
Sign up here to get your free Plivo account today.
Follow these steps to sign up for a free trial account:
- Sign up with your work email address
- Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
- Enter your mobile number to complete the phone verification step.
Sign up with your work email address
If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.
Install Java and the Plivo Java package
You must set up and install Java(Java 1.8 or higher) and Plivo’s Java SDK to send your first SMS. Here’s how.
Install Java
Operating System | Instructions |
---|---|
OS X & Linux | To see if you already have Java installed, run the command java -version in the terminal. If you do not have it installed, you can install it from here. |
Windows | To install Java on Windows follow the instructions listed here. |
Install Plivo Java Package using IntelliJ Idea
- Create a new project in IntelliJ Idea
- Choose a dependency management and Java SE SDK for the new project
- Install the Plivo Java package by adding the dependency in
pom.xml
<dependency>
<groupId>com.plivo</groupId>
<artifactId>plivo-java</artifactId>
<version>4.7.1</version>
</dependency>
Send your first Outbound SMS
This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these prerequisites before we dive into the code.
- Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!
- Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
Now, create a Java class in the project called SendSMS
and paste the following code.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.IOException;
import java.util.Collections;
import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.models.message.Message;
import com.plivo.api.models.message.MessageCreateResponse;
class SendSMS
{
public static void main(String [] args) throws IOException, PlivoRestException {
Plivo.init("auth_id","auth_token");
MessageCreateResponse response = Message.creator("plivo_src_number",
Collections.singletonList("the_destination_number"),
"Hello, this is test message")
.create();
System.out.println(response);
}
}
- Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
- We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
- You can use System.getenv() to store and retrieve environment variables while initializing the client.
- Replace the placeholder plivo_src_number with the Phone number which you have purchased and the_destination_number with the phone number you will be sending SMS text messages to
- Both plivo_source_number and the_destination_number should be in E.164 format, for example +15671234567.
Save the file and run it.
Set up a Spark Webapp for Incoming Messages & Callbacks
In this section, we’ll walk you through how to set up a Spark Webapp in under five minutes and start handling incoming messages & callbacks.
Plivo supports receiving SMS text messages in 9 countries (see complete SMS API coverage). When an SMS is sent to a Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.
Set up a Spark Webapp
- Add Spark web app and SLF4J dependency:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
Create a Java class named ReceiveSMS and paste the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import static spark.Spark.*;
public class ReceiveSms {
public static void main(String[] args) {
get("/receive_sms", (request, response) -> {
// Sender's phone number
String from_number = request.queryParams("From");
// Receiver's phone number - Plivo number
String to_number = request.queryParams("To");
// The text which was received
String text = request.queryParams("Text");
// Print the message
System.out.println(from_number + " " + to_number + " " + text);
return "Message Received";
});
}
}
Run the project and you should see your basic server app in action on http://localhost:4567/receive_sms
Exposing your local server to the internet
To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:
./ngrok http <port_on_which_your_local_server_is_running>
This will give you a UI with links that look like ngrok.io/*
which you can use to access your local server using the public network.
Create an Application
- Create an Application by visiting the Application Page and click on
New Application
. You can also use Plivo’s Application API. - Give your application a name. Let’s call it
Receive SMS
. Enter your server URL (e.g.http://example.com/receive_sms/
) in theMessage URL
field and set the method asPOST
. See our Application API docs to learn how to modify your application through our APIs. - Click on
Create
to save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
- Select
Receive SMS
(name of the app) from the Plivo App dropdown list. - Click on
Update
to save.
Test and validate
Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL
with the parameters listed in the XML Request - Messages Documentation.
Reply to an incoming SMS
When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.
Create a Java class named ReplytoInbound and paste the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import com.plivo.api.xml.Message;
import com.plivo.api.xml.Response;
import static spark.Spark.*;
class ReplytoInbound {
public static void main(String[] args) {
post("/reply_to_inbound/", (request, response) -> {
response.type("application/xml");
Response resp = new Response()
.children(
new Message("+12023222222", "+15671234567", "Hi, message from Plivo.")
.callbackMethod("POST")
.callbackUrl("http://foo.com/sms status/")
.type("sms")
);
return resp.toXmlString();
});
}
}
Test and validate
Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL
with the parameters listed in the XML Request - Messages Documentation.