<

Java Helper

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

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.

Prerequisites

  • Java 1.8 or higher.

Installation

You can use this SDK by adding it as a dependency in your dependency management tool.

Maven

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

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

Alternatively, you can use the JAR file.

Gradle

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

compile 'com.plivo:plivo-java:4.1.3'
  • Alternatively, you can use the JAR file.

Simple Java Project

If you are creating a simple Java project, you can use the fatJAR.

Getting started

Authentication

To make the API requests, you need to create a Plivo instance and provide it with authentication credentials (which can be found at https://console.plivo.com/dashboard/).

We recommend that you store your credentials in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialise 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 these to Plivo.init()’s 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, you can 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("the_destination_number"), "Hello, world!")
                    .client(client)
                    .create();
  }
}

The Basics

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

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

Using Resource.lister().list() would list the first 20 resources by default (which is the first page, with limit as 20, and offset as 0). To get more, you will have to use limit and offset to get the second page of resources.

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

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

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

Examples

Send a message

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

Make a call

class Example {
  public static void main(String [] args) {
    Plivo.init();
    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 following XML:

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

More examples

Refer to the Plivo API Reference for more examples.

Reporting issues

Report any feedback or problems with this version by opening an issue on Github.

Please note that this is the latest version. If you are looking for the legacy version, you can get it here.