Parameterized message tutorial continued
Create your file to send a message
Create a new file named index.mjs
in the project and paste the provided "Send a parameterized SMS message" code into the file.
Send a parameterized SMS message
// Find your Service Plan ID and API Token at dashboard.sinch.com/sms/api/rest
// Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
const SERVICE_PLAN_ID = 'YOUR_service_plan_ID';
const API_TOKEN = 'YOUR_API_token';
const SINCH_NUMBER = 'YOUR_Sinch_virtual_number';
const TO_NUMBER = '14051234567';
//Use node-fetch
import fetch from 'node-fetch';
//SMS API functionality
async function run() {
const resp = await fetch(
'https://us.sms.api.sinch.com/xms/v1/' + SERVICE_PLAN_ID + '/batches',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + API_TOKEN
},
body: JSON.stringify({
from: SINCH_NUMBER,
to: [TO_NUMBER],
body: 'Hey ${firstName}! Are ${team} signed up for the Tulsa Trivia Bar Crawl 22\'? https://tulsatriviacrawl.com/signup. Drink vouchers for the first 25 paid participants!',
//Temporary parameters right in the sample for testing.
parameters: {
firstName: {
'14051234567': 'Aaron',
default: 'Buddy'
},
team: {
'14051234567': 'The Cyber Nerds',
default: 'you'
}
}
})
}
);
const data = await resp.json();
console.log(data);
}
run();
Fill in your parameters
Any parameters below with curly brases can be switched out for parameters from your own database. For example, we are using team
, you may have something like fav_charity
, last_purchase
or address
. The sky is the limit!
- Assign your values to the following parameters:
Parameter | Your value |
---|---|
SERVICE_PLAN_ID | The service plan ID found on your Sinch Customer Dashboard. SMS > APIs > REST configuration |
API_TOKEN | The API token found on your Sinch Customer dashboard. SMS > APIs > REST configuration > Click Show to reveal your API token. |
SINCH_NUMBER | A free test number or any Sinch virtual number you've rented. Find the number on your Sinch Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page. |
TO_NUMBER | The phone number to which you want to send the test SMS message. |
parameter_key | The parameters from your database. We have used firstName and team in our sample. |
msisdn | In the API reference, you'll see msisdn . This is the phone number you are sending a message to. For testing, use your own phone number. In our sample, it's 14051234567 . |
- Save the file.
Send your customized SMS message
Now you can execute the code and send your test SMS message.
Run the following command:
node index.mjs
If you used your own phone number as the msisdn
, you should have recieved an SMS message!
Next steps
- Find other useful code samples in our API reference.
- Check out another tutorial.