The Sinch Java SDK allows you to quickly interact with the Voice API from inside your Java applications. When using the Java SDK, the code representing requests and queries sent to and responses received from the Voice API are structured similarly to those that are sent and received using the Voice API.
The fastest way to get started with the SDK is to check out our getting started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.
You can also view the generated JavaDocs for the Java SDK here.
This guide describes the syntactical structure of the Java SDK for the Voice API, including any differences that may exist between the API itself and the SDK. For a full reference on Voice API calls and responses, see the Voice API Reference.
The code sample below is an example of how to use the Java SDK to make a text to speech phone call to phone number using the Java SDK and the Voice API. We've also provided an example that accomplishes the same task using the REST API.
/**
* Sinch Java Snippet
*
* <p>This snippet is available at https://github.com/sinch/sinch-sdk-java
*
* <p>See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details
*/
package voice.callouts;
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.domains.voice.api.v1.CalloutsService;
import com.sinch.sdk.domains.voice.models.v1.callouts.request.CalloutRequestTTS;
import com.sinch.sdk.domains.voice.models.v1.destination.DestinationPstn;
import com.sinch.sdk.models.Configuration;
import java.util.logging.Logger;
import utils.Settings;
public class Call {
private static final Logger LOGGER = Logger.getLogger(Call.class.getName());
public static void main(String[] args) {
String applicationKey = Settings.getApplicationKey().orElse("MY_APPLICATION_KEY");
String applicationSecret = Settings.getApplicationSecret().orElse("MY_APPLICATION_SECRET");
// The phone number you want to call, in E.164 format (e.g., +12025550123)
String recipientPhoneNumber = "RECIPIENT_PHONE_NUMBER";
String textToSpeech = "Hello, this is a call initiated from Sinch Java SDK. Goodbye.";
Configuration configuration =
Configuration.builder()
.setApplicationKey(applicationKey)
.setApplicationSecret(applicationSecret)
.build();
SinchClient client = new SinchClient(configuration);
CalloutsService calloutsService = client.voice().v1().callouts();
LOGGER.info(String.format("Calling phone number '%s'", recipientPhoneNumber));
CalloutRequestTTS request =
CalloutRequestTTS.builder()
.setDestination(DestinationPstn.from(recipientPhoneNumber))
.setText(textToSpeech)
.build();
String response = calloutsService.call(request);
LOGGER.info("Response: " + response);
}
}This example highlights the following required to successfully make a Voice API call using the Sinch Java SDK:
When using the Sinch Java SDK, you initialize communication with the Sinch backend by initializing the Java SDK's main client class. This client allows you to access the functionality of the Sinch Java SDK.
To start using the SDK, you need to initialize the main client class and create a configuration object to connect to your Sinch account and Voice app. You can find all of the credentials you need on your Sinch dashboard.
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
public class App {
public static void main(String[] args) {
SinchClient client = new SinchClient(Configuration.builder()
.setApplicationKey("YOUR_application_key")
.setApplicationSecret("YOUR_application_secret")
.build());
}
}For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials.
The Sinch Java SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, client.voice().[endpoint_category()].[method()].
In the Sinch Java SDK, Voice API endpoints are accessible through the client. The naming convention of the endpoint's representation in the SDK matches the API:
voice().v1().callouts()voice().v1().calls()voice().v1().conferences()voice().v1().applications()voice().v1().sinchEvents()
For example:
var response = client.voice()
.v1()
.callouts()
.textToSpeech(CalloutRequestTTS
.builder()
.setDestination(DestinationPstn.from("YOUR_phone_number"))
.setText("Thank you for calling Sinch. This call will now end.")
.build());Requests and queries made using the Java SDK are similar to those made using the Voice API. Many of the fields are named and structured similarly.
Many fields in the Java SDK are rendered as enums in data models.
When making calls directly to the API, we use JSON objects, including (in some cases) nested JSON objects. When using the Java SDK, we use Java data models instead of nested JSON objects. For example, consider the Voice configuration objects below. One is represented in JSON, the other as a Java object:
CalloutRequestTTS.builder()
.setDestination(DestinationPstn.from("YOUR_phone_number"))
.setText("Thank you for calling Sinch. This call will now end.")
.build()Note that in the Java SDK you would use a specific helper class for the type of Voice call you want to make and a builder() method to construct the appropriate data model in the correct structure.
Response fields match the API responses. They are delivered as Java objects.