Skip to content

Look up a number with Node.js

You can quickly see how the Number Lookup API works by looking up a number using the API.

In this guide you will learn how to:

Learn how to:
  1. Set up your Node.js application
  2. Look up your phone number

What you need to know before you start

Before you can get started, you need the following already set up:

Set up your Node.js application

To quickly get started setting up a simple client application using the Node SDK:

  1. If you haven't already, clone the sinch-sdk-node repository.

  2. Navigate to the examples/snippets/ folder.

  3. Open a command prompt or terminal and run the following command to install the necessary dependencies:

    npm install
  4. Rename the .env.example file to .env. Using the access key credentials from your Sinch Build Dashboard, populate the following fields with your values:

    FieldDescription
    SINCH_PROJECT_IDThe unique ID of your Project.
    SINCH_KEY_IDThe unique ID of your access key.
    SINCH_KEY_SECRETThe secret that goes with your access key.
    Note: For security reasons, this secret is only visible right after access key creation.
Note:

This tutorial uses basic authentication for testing purposes. We recommend using OAuth for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use OAuth authentication instead.

  1. Save the file.

  2. Navigate to examples/snippets/number-lookup/lookup.js.

numbersSample.js
// Use this code to look up a number. 
/**
 * Class to lookup a number through the NumberLookup API using the Sinch Node.js SDK.
 */
export class NumberLookupSample {
  /**
   * @param { import('@sinch/sdk-core').NumberLookupService } numberLookupService - the NumberLookupService instance from the Sinch SDK containing the API methods.
   */
  constructor(numberLookupService) {
    this.numberLookupService = numberLookupService;
  }

  async start() {
    // The phone number to lookup in E.164 format
    const phoneNumber = 'PHONE_NUMBER_TO_LOOKUP';

    const response = await this.numberLookupService.lookup({
      numberLookupRequestBody: {
        number: phoneNumber,
      },
    });

    console.log('Response:', response);
  }
}
  1. Assign your values to the following parameters:

    ParameterYour value
    NUMBERThe phone number that you want to look up in E.164 format.
  2. Save the file.

Look up your phone number

Now you can execute the code and look up your phone number. Open a terminal or command prompt to the examples/snippets/ folder and run the following command:

npm run number-lookup:lookup

You should receive a response in your console with details about the phone number you specified.

Troubleshooting tip

If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.

Additional resources

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