Use this guide to create a Node.js application for use with the Numbers API and search for an available Sinch virtual number.
Before you can get started, you need the following already set up:
All Numbers API prerequisite steps.
- NPM and a familiarity with how to install packages.
- Node.js and a familiarity with how to create a new app.
- Set up your Node.js application.
- Search for an available virtual number for SMS, Voice or both.
To quickly get started setting up a simple client application using the Node SDK:
If you haven't already, clone the sinch-sdk-node-quickstart repository.
Navigate to the
templates/client
folder.Open a command prompt or terminal and run the following command to install the necessary dependencies:
npm install
Open the
.env
file. Using the access key credentials from your Sinch Build Dashboard, populate the following fields with your values:Field Description SINCH_PROJECT_ID The unique ID of your Project. SINCH_KEY_ID The unique ID of your access key. SINCH_KEY_SECRET The secret that goes with your access key.
Note: For security reasons, this secret is only visible right after access key creation.NoteIf you're sending SMS messages, ensure you set your region in the
SMS_REGION
field.Save the file.
- Navigate to the
/templates/client/src/numbers/
folder and open thesnippet.js
file. Replace the existing content within that file with thesnippet.js
code provided on this page. That code is also found here if you want to just replace the file.
//This code lists the available numbers for a given set of search criteria.
// eslint-disable-next-line no-unused-vars
import { Numbers, NumbersService } from '@sinch/sdk-core';
/** @param {NumbersService} numbersService */
export const execute = async (numbersService) => {
/** @type {Numbers.ListAvailableNumbersRequestData} */
const requestData = {
regionCode: 'US',
type: 'LOCAL',
capabilities: ['SMS', 'VOICE'],
};
const response = await numbersService.availableNumber.list(requestData);
console.log(`Available numbers to rent:\n${JSON.stringify(response, null, 2)}`);
};
- The code provided in snippet.js includes default parameters. If you want, you can replace the following values for these parameters with your own values:
Parameter | Your value |
---|---|
regionCode | The two letter abbreviation of the country for which you'd like a number. For example, the United States is US . Should be in ISO 3166-1 alpha-2 format. |
type | The 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 . |
- Save the file.
Now you can execute the code. Run the following command:
node src/app.js
You should receive a response in the console with a list of numbers available to rent.
Copy the phoneNumber
you would like to use and rent your virtual number using the Numbers API.
- Explore the API specification to test more endpoints.
- Prefer a UI to search for a number? Follow the entire number searching and renting process in the Sinch Build Dashboard.