Make a call with Java SDK

You can quickly see how the Voice API works by calling yourself using the API and the Java SDK.

In this guide you will learn:

  1. How to set up your Java application.
  2. How to call your phone number.
Note:

Before you can get started, you need the following already set up:

  • Set all Voice API configuration settings.
  • JDK 8 or later and a familiarity with how to create a new Java application.
  • Gradle and a familiarity with how use the Gradle build tools.

Set up your Java application

  1. Create a new folder where you want to store your application. Open a command prompt or terminal to that location and execute the following command:
    Copy
    Copied
    gradle init

    This command starts up the process to create a new blank Java application. You can select the default options for most of the prompts, but ensure you select that you want to create an application.

    Note:

    This guide assumes you're using Gradle but if you want to use another Java build tool, we've provided installation instructions for the most popular ones below.

  2. Open App.java in your favorite editor and copy/paste in the App.java sample found on this page. Remember to keep your own package com.

Now that you've set up your application file, you must install the SDK itself and populate the code sample with your corresponding information.

The easiest way to install the SDK is using the Maven central repository.

Installing the SDK

You can create your Java project using your favorite method. Once your project is created, you need to add the SDK as a dependency. Depending on if you are using Gradle or Maven, you can add the necessary dependencies in the following ways:

Gradle

Add or ensure the following is in the application's build.gradle file:
Gradle with GroovyGradle with Kotlin
Copy
Copied
...
repositories {
    ...
    mavenCentral()
    ...
}
...

dependencies {
    ...
    implementation 'com.sinch.sdk:sinch-sdk-java:+'
    ...
}
...
Copy
Copied
...
repositories {
    ...
    mavenLocal()
    ...
}
...

dependencies {
    ...
    implementation("com.sinch.sdk:sinch-sdk-java:+")
    ...
}
...

Maven

Add the following to the pom.xml file:
Copy
Copied
<dependency>
    <groupId>com.sinch.sdk</groupId>
    <artifactId>sinch-sdk-java</artifactId>
    <version>LATEST</version>
</dependency>

Modify your application

  1. Open the App.java file in your project folder, located in \app\scr\main\java\app, and populate that file with the "App.java" code found on this page.
  2. The code provided in App.java includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

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 Verification app. You can find all of the credentials you need on your Sinch dashboard.

Copy
Copied
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());
    }
}
Note:

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.

Destination number

In this example you want to call a phone number. Change the value of the DestinationNumber parameter to the phone number you verified in your dashboard in E.164 format.
Note:

When your account is in trial mode, you can only call your verified numbers. If you want to call any number, you need to upgrade your account!

Save the file.

Make your first call

Now you can execute the code and make your text-to-speech call. Run the following command:

Copy
Copied
gradle run

You should receive a phone call to the number you called with the message "Hello, this is a call from Sinch. Congratulations! You made your first call."

Next steps

Now that you know how to make a call, learn how to handle an incoming call.

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.