Use this guide to setup your Java application for use with the Numbers API and search for an available Sinch virtual number.
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.
- Set up your Java application
- Search for an available virtual number for SMS, Voice or both.
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/search-available/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/search-available/client/src/main/java/numbers
folder and open theSnippet.java
file.
// This code returns a list of all the available numbers for a given set of search criteria.
package numbers;
import com.sinch.sdk.domains.numbers.api.v1.NumbersService;
import com.sinch.sdk.domains.numbers.models.v1.NumberType;
import com.sinch.sdk.domains.numbers.models.v1.request.AvailableNumberListRequest;
import com.sinch.sdk.domains.numbers.models.v1.response.AvailableNumberListResponse;
import java.util.logging.Logger;
public class Snippet {
private static final Logger LOGGER = Logger.getLogger(Snippet.class.getName());
static void execute(NumbersService numbersService) {
String regionCode = "US";
NumberType type = NumberType.LOCAL;
AvailableNumberListRequest parameters =
AvailableNumberListRequest.builder().setRegionCode(regionCode).setType(type).build();
LOGGER.info(
String.format("Listing available number type '%s' for region '%s'", type, regionCode));
AvailableNumberListResponse response = numbersService.searchForAvailableNumbers(parameters);
response
.iterator()
.forEachRemaining(
number -> LOGGER.info(String.format("Available number details: %s", number)));
}
}
- The code provided in Snippet.java includes default parameters. If you want, you can replace the following values for these parameters with your own values:
Parameter | Your value |
---|---|
regionCode | The two letter abbreviation of the country for which you'd like a number. For example, the United States is US . Should be in ISO 3166-1 alpha-2 format. |
NumberType.LOCAL | The type of number you would like to rent. Available options are: MOBILE , LOCAL , or TOLL_FREE . Note that 10DLC numbers should be set to LOCAL . For more information, see the JavaDocs. |
- 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/search-available/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
These steps should return a list of numbers available to rent for the search criteria you specified.
Copy the phoneNumber
you would like to use and rent your virtual number using the Numbers API.
- You can also see how to rent the first available number.
- Explore the API specification to test more endpoints.
- Prefer a UI to search for a number? Follow the entire number searching and renting process in the Sinch Build Dashboard.