Yazzz
Yazzz

Reputation: 11

Twillio accountsid and token issues

I am having trouble getting my token and account sid to work. i set them as constants in the file and the following error appears

Error:
throw 'Client requires an Account SID and Auth Token set explicitly ' + ^ Client requires an Account SID and Auth Token set explicitly or via the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables

const sid = xxxxxxxxxxx'
const tkn = 'xxxxxxxxxxx'

function Client(sid, tkn, host, api_version, timeout) {
    //Required client config

    if (!sid || !tkn) {
        if (process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN) {
            this.accountSid = process.env.TWILIO_ACCOUNT_SID;
            this.authToken = process.env.TWILIO_AUTH_TOKEN;
        }
        else {
            throw 'Client requires an Account SID and Auth Token set explicitly ' +
                'or via the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables';
        }
    }
    else {
        //if auth token/SID passed in manually, trim spaces
        this.accountSid = sid.replace(/ /g, '');
        this.authToken = tkn.replace(/ /g, '');
    }

    //Optional client config
    this.host = host || defaultHost;
    this.apiVersion = api_version || defaultApiVersion;
    this.timeout = timeout || 31000; // request timeout in milliseconds
}

Upvotes: 0

Views: 828

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

You are setting sid and tkn as constants outside the Client function, however you also name the first two arguments passed into the function as sid and tkn. Within the scope of Client that means that sid and tkn are whatever you pass as arguments to the function.

My guess is that you are calling the Client function without any arguments and that leads to this error.

Rather than setting the sid and tkn in the file with the Client function, I recommend you pass them in as arguments when you call it.

const client = new Client(sid, tkn);

Let me know if that helps at all.

Upvotes: 1

Related Questions