You can quickly see how the Voice API works by calling yourself using the API and the Java SDK.
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.
- Apache Maven and a familiarity with how to use the Maven CLI.
To quickly get started setting up a simple client application using the Java SDK:
- If you haven't already, clone the sinch-sdk-java repository.
- Navigate to the
sinch-sdk-java/examples/getting-started/voice/make-a-call/src/main/resourcesfolder. - Open the
config.propertiesfile. Using the Voice app credentials from your Sinch Build Dashboard, uncomment the following fields and populate them with your values:
| Field | Description |
|---|---|
| APPLICATION_API_KEY | The unique ID of your application. |
| APPLICATION_API_SECRET | The secret for your application. |
- Save the file.
Navigate to the examples/getting-started/voice/make-a-call/src/main/java/voice folder and open the Snippet.java file.
The code provided in Snippet.java contains some placeholder values. You'll need to update the parameters detailed in the following subsections with your values.
// Use this code to make a phone call using the Voice API and the Java SDK.
/**
* 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);
}
}In this example you want to call a phone number. Change the value of the recipientPhoneNumber parameter to the phone number you verified in your dashboard in E.164 format.
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.
Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the examples/getting-started/voice/make-a-call/src/main/java/voice folder and run the following command:
mvn packageThis command creates the target folder and application.
Now you can execute the code and make your text-to-speech call. Run the following command:
java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jarYou 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." Additionally you should see the response with the callId in the console.
Now that you know how to make a call, learn how to handle an incoming call.