Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
My simple "send message" javascript program for Twilio is looping twice, but its only sending a message to the same number, What's missing?

I'm trying to create a simple Javascript program that will send a single message to an array of different numbers. I found a twilio page that got me almost there but it doesn't tell me how to loop. I'm admittedly a programming nubie and found a line of loop code. The program did loop, but it called the same number in the array twice instead of calling the two numbers in the array. Here is the code I was using:

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const twilio = require('twilio')(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers,
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}

Is there another function I need to add in to make it send the message to the second number the second time?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've never used twilio but it looks like you give the array of numbers in the .create function instead of one number,

try switching "numbers" to "numbers[i]":

for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers[i],
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!