Get Started with Java

    Sign up for a Plivo Account

    Whenever a customer signs up with Plivo, they are provided with a free trial account to experiment with our services. This free trial account is given free credits for testing and using the same credits you can go ahead and purchase a number by navigating here. Once you have a number and credits in your account, you are all set to start testing our voice and SMS service.

    Register here to get your free Plivo account today.

    Below are the steps that are to be followed to successfully Sign up for a free trial account.

    1. Sign up with your corporate email address

    2. You will receive an activation email from Plivo. Click on the link mentioned in the email to activate your account.

    3. Enter an sms enabled, non-VOIP number to complete the phone verification.

    Sign up with your corporate email address

    Sign up with email addres

    If you are facing any issues while creating a Plivo account, you can reach out to our Support Team

    Install Java and the Plivo Java Server SDK

    To get started with Sending your First SMS, you have to set up java and install Plivo java SDK.

    Install Java

    Operating SystemInstructions
    OS X & LinuxYou would already have java installed, you can check this by running the command java --version in the terminal. If you don't have it installed, you can install it using homebrew.
    WindowsTo install java on Windows you can download it from here and install.

    Install Plivo Java Package

    Supported Java versions: This SDK works with Java 1.8 & 1.9 i.e., JDK 8 and JDK 9. While using the SDK with Java 1.9, you may have to use the --add-modules java.se.ee flag to include modules that are no longer present by default.

    To Install Stable release

    You can use this SDK by adding it as a dependency in your dependency management tool. Alternatively, you can use the JAR file.

    If you are using Maven, use the following XML to include the Plivo SDK as a dependency.

    <dependency>
      <groupId>com.plivo</groupId>
      <artifactId>plivo-java</artifactId>
      <version>4.4.0</version>
    </dependency>
    

    If you are using Gradle, use the following line in your dependencies.

    compile 'com.plivo:plivo-java:4.4.0'
    

    To Install Beta release

    You can use this SDK by adding it as a dependency in your dependency management tool. Alternatively, you can use the JAR file.

    If you are using Maven, use the following XML to include the Plivo SDK as a dependency.

    <dependency>
      <groupId>com.plivo</groupId>
      <artifactId>plivo-java</artifactId>
      <version>4.3.0-beta-2</version>
    </dependency>
    

    If you are using Gradle, use the following line in your dependencies.

    compile 'com.plivo:plivo-java:4.3.0-beta-2'
    
    Note: If you are already using Retrofit, exclude this SDK's dependency so there is no version conflict.

    Send your first Outbound SMS

    We’ll see how to use Plivo APIs to send SMS messages from your web application. Let’s have a look at some 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!

    credentials

    • Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Buy New Number

    Create a file called SendSMS.java and paste the below code.

    Code

    1
    2
    3
    4
    5
    6
    7
    
    class Example {
      public static void main(String [] args) {
        Plivo.init();
        Message.creator("plivo_src_number", Collections.singletonList("the_destination_number"), "Hello, world!")
                        .create();
      }
    }
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console
    • Replace the placeholder plivo_source_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 use the below command to run it.

    java SendSMS.java
    
    Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Set up Sinatra for Incoming Messages & Callbacks

    In this section, you will learn how to set up Sinatra in under 5 minutes and start handling Incoming Messages & Callbacks.

    Set up Sinatra

    Use the following code snippet to start a local server.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    require 'sinatra'
    require 'plivo'
    
    include Plivo
    include Plivo::XML
    
    get '/speak/' do
      response = Response.new
      speak_body = 'Testing Sinatra application'
      response.addSpeak(speak_body)
    
      xml = PlivoXML.new(response)
      xml.to_xml
    end
    

    This code returns the following XML

    1
    2
    3
    
    <Response>
        <Speak>Testing Sinatra application</Speak>
    </Response>
    

    Save this code in any file (let’s say the file name is plivo-server.java). To run this file on the server, go to the folder where this file resides and use the following command:

      java plivo-server.java
    

    And you should see your basic server app in action on http://localhost:4567/speak/

    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.

    ngrok block diagram

    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.

    ngrok

    You can check the XML in your browser by accessing the public URL with /speak/ appended.

    Receive an inbound SMS

    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.

    Create a file called receive_sms.java and paste the below code.

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    
    require "sinatra"
    
    get "/receive_sms/" do
      from_number = params[:From]
      to_number = params[:To]
      text = params[:Text]
      puts "Message received - From: #{from_number}, To: #{to_number}, Text: #{text}"
    end
    

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application or by using Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive SMS. Enter your server URL (e.g. http://example.com/receive_sms/) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create to save your application.

    Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Receive SMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive SMS App

    Test it out

    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.

    Note: In order to reply to a US or Canadian phone number, the `'src'` (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.

    Create a file called reply_to_sms.java and paste the below code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    require "plivo"
    require "sinatra"
    
    include Plivo
    include Plivo::XML
    
    get "/reply_to_sms/" do
      from_number = params[:From]
      to_number = params[:To]
      text = params[:Text]
      puts "Message received - From: #{from_number}, To: #{to_number}, Text: #{text}"
    
      # send the details to generate an XML
      response = Response.new
      params = {
        src: to_number,
        dst: from_number,
      }
      message_body = "Thank you, we have received your request"
      response.addMessage(message_body, params)
      xml = PlivoXML.new(response)
    
      content_type "application/xml"
      return xml.to_s()
    end
    
    Note: Create an application in the Plivo console with your Server URL in the message_URL field and assign the app to a Plivo number.

    Test it out

    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.