Rent and configure your virtual number using Java
After searching for a number, rent and configure that number for SMS, Voice or both.
Note:
Before you can get started, you need the following already set up:
- All Numbers API prerequisite steps.
- 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.
- A virtual number you have confirmed to be available.
Steps:
- Set up your Java application
- Rent and configure the virtual number for SMS, Voice or both.
Set up your Java application
- 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:
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.
- Open
App.java
in your favorite editor and copy/paste in the App.java sample found on this page. Remember to keep your ownpackage 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'sbuild.gradle
file:...
repositories {
...
mavenCentral()
...
}
...
dependencies {
...
implementation 'com.sinch.sdk:sinch-sdk-java:+'
...
}
...
...
repositories {
...
mavenLocal()
...
}
...
dependencies {
...
implementation("com.sinch.sdk:sinch-sdk-java:+")
...
}
...
Maven
Add the following to thepom.xml
file:<dependency>
<groupId>com.sinch.sdk</groupId>
<artifactId>sinch-sdk-java</artifactId>
<version>LATEST</version>
</dependency>
Modify your application
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
Before initializing a client using this SDK, you'll need three pieces of information:
- Your Project ID
- An access key ID
- An access key Secret
Note:
To start using the SDK, you need to initialize the main client class and create a configuration object to connect to your Sinch account. You can find all of the credentials you need on your Sinch dashboard.
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;
public class App {
public static void main(String[] args) {
SinchClient client = new SinchClient(Configuration.builder()
.setKeyId("YOUR_access_key")
.setKeySecret("YOUR_access_secret")
.setProjectId("YOUR_project_id")
.setSmsRegion(SMSRegion.US)
.build());
}
}
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;
public class App {
public static void main(String[] args) {
SinchClient client = new SinchClient(Configuration.builder()
.setKeyId("YOUR_access_key")
.setKeySecret("YOUR_access_secret")
.setProjectId("YOUR_project_id")
.setSmsRegion(SMSRegion.EU)
.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.
Fill in remaining parameters
Replace the remaining placeholder values for these parameters with your values:
Parameter | Your value |
---|---|
YOUR_service_plan_id | Your SMS service plan IDThis is only required for SMS configuration. |
YOUR_app_id | The Voice app ID. Found in the Customer Dashboard. This is only required for Voice configuration. |
YOUR_phone_number | The virtual phone number that you previously searched for and would like to rent. |
- Save the file.
Rent and configure the virtual number
gradle run
Response
You should get a response similar to this one:
{
"phoneNumber": "+12025550134",
"projectId": "51bc3f40-f266-4ca8-8938-a1ed0ff32b9a",
"displayName": "string",
"regionCode": "US",
"type": "MOBILE",
"capability": [
"SMS"
],
"money": {
"currencyCode": "USD",
"amount": "2.00"
},
"paymentIntervalMonths": 0,
"nextChargeDate": "2019-08-24T14:15:22Z",
"expireAt": "2019-08-24T14:15:22Z",
"smsConfiguration": {
"servicePlanId": "82b42acf74924bd687ef9fb212f2060c",
"scheduledProvisioning": {
"servicePlanId": "82b42acf74924bd687ef9fb212f20611",
"status": "WAITING",
"lastUpdatedTime": "2019-08-24T14:15:22Z",
"campaignId": "string",
"errorCodes": [
"INTERNAL_ERROR"
]
},
"campaignId": "string"
},
"voiceConfiguration": {
"appId": "string",
"scheduledVoiceProvisioning": {
"appId": "string",
"status": "WAITING",
"lastUpdatedTime": "2019-08-24T14:15:22Z"
},
"lastUpdatedTime": "2019-08-24T14:15:22Z"
}
}
Next steps
Send a message to yourself using the SMS API or the Voice API (after setting each up accordingly) to verify that the configuration was successful.
Additional resources
- Explore the API specification to test more endpoints.
- Follow the number rental process in the Customer Dashboard UI rather than through this API.