Make a call with Java
You can quickly see how the Voice API works by calling yourself using the API.
What you need to know before you start
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
Create a new folder where you want to keep your app project. Then, open a terminal or command prompt to that location.
Create a new Java application using Gradle with the following command:
gradle init
app
, and then accept the defaults for the rest of the options.Modify your application
Open theApp.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.Note:
This tutorial uses basic authentication for testing purposes. We recommend using a signed request for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use request signing authentication instead.
to
parameter in the body of the request. The Voice API uses callouts to make voice calls. In this guide, we are using the ttsCallout
or "text-to-speech callout" to call a number and then play a text-to-speech message before hanging up.To
In this example you want to call a phone number. Change the value of theto
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!
Fill in your parameters
Before you can run the code, you need to update some more values so you can connect to your Sinch account. Update the following parameters with your own values:
Parameter | Your value |
---|---|
key | The application key found on your Sinch dashboard. Click on your application to find the key. |
secret | The application secret found on your Sinch dashboard. Click on your application to fine the secret. |
fromNumber | Any number you've assigned to your application. This can be a number you've purchased or your free test number. Find the number on your Sinch dashboard by clicking on your app and looking in the Inbound Numbers section. |
locale | The language and locale you want to use for the text-to-speech call. Locale is specified with a language code according to ISO 639 , a dash and a country code according to ISO 3166-1 alpha-2 . For example, American English is represented by en-US . |
Save the file.
Make your first call
Now you can execute the code and make your text-to-speech call. Run the following command:
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."
Tip:
Troubleshooting tip
If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.
Next steps
Now that you know how to make a call, learn how to handle an incoming call.