Search for a virtual number using Node.js

Use this guide to setup your Node.js application for use with the Numbers API and search for an available Sinch virtual number.

Note:

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

Steps:
  1. Set up your Node.js application.
  2. Search for an available virtual number for SMS, Voice or both.

Set up your Node.js application

  1. Once your free Sinch account is setup and you have a virtual number, create a new node app with npm.
    Copy
    Copied
    npm init
  2. Accept the defaults for the application.
  3. Add the fetch package with npm to generate the necessary dependencies.
    Copy
    Copied
    npm install node-fetch

Search for an available virtual number

Create your file

Create a new file named index.mjs in the project and paste the "Search for a virtual number" code.

Search for a virtual number

import fetch from 'node-fetch';

async function run() {
  const query = new URLSearchParams({
    regionCode: 'US',
    type: 'LOCAL'
  }).toString();

  const projectId = 'YOUR_projectId_PARAMETER';
  const resp = await fetch(
    `https://numbers.api.sinch.com/v1/projects/${projectId}/availableNumbers?${query}`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64')
      }
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();

Fill in your parameters

ParameterYour value
YOUR_ProjectIDThe project ID found in the Sinch Customer Dashboard.
YOUR_usernameyour client_id or key_id found in the Customer Dashboard.
YOUR_passwordYour client_secret or key_secret. This is generated upon new key creation. Generate a new key if you have lost your secret key.
regionCodeThe two letter abbreviation of the country for which you'd like a number. ISO 3166–1 alpha–2.
typeThe type of number you would like to rent. Available options are: MOBILE, LOCAL, or TOLL_FREE. Note that 10DLC numbers should be set to LOCAL.
  1. Save the file.

Run the file

Run the following command in your terminal or command prompt:

Copy
Copied
node index.mjs

Response

The response should look something like this (repeated many times over!):

Copy
Copied
{
  "availableNumbers": [
    {
      "phoneNumber": "+12089087284",
      "regionCode": "US",
      "type": "LOCAL",
      "capability": ["SMS"],
      "setupPrice": {
        "currencyCode": "USD",
        "amount": "0.00"
      },
      "monthlyPrice": {
        "currencyCode": "USD",
        "amount": "2.00"
      },
      "paymentIntervalMonths": 1
    }
  ]
}

Next steps

Copy the phoneNumber you would like to use and rent your virtual number using the Numbers API.

Additional resources

Was this page helpful?

Search for a virtual number

import fetch from 'node-fetch';

async function run() {
  const query = new URLSearchParams({
    regionCode: 'US',
    type: 'LOCAL'
  }).toString();

  const projectId = 'YOUR_projectId_PARAMETER';
  const resp = await fetch(
    `https://numbers.api.sinch.com/v1/projects/${projectId}/availableNumbers?${query}`,
    {
      method: 'GET',
      headers: {
        Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64')
      }
    }
  );

  const data = await resp.text();
  console.log(data);
}

run();