0
votes

I'm trying to make twilio access token in AWS lambda, but I get the error "callback is not a function". How can I fix it?

const AccessToken = require('twilio').jwt.AccessToken;
    const VoiceGrant = AccessToken.VoiceGrant;

    exports.generateToken = function(identity, callback) {
        // Used when generating any kind of tokens
        const accountSid = 'xxxxxxxxx';
        const apiKey = 'xxxxx';
        const apiSecret = 'xxx';

        // Used specifically for creating Voice tokens
        const pushCredSid = 'xxx';
        const outgoingApplicationSid = 'xxxxx';

        // Create an access token which we will sign and return to the client,
        // containing the grant we just created
        const voiceGrant = new VoiceGrant({
            outgoingApplicationSid: outgoingApplicationSid,
            pushCredentialSid: pushCredSid
        });

        // Create an access token which we will sign and return to the client,
        // containing the grant we just created
        const token = new AccessToken(accountSid, apiKey, apiSecret);
        token.addGrant(voiceGrant);
        token.identity = identity;
        console.log('Token:' + token.toJwt());
        callback(null, token.toJwt());
    };
1
Hey, This line callback(null, token.toJwt());? mh isn't the important part how you call generateToken? - Roland Starke
@RolandStarke This is an important part, since here the result should return, I did two other lambda functions and they work well, but here I am constantly getting the error that I wrote above. - Alexander Khitev
Mh could it be that AWS Lambda requires you to have 3 params? from the docs exports.myHandler = function(event, context, callback) { - Roland Starke
@RolandStarke No, there are not necessarily three parameters - Alexander Khitev
Mh i meant you are required to use 3 params if you wanna use a callback. (but yeah ofc there could be some magic with parameter names. but in general there are no named parameters in javascript so i would give it a try to define your function with 3 params) - Roland Starke

1 Answers

1
votes

As Roland Starke said, it's worth changing this exports.generateToken = function(identity, callback) to exports.generateToken = function(event, context, callback) and everything will work fine.