Receive an SMS Message with Node.js

Note:

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

Quickly set up a node.js server to receive SMS messages with the Sinch SMS API.

Steps:
  1. Set up your Node.js application.
  2. Configure your Callback URL.
  3. Test the application.

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:

  1. Create a folder called receive-sms-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.

  3. Add the axios package with npm to generate the necessary dependencies. In this application, we are going to use Express to handle incoming requests.
    Copy
    Copied
    npm install axios
    npm install express

Create your file

Create a new file named index.js in the project and paste the "Receive an SMS message" code into the file.

Fill in your parameters

Change the following parameters in the code to your values.

ParameterYour value
SERVICE_PLAN_IDThe service plan ID found on your Sinch Customer Dashboard. SMS > APIs > REST configuration
API_TOKENThe API token found on your Sinch Customer Dashboard. SMS > APIs > REST configuration > Click Show to reveal your API token.

Double check that the region is correct on your base URL. Learn more about regional options here.

Start your web server and set up a tunnel

We use webhooks to notify your application when someone sends a text to your Sinch number. To handle these, you will learn how to create a webserver and make it reachable on the Internet.

  1. Start the server by executing the following command:
    Copy
    Copied
    node index.js
  2. Now you need to open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already, install it with the following command:
    Copy
    Copied
    npm install ngrok -g
  3. Open a terminal or command prompt and enter:
    Copy
    Copied
    ngrok http 3000

    You will see a screen like the following. ngrok screenshot

  4. On the highlighed "Forwarding" line, copy the address ending in .ngrok.io.

Configure your Callback URL

Next, configure a Callback URL for your Sinch account.

  1. On your Sinch Customer Dashboard-> Service APIs click on the service plan ID link.
  2. In “Callback URL” click Add Callback URL and paste in the HTTPS address referred to in the previous step.

Send an SMS message to your sinch number

To test your application, send an SMS message to your Sinch number from your mobile phone to get an automatic reply.

Additional resources

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