Home > Mobile >  Is it possible to use the Twilio SMS API authenticated with API Key?
Is it possible to use the Twilio SMS API authenticated with API Key?

Time:10-25

The SMS API example code works by create a client session using an account SID and an auth token:

const client = require('twilio')(accountSid, authToken);

However in the example code shown here a client is being initialised using accountSid, API Key and API Secret:

const client = require('twilio')(twilioApiKey, twilioApiSecret, {
    accountSid: twilioAccountSid });

but using this with SMS-service credentials fails to authenticate.

Is the SMS API only authenticate-able using an accountSid with an auth token?

CodePudding user response:

I think your second code sample should work. Please make sure the ApiKey, ApiSecret and AccountSid are set and match the information on your account.

Try extending your code:

const accountSid = "...";
const apiKey = "...";
const apiSecret = "...";
const client = require('twilio')(apiKey, apiSecret, { accountSid: accountSid });


client.messages
  .create({
    body: 'Hello from Node',
    to: '<put your phone number here>', // Text this number
    from: '<put the account phone number here>', // From a valid Twilio number
  })
  .then((message) => console.log(message.sid));

This should log the message ID if successful.

  • Related