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>5.9.3</version>
</dependency>
If you’re using Gradle, use this line in your dependencies.
compile 'com.plivo:plivo-java:5.9.3'
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'
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("<auth_id>", "<auth_token>");
}
}
Replace the auth placeholders with your authentication credentials from the Plivo console.
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("<auth_id>", "<auth_token>");
Message.creator("<caller_id>", "<destination_number>", "Hello, world!")
.client(client)
.create();
}
}
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).
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", "+14157654321", "Hello, world!")
.create();
}
}
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).
Make a call
class Example {
public static void main(String [] args) {
Plivo.init("<auth_id>","<auth_token>");
Call.creator("<caller_id>", Collections.singletonList("<destination_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>
Log Level
We’ve introduced a customizable logging mechanism in the Java SDK that enables you to choose the level of logging in your development/production environment.
Log-level | Description |
NONE | No logs |
BASIC | Logs request and response line |
HEADER | Logs request and response line along with their headers |
BODY | Logs request and response line along with their headers and bodies |
Example
package com.plivo.api.samples.call;
import com.plivo.api.Plivo;
import com.plivo.api.models.base.LogLevel;
class Example {
public static void main(String [] args) {
Plivo.init("<auth_id>","<auth_token>", LogLevel.NONE); // LogLevel.NONE is the default value.
// Plivo.init("<auth_id>","<auth_token>", LogLevel.BASIC);
// Plivo.init("<auth_id>","<auth_token>", LogLevel.BODY);
// Plivo.init("<auth_id>","<auth_token>", LogLevel.HEADERS);
}
}
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.