Skip to content

Sinch Java SDK for Number Lookup API

The Sinch Java SDK allows you to quickly interact with the Number Lookup API from inside your Java applications. When using the Java SDK, the code representing requests and queries sent to and responses received from the Number Lookup API are structured similarly to those that are sent and received using the Number Lookup 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:

This guide describes the syntactical structure of the Java SDK for the Number Lookup API, including any differences that may exist between the API itself and the SDK. For a full reference on Number Lookup API calls and responses, see the Number Lookup API Reference.

This code sample is an example of how to use the Java SDK to lookup a specified number. We've also provided an example that accomplishes the same task using the REST API.

Snippet.java
// This code looks up a specified number. 
/**
 * Sinch Java Snippet
 *
 * <p>This snippet is available at https://github.com/sinch/sinch-sdk-java
 *
 * <p>See https://github.com/sinch/sinch-sdk-java/blob/main/examples/snippets/README.md for details
 */
package numberlookup;

import com.sinch.sdk.SinchClient;
import com.sinch.sdk.domains.numberlookup.api.v2.NumberLookupV2Service;
import com.sinch.sdk.domains.numberlookup.models.v2.request.LookupFeatureType;
import com.sinch.sdk.domains.numberlookup.models.v2.request.NumberLookupRequest;
import com.sinch.sdk.domains.numberlookup.models.v2.response.NumberLookupResponse;
import com.sinch.sdk.models.Configuration;
import java.util.Collections;
import java.util.logging.Logger;
import utils.Settings;

public class Lookup {

  private static final Logger LOGGER = Logger.getLogger(Lookup.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");

    // The phone number you want to lookup in E.164 format
    String phoneNumber = "PHONE_NUMBER";

    Configuration configuration =
        Configuration.builder()
            .setProjectId(projectId)
            .setKeyId(keyId)
            .setKeySecret(keySecret)
            .build();

    SinchClient client = new SinchClient(configuration);

    NumberLookupV2Service numberLookupService = client.lookup().v2();

    NumberLookupRequest request =
        NumberLookupRequest.builder()
            .setNumber(phoneNumber)
            .setFeatures(Collections.singletonList(LookupFeatureType.LINE_TYPE))
            .build();

    LOGGER.info("Lookup for: " + phoneNumber);

    NumberLookupResponse response = numberLookupService.lookup(request);

    LOGGER.info("Response: " + response);
  }
}

This example highlights the following required to successfully make a Number Lookup API call using the Sinch Java SDK:

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 the functionality of the Sinch Java SDK.

Initialization

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.

Number Lookup 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.lookup().v2().[method()].

Endpoint categories

In the Sinch Java SDK, Numbers API endpoints are accessible through the client:

For example:

var number-lookup = client.lookup().v2().lookup(NumberLookupRequest.builder()
                                                .setNumber(phonenumber)
                                                .setFeatures(Collections.singletonList(LookupFeatureType.LINE_TYPE))
                                                .build());

Request and query parameters

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

LookupFeatureType.LINE_TYPE

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

NumberLookupRequest.builder()
                  .setNumber("+12312312312")
                  .setFeatures(Collections.singletonList(
                    LookupFeatureType.LINE_TYPE))
                  .build()

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.

We'd love to hear from you!
Rate this content: