1

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());
    };
7
  • Hey, This line callback(null, token.toJwt());? mh isn't the important part how you call generateToken? Commented Mar 30, 2018 at 17:06
  • @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. Commented Mar 30, 2018 at 17:08
  • Mh could it be that AWS Lambda requires you to have 3 params? from the docs exports.myHandler = function(event, context, callback) { Commented Mar 30, 2018 at 17:10
  • @RolandStarke No, there are not necessarily three parameters Commented Mar 30, 2018 at 17:12
  • 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) Commented Mar 30, 2018 at 17:13

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.