After searching for a number, rent and configure that number for SMS.
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.
- Apache Maven and a familiarity with how to use the Maven CLI.
- A virtual number you have confirmed to be available.
- Set up your Java application
- Rent and configure the virtual number for SMS.
To quickly get started setting up a simple client application using the Java SDK:
- If you haven't already, clone the sinch-sdk-java-quickstart repository.
- Navigate to the
sinch-sdk-java-quickstart/getting-started/numbers/rent-and-configure/client/src/main/resources
folder. - Open the
config.properties
file. Using the access key credentials from your Sinch Build Dashboard, populate the following fields with your values:
Field | Description |
---|---|
SINCH_PROJECT_ID | The unique ID of your Project. |
SINCH_KEY_ID | The unique ID of your access key. |
SINCH_KEY_SECRET | The secret that goes with your access key. Note: For security reasons, this secret is only visible right after access key creation. |
- Save the file.
- Navigate to the
getting-started/numbers/rent-and-configure/client/src/main/java/numbers
folder and open theSnippet.java
file.
// Use this code to rent and configure a virtual number with the Java SDK.
package numbers;
import com.sinch.sdk.domains.numbers.api.v1.NumbersService;
import com.sinch.sdk.domains.numbers.models.v1.ActiveNumber;
import com.sinch.sdk.domains.numbers.models.v1.Capability;
import com.sinch.sdk.domains.numbers.models.v1.NumberType;
import com.sinch.sdk.domains.numbers.models.v1.SmsConfiguration;
import com.sinch.sdk.domains.numbers.models.v1.request.AvailableNumberRentAnyRequest;
import java.util.Collections;
import java.util.logging.Logger;
public class Snippet {
private static final Logger LOGGER = Logger.getLogger(Snippet.class.getName());
static void execute(NumbersService numbersService) {
String servicePlanId = "YOUR_service_plan_id";
String regionCode = "YOUR_region_code";
Capability capability = Capability.SMS;
NumberType numberType = NumberType.LOCAL;
LOGGER.info(
String.format(
"Sending request to rent the first available number and configure it with the"
+ " pre-configured service plan id '%s' to use the SMS capability",
servicePlanId));
ActiveNumber response =
numbersService.rentAny(
AvailableNumberRentAnyRequest.builder()
.setCapabilities(Collections.singletonList(capability))
.setType(numberType)
.setRegionCode(regionCode)
.setSmsConfiguration(
SmsConfiguration.builder().setServicePlanId(servicePlanId).build())
.build());
LOGGER.info(String.format("Rented number: %s", response));
}
}
- The code provided in Snippet.java includes placeholder parameters. Replace the following values for these parameters with your own values:
Parameter | Your value |
---|---|
YOUR_service_plan_id | Your SMS service plan IDThis is only required for SMS configuration. |
YOUR_phone_number | The virtual phone number that you previously searched for and would like to rent. |
- 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 sinch-sdk-java-quickstart/getting-started/numbers/rent-and-configure/client
folder and run the following command:
mvn package
This command creates the target
folder and application.
Now you can run the code with the following command:
java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar
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"
}
}
Send a message to yourself using the SMS API to verify that the configuration was successful.
- Explore the API specification to test more endpoints.
- Follow the number rental process in the Sinch Build Dashboard UI rather than through this API.