2

I'm following the NodeJS Twilio SDK docs with the following:

const twilio = require('twilio')

exports.sendActivationCode = async (phone, activationCode) => {
  const accountSID = '<REDACTED>'
  const authToken = '<REDACTED>'
  const client = twilio(accountSID, authToken)
  return await client.messages.create({
    body: `Your activation code is ${activationCode}`,
    from: '+1<REDACTED>',
    to: `+1${phone.toString().replace(/\D/g, '')}`
  })
}

I've checked this numerous times to ensure I have a direct match with the docs. When I try to run the code I get Error: Headers User-Agent forbidden. The request itself shows the header is going out: 'User-Agent': 'twilio-node/3.45.0 (node.js v10.15.3)' and the stack output indicates that this is coming from jsdom -> xhr-utils.

Fluidbyte
  • 5,162
  • 9
  • 47
  • 76
  • I have confirmed your code is correct. Are you sending this from a trial or Dev account? There are some restrictions on sending messages from a trial account. The number has to be a confirmed number. If you have an upgraded account this indeed works. I have sent multiple messages with just a copy and past. – AhDev Jun 08 '20 at 15:26
  • As far as I can tell everything on the account is fine. We have an account that is actively being billed, I have a phone number, and have tried both the account credentials and test credentials to no avail. I can make requests and get the message on my phone through cURL. – Fluidbyte Jun 08 '20 at 16:03
  • The above code works fine for me as well. – Alan Jun 08 '20 at 19:57
  • Where does this code live and where are you calling it from. – AhDev Jun 09 '20 at 15:29

1 Answers1

1

This is what fixed it for me. Just add the following to your package.json file.

    "jest": {
      "testEnvironment": "node"
    },

Found here: https://medium.com/@kevinsimper/how-to-disable-jsdom-in-jest-make-jest-run-twice-as-fast-a01193f23405

T1ASH
  • 11
  • 1
  • This saved me an incredible amount of time and should be write large on the twilio web site!!!! I'd give you a thousand points if I could! – Robert Moskal Dec 08 '22 at 22:22