0

I want to authenticate with Twilio's api using the Node.js https module. My code is essentially:

const options = {
    host: 'api.twilio.com',
    path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json',
    auth: {
        user: TWILIO_ACCOUNT,
        pass: TWILIO_API_KEY
    }
};

const req = https.get(options, (res) => { ...

The error I receive is TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. If I remove the auth argument in the options:

const options = {
    host: 'api.twilio.com',
    path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json'
};

const req = https.get(options, (res) => { ...

the error I receive is Authentication Error - No credentials provided. This leads me to believe I am not passing the authentication correctly in the options.

( Using request-promise, this method of passing the authentication works; I was trying to see if I could get it to work using a Node.js built-in module )

2
  • Why don't you use twilio npm? Commented Sep 3, 2018 at 7:11
  • @Shaharyar - as an challenge, to see if I can do this only using built-in modules Commented Sep 3, 2018 at 14:07

1 Answer 1

1

Twilio developer evangelist here.

The auth property in the options object requires that its value is a string (see the options you can use here).

So, to correct your options object, you need to concatenate the Account SID and Auth Token with a colon, like this:

const options = {
  host: 'api.twilio.com',
  path: '/2010-04-01/Accounts/' + TWILIO_ACCOUNT + '/Messages.json',
  auth: `${TWILIO_ACCOUNT}:${TWILIO_API_KEY}`
}

Let me know if that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This works. Would be helpful if it was clearer in the Twilio docs.
The Twilio docs are about the Twilio Node module which we provide to make this easier. This particular detail is only relevant if you are using the Node.js HTTP standard library module and is documented there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.