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:
- All Numbers API prerequisite steps.
- Node.js and a familiarity with how to create a new app.
Steps:
- Set up your Node.js application
- 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:
- Create a folder called
rent-any-app
- Navigate into the folder you created and run the following command.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.
npm init
You can install the Sinch Node.js SDK using either NPM or Yarn:
npm install @sinch/sdk-core
yarn add @sinch/sdk-core
Create your file
Create a new file namedindex.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
Note:
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.
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
PROJECTID="YOUR_project_id"
ACCESSKEY="YOUR_access_key"
ACCESSSECRET="YOUR_access_secret"
app.js
File
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:
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
- Replace the remaining placeholder values for these parameters with your values:
Parameter | Your value |
---|---|
YOUR_region_code | 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. |
YOUR_number_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 . |
YOUR_service_plan_id | Your SMS service plan ID. This is required for SMS configuration. |
- Save the file.
Rent the first available virtual number
Now you can run the code with the following command:
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
- 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 Customer Dashboard.