Before you can get started, you need the following already set up:
Set all SMS API configuration settings.
- NPM and a familiarity with how to install packages.
- Node.js and a familiarity with how to create a new app.
Learn how to quickly send SMS messages in a Node.js application with the Sinch Node.js SDK.
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/sms/
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 at this location in the repo if you want to just replace the file.snippet.js// eslint-disable-next-line no-unused-vars import { Sms, SmsService } from '@sinch/sdk-core'; /** @param {SmsService} smsService */ export const execute = async (smsService) => { const from = 'YOUR_sinch_phone_number'; const recipient = 'YOUR_recipient_phone_number'; const body = 'This is a test SMS message using the Sinch Node.js SDK.'; /** @type {Sms.SendSMSRequestData} */ const requestData= { sendSMSRequestBody: { type: 'mt_text', from, to: [recipient], body, }, }; const response = await smsService.batches.send(requestData); console.log(`Response:\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 YOUR_sinch_phone_number
Any number you've assigned to your Sinch account. Find the number on your Sinch dashboard by clicking the service plan ID link and scrolling to the bottom of the page. YOUR_recipient_phone_number
The phone number to which you want to send the test SMS message. Ensure you save your file.
Now you can execute the code and send your test SMS message.
Run the following command:
node src/app.js
You should receive a text to the phone number you entered and you'll see a response in your terminal or command prompt. You did it!
The code you used in the snippet.js
file sends a POST request to the Sinch API /batches
endpoint to send the SMS message. Click here to learn more about the batches endpoint.
- Explore the API specification to test more endpoints.