Java SDK

    The Plivo Java SDK makes it simpler to integrate voice and SMS communications into your Java applications using the Plivo REST API. Using the SDK, you’ll be able to make voice calls, send SMS messages, and generate Plivo XML documents to control your call flows.

    Supported Java versions: This SDK works with Java 1.8 and 1.9, meaning 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.

    Installation

    To install the 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’re using Maven, use this XML code 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’re using Gradle, use this line in your dependencies.

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

    To install a beta release

    You can see the full list of Java SDK releases on GitHub. You can use a beta SDK by adding it as a dependency in your dependency management tool. Alternatively, you can use the JAR file.

    If you’re using Maven, use this XML code 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’re using Gradle, use this line in your dependencies.

    compile 'com.plivo:plivo-java:4.3.0-beta-2'
    
    Note: If you’re already using Retrofit, exclude this SDK's dependency to avoid version conflict.

    Getting started

    Authentication

    To make API requests, you need to create a Plivo instance and provide it with authentication credentials, which you can find on the Overview page of the Plivo console.

    We recommend that you store your credentials in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables, 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:

    class Example {
      public static void main(String [] args) {
        Plivo.init();
      }
    }
    

    Alternatively, you can provide them to the Plivo.init() constructor yourself:

    class Example {
      public static void main(String [] args) {
        Plivo.init("<insert your authentication ID here>", "<insert your authentication token here>");
      }
    }
    

    To use multiple clients, create a PlivoClient instance yourself and set it on a request:

    class Example {
      public static void main(String [] args) {
        PlivoClient client = new PlivoClient("<insert your authentication ID here>", "<insert your authentication token here>");
        Message.creator("the_source_number", Collections.singletonList("+14157654321"), "Hello, world!")
                        .client(client)
                        .create();
      }
    }
    

    The basics

    The SDK uses consistent interfaces to create, retrieve, update, delete, and list resources. The pattern is:

    Resource.creator(parameters).create();
    Resource.getter(parameters).get();
    Resource.updater(identifier, parameters).update();
    Resource.deleter(identifier).delete();
    Resource.lister().list();
    

    Using Resource.lister().list() lists the first 20 resources by default (the first page, with limit as 20, and offset as 0). Use limit and offset to get more pages of resources.

    To list all objects of any resource, use the request object itself as an iterable:

    class Example {
      public static void main(String [] args) {
        Plivo.init("<auth_id>","<auth_token>");
        for (Message message : Message.lister()) {
          System.out.println(message.getMessageUuid());
        }
      }
    }
    

    Please note that this makes several requests to the Plivo API, and will pause for a short duration at every 20 resources.

    Examples

    Send a message

    class Example {
      public static void main(String [] args) {
        Plivo.init("<auth_id>","<auth_token>");
        Message.creator("the_source_number", Collections.singletonList("+14157654321"), "Hello, world!")
                        .create();
      }
    }
    

    Make a call

    class Example {
      public static void main(String [] args) {
        Plivo.init("<auth_id>","<auth_token>");
        Call.creator("the_from_number", Collections.singletonList("the_to_number"), "https://answer.url")
                        .answerMethod("GET")
                        .create();
      }
    }
    

    Generate Plivo XML

    class Example {
      public static void main(String [] args) {
        System.out.println(new Response()
                                 .children(
                                   new Speak("Hello, world!")
                                 ).toXmlString());
      }
    }
    

    This generates the XML code:

    <Response>
      <Speak>Hello, world!</Speak>
    </Response>
    

    More examples

    Refer to the Plivo API Reference documentation for more examples.

    Reporting issues

    Report feedback or problems with this SDK by opening an issue on GitHub.