Fetch Using Basic Authentication (Twillio Lookup API)

Hi all,

I’m hoping to get some help here. I’m trying to use Fetch (GET) to pull information on a phone number using Twillio Lookup API:
https://www.twilio.com/docs/lookup/api#authentication

I’ve tried everything (spent 5 hours trying different things) and I keep getting the below as a response.


{code: 20003, detail: "Your AccountSid or AuthToken was incorrect.", message: "Authentication Error - No credentials provided", more_info: "https://www.twilio.com/docs/errors/20003", status: 401}

Here’s the code I have to done so far:


import {fetch} from 'wix-fetch';

//this is on the backend...
export async function a007_phoneNumberVerification(phone_number) {
 let data = ""
 let url = 'https://lookups.twilio.com/v1/PhoneNumbers/'+phone_number+'?CountryCode=US';
 let username = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
 let password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
 let headers = {
 'Authorization': username + ":" + password,
  };
 let options = {
      method:'get',
      headers: headers
  }
 await fetch(url, options)
    .then(response => response.json())
    .then(json => data = json);
 return data
}

//this is on the frontend
let value = await a007_phoneNumberVerification(phone_number);
console.log(value)

I have no idea how to get the username and password to push through, but I’m successfully getting a response back from the API with the error message.

Could anyone help out? THANK YOU!!!

See here
https://www.wix.com/corvid/forum/community-discussion/require-doesn-t-work
https://www.twilio.com/docs/usage/your-request-to-twilio

Twilio api expects accountSId and Token instead of your login username and password.

Thanks Ido, but that isn’t the issue. I’m actually able to successfully use the API using Postman, so I know the API key / Auth is correct. It something with my code. Here’s a new version that is still having issues. Any suggestions? Thanks so much!

export async function phoneNumberVerification(phone_number) {
 let data = ""
 let url = 'https://lookups.twilio.com/v1/PhoneNumbers/'+phone_number+'?CountryCode=US';
 let account_sid = "xxxxxx";
 let auth_token = "xxxxxxx";
 let headers = {
 'Authorization': account_sid + ":" + auth_token,
  };
 let options = {
      method:'get',
      headers: headers
  }
 await fetch(url, options)
    .then(response => response.json())
    .then(json => data = json);
 return data
}

I’m also trying the Node.JS way, but I’m pulling up an undefined result. See below.

Backend code:

// Filename: backend/twillio.jsw (web modules need to have a .jsw extension)
import Twillo from 'twilio'; 

const accountSid = 'xxxxxx'; //your accountSid
const authToken = 'xxxxxxxxx'; // your authToken
const client = require('twilio')(accountSid, authToken);

export async function verifyPhone(phone_number_to_verify) {
  client.lookups.phoneNumbers('(510)867-5310')
    .fetch({countryCode: 'US', type: ['carrier']})
    .then(phone_number => console.log(phone_number.carrier));
}

Frontend code:

import {verifyPhone} from 'backend/twillio.jsw';
export async function button76_click(event) {
 let phone_number_to_verify = "'(510)867-5310;
 let value = await verifyPhone(phone_number_to_verify)
    console.log(value)
}

There is an error displaying on the line


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

,but I guess that is to be ignored.

What am I missing :frowning: . Thanks again

First, you have a typo on the first line on your backend file

import Twillo from 'twilio'; //should be Twilio

When you use Node you need to return the response from the backend in the proper way. See below.

//lookup.jsw

import Twilio from 'twilio';

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

export function lookup(phone) {
 return client.lookups.phoneNumbers(phone)
    .fetch({type: ['carrier']})
    .then( function(err, token) {
 if (err) {
 return Promise.resolve(err);
        }
 return Promise.resolve(token);
    })
    .catch( (err) => {
 return err;
    });
}

I pass the phone number from the page like this

import {lookup} from 'backend/lookup.jsw';

export function button1_click(event) {
 let num = $w("#phoneNumber").value; //5108675310
 let phone = '+1' + num;
    lookup(phone)
    .then( (response) => {
        console.log(response);
    });
}

Thanks so much @shan! It worked perfectly - huge help!!!

One note for anyone else looking at the post, the “require” line has an error code, but it will still run.