# Sinch Java SDK for Numbers The Sinch Java SDK allows you to quickly interact with the from inside your Java applications. When using the Java SDK, the code representing requests and queries sent to and responses received from the are structured similarly to those that are sent and received using the . The fastest way to get started with the SDK is to check out our [getting started](/docs/numbers/getting-started/) guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK. ## Syntax Note: You can also view the generated JavaDocs for the Java SDK [here](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/package-summary.html). Note: This guide describes the syntactical structure of the Java SDK for the Numbers API, including any differences that may exist between the API itself and the SDK. For a full reference on Numbers API calls and responses, see the [Numbers API Reference](/docs/numbers/api-reference/numbers). This code sample is an example of how to use the Java SDK to list the available numbers of a given type and region. We've also provided an example that accomplishes the same task using the REST API. SDK Snippet.java // This code returns a list of all the available numbers for a given set of search criteria. /** * Sinch Java Snippet * *
This snippet is available at https://github.com/sinch/sinch-sdk-java * *
See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details */ package numbers; import com.sinch.sdk.SinchClient; 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.AvailableNumbersListQueryParameters; import com.sinch.sdk.domains.numbers.models.v1.response.AvailableNumberListResponse; import com.sinch.sdk.models.Configuration; import java.util.logging.Logger; import utils.Settings; public class SearchForAvailableNumbers { private static final Logger LOGGER = Logger.getLogger(SearchForAvailableNumbers.class.getName()); public static void main(String[] args) { String projectId = Settings.getProjectId().orElse("MY_PROJECT_ID"); String keyId = Settings.getKeyId().orElse("MY_KEY_ID"); String keySecret = Settings.getKeySecret().orElse("MY_KEY_SECRET"); // ISO 3166-1 alpha-2 country code of the phone number. e.g. "US", "GB", "SE"... // See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for details String regionCode = "MY_REGION_CODE"; NumberType type = NumberType.LOCAL; Configuration configuration = Configuration.builder() .setProjectId(projectId) .setKeyId(keyId) .setKeySecret(keySecret) .build(); SinchClient client = new SinchClient(configuration); NumbersService numbersService = client.numbers().v1(); AvailableNumbersListQueryParameters parameters = AvailableNumbersListQueryParameters.builder() .setRegionCode(regionCode) .setType(type) .build(); LOGGER.info("Looking for available numbers"); AvailableNumberListResponse response = numbersService.searchForAvailableNumbers(parameters); response .iterator() .forEachRemaining( number -> LOGGER.info(String.format("Available number details: %s", number))); } } REST API ```java import java.net.*; import java.net.http.*; import java.util.*; public class App { public static void main(String[] args) throws Exception { var httpClient = HttpClient.newBuilder().build(); var host = "https://numbers.api.sinch.com"; var projectId = "YOUR_projectId"; var pathname = "/v1/projects/" + projectId + "/availableNumbers"; var request = HttpRequest.newBuilder() .GET() .uri(URI.create(host + pathname )) .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(("YOUR_username:YOUR_password").getBytes())) .build(); var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } } ``` ## Client When using the Sinch Java SDK, you initialize communication with the Sinch backend by initializing the Java SDK's main client class. This client allows you to access the functionality of the Sinch Java SDK. ### Initialization To successfully initialize the Sinch client class, you must provide a valid access key ID and access key secret combination. You must also provide your Project ID. For example: ```java package numbers.sdk; import com.sinch.sdk.SinchClient; public class App { public static String access_key = "YOUR_access_key"; public static String access_secret = "YOUR_access_secret"; public static String project_id = "YOUR_project_id" public static void main(String[] args) { SinchClient client = new SinchClient(Configuration.builder() .setKeyId(access_key) .setKeySecret(access_secret) .setProjectId(project_id) .build()); } ``` ## Numbers domain The Sinch Java SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, `client.numbers().v1().[endpoint_category()].[method()]`. In the Sinch Java SDK, Numbers API endpoints are accessible through the client: - [`numbers().v1()`](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/api/v1/package-summary.html) - [`numbers().v1().regions()`](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/api/v1/AvailableRegionsService.html) - [`numbers().v1().eventDestinations()`](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/api/v1/EventDestinationsService.html) - [`numbers().v1().sinchEvents()`](https://www.javadoc.io/doc/com.sinch.sdk/sinch-sdk-java/latest/com/sinch/sdk/domains/numbers/api/v1/SinchEventsService.html) For example: ```java var numbers = client.numbers().v1().list(ActiveNumbersListQueryParameters.builder() .setType(NumberType.LOCAL) .setRegionCode("US") .build()); ``` Requests and queries made using the Java SDK are similar to those made using the Numbers API. Many of the fields are named and structured similarly. For example, consider the representations of a MOBILE number type. One field is represented in JSON, and the other is using our Java SDK: SDK ```java NumberType.MOBILE ``` REST API ```JSON "type": "MOBILE" ``` Many fields in the Java SDK are rendered as enums in data models. When making calls directly to the API, we use JSON objects, including (in some cases) nested JSON objects. When using the Java SDK, we use Java data models instead of nested JSON objects. For example, consider the Numbers configuration objects below. One is represented in JSON, the other as a Java object: ```Java AvailableNumberListQueryParameters.builder() .setType(NumberType.LOCAL) .setRegionCode("US") .build() ``` ```JSON { "type": "LOCAL", "regionCode": "US" } ``` Note that in the Java SDK you would use a `builder()` method to construct the appropriate data model in the correct structure. Response fields match the API responses. They are delivered as Java objects.