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.
If you’re using Gradle, use this line in your dependencies.
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.
If you’re using Gradle, use this line in your dependencies.
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:
Alternatively, you can provide them to the Plivo.init() constructor yourself:
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:
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:
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:
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
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
Generate Plivo XML
This generates the XML code:
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
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.