Rent the first available number using the Node.js SDK

Use this guide to set up your Node.js application for use with the Numbers API and rent the first available Sinch virtual number and assign it to your SMS service plan.

Note:

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

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

Set up your Node.js application

First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding.

To create the project, do the following steps:

  1. Create a folder called rent-any-app
  2. Navigate into the folder you created and run the following command.
    Copy
    Copied
    npm init
    This command adds the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.

You can install the Sinch Node.js SDK using either NPM or Yarn:

NPMYarn
Copy
Copied
npm install @sinch/sdk-core
Copy
Copied
yarn add @sinch/sdk-core

Create your file

Create a new file named index.js in the project and paste the provided code into the file.

Modify your application

The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

Before initializing a client using this SDK, you'll need three pieces of information:

  • Your Project ID
  • An access key ID
  • An access key Secret
These values can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret"
});
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, as in the following example:

.env File

Copy
Copied
PROJECTID="YOUR_project_id"
ACCESSKEY="YOUR_access_key"
ACCESSSECRET="YOUR_access_secret" 

app.js File

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: process.env.PROJECTID,
    keyId: process.env.ACCESSKEY,
    keySecret: process.env.ACCESSSECRET
});
Note:

If you are using the Node.js SDK for multiple products that use different sets of authentication credentials, you can include all of the relevant credentials in the same configuration object, as in the following example:

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret",
    applicationKey: "YOUR_application_key",
    applicationSecret: "YOUR_application_secret"
});

Fill in remaining parameters

  1. Replace the remaining placeholder values for these parameters with your values:
ParameterYour value
YOUR_region_codeThe 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.
YOUR_number_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.
YOUR_service_plan_idYour SMS service plan ID. This is required for SMS configuration.
  1. Save the file.

Rent the first available virtual number

Now you can run the code with the following command:

Copy
Copied
node index.js

This code will rent the first available number that fits the search criteria you specified and assign (or provision) it to your SMS service plan ID.

Next steps

Send a message to yourself using the SMS API to verify that the configuration was successful.

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.