Skip to content
Last updated

The Sinch Java SDK allows you to quickly interact with the Numbers API from inside your Java applications. When using the Java SDK, the code representing requests and queries sent to and responses received from the Numbers API are structured similarly to those that are sent and received using the Numbers API.

The fastest way to get started with the SDK is to check out our 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.

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.

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.

Snippet.java
// 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)));
  }
}

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:


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()
  • numbers().v1().regions()
  • numbers().v1().callbackConfiguration()
  • numbers().v1().webhooks()

For example:

var numbers = client.numbers().v1().list(AvailableNumberListRequest.builder()
                                                                .setType(NumberType.LOCAL)
                                                                .setRegionCode("US")
                                                                .build());

The field mappings are described in the sections below.

numbers().v1() endpoint category

The numbers().v1() category of the Java SDK has methods that correspond to the available and active endpoints. The mapping between the API operations and corresponding Java methods are described below, as well as links to the JavaDocs page:

numbers().v1().regions() endpoint category

The numbers().v1().regions() category of the Java SDK corresponds to the availableRegions endpoint. The mapping between the API operation and corresponding Java method are described below, as well as links to the JavaDocs page:

API operationSDK methodJavaDocs
List available regionslist()list

numbers().v1().callbackConfiguration() endpoint category

The numbers().v1().callbackConfiguration() category of the Java SDK corresponds to the callbackConfiguration endpoint. The mapping between the API operations and corresponding Java methods are described below, as well as links to the JavaDocs page:

API operationSDK methodJavaDocs
Get callbacks configurationget()get
Update callback configurationupdate()update

numbers().v1().webhooks() endpoint category

The numbers().v1().webhooks() category of the Java SDK corresponds to the Event notification operation. The mapping between the API operation and corresponding Java method are described below, as well as links to the JavaDocs page:

API operationSDK methodJavaDocs
Event notificationparseEvent()parseEvent

Request and query parameters

Requests and queries made using the Java SDK are similar to those made using the SMS API. Many of the fields are named and structured similarly. For example, consider the representations of an SMS API message type. One field is represented in JSON, and the other is using our Java SDK:

NumberType.MOBILE

Many fields in the Java SDK are rendered as enums in data models.

Nested objects

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 SMS configuration objects below. One is represented in JSON, the other as a Java object:

AvailableNumberListRequest.builder()
                        .setType(NumberType.LOCAL)
                        .setRegionCode("US")
                        .build()
{
  "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.

Responses

Response fields match the API responses. They are delivered as Java objects.