# Sinch Java SDK for Number Lookup API

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/number-lookup-api-v2/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](/docs/number-lookup-api-v2/api-reference/number-lookup-v2).

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.

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

REST API
```java

package app;

import java.net.*;
import java.net.http.*;
import java.util.*;

public class App {
  private static final String key = "";
  private static final String secret = "";
  private static final String projectId = "";
  private static final String number = "";

  
  public static void main(String[] args) throws Exception {
    var httpClient = HttpClient.newBuilder().build();

    var payload = String.join("\n"
      , "{"
      , " \"number\": \"" + number + "\","
      , "}"
    );

    var host = "https://lookup.api.sinch.com/v2/projects";
    var pathname = "/" + projectId + "/lookups";
    var request = HttpRequest.newBuilder()
      .POST(HttpRequest.BodyPublishers.ofString(payload))
      .uri(URI.create(host + pathname ))
      .header("Content-Type", "application/json")
      .header("Authorization", "Basic " + Base64.getEncoder().encodeToString((key + ":" + secret).getBytes()))
      .build();

    var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response.body());
  }
}
```

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

- [Client initialization](#client)
- [Numbers domain access](#numbers-domain)
- [Endpoint usage](#endpoint-categories)
- [Field population](#request-and-query-parameters)


## 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](https://dashboard.sinch.com).

```java
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());
    }
}
```

```java
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()]`.

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

- [`lookup().v2()`![external](/assets/external-blue.5a59beb5e5a442f78b530509ddf6b5d0fa705285ff8181e59adf96b5c2c73e25.33310b29.svg)](https://developers.sinch.com/sdk/sinch-sdk-java/2.1.2/com/sinch/sdk/domains/numberlookup/api/v2/package-summary.html)


For example:

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

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:

SDK
```java
LookupFeatureType.LINE_TYPE
```

REST API
```JSON
"features": ["LineType"]
```

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

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

REST API
```JSON
{
  "number": "+12312312312",
  "features": ["LineType"]
}
```

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.