0

I am developing a custom activity and I want to get the subscriberKey from my Data Extension as an inArgument. I use data binding, but don't have a relationship between my DE and Contact Builder, as the data in my DE is completely fake.

When my activity hits the /execute endpoint and the JWT gets decoded, the body doesn't contain any inArgs.

config.json

{
    "workflowApiVersion": "1.1",
    "metaData": {
        "icon": "images/icon.jpeg",
        "category": "custom"
    },
    "type": "REST",
    "lang": {
        "en-US": {
            "name": "Custom Activity",
            "description": "A custom Journey Builder activity."
        }
    },
    "arguments": {
        "execute": {
            "inArguments": [],
            "outArguments": [],
            "timeout": 10000,
            "retry": 3,
            "url": `https://${process.env.HEROKU_APP_NAME}.herokuapp.com/execute`,
            "body": "",
            "header": "",
            "useJwt": true
        }
    },
    "configurationArguments": {
        "save": {
            "url": `https://${process.env.HEROKU_APP_NAME}.herokuapp.com/save`,
            "body": "",
            "header": "",
            "useJwt": true
        },
        "publish": {
            "url": `https://${process.env.HEROKU_APP_NAME}.herokuapp.com/publish`,
            "body": "",
            "header": "",
            "useJwt": true
        },
        "validate": {
            "url": `https://${process.env.HEROKU_APP_NAME}.herokuapp.com/validate`,
            "body": "",
            "header": "",
            "useJwt": true
        }
    },
    "userInterfaces": {
        "configModal": {
            "height": 200,
            "width": 500,
            "fullscreen": true
        }
    }
}

customActivity.js

...
let payload = {};
let dataExtensionSchema;
let activityConfiguration = {
    subscriberKey: "",
    productId: ""
};

function onRender() {
    connection.trigger('ready');
    connection.trigger('requestSchema');
}

connection.on("initActivity", function (data) {
    if (data) payload = data;
    if (payload["arguments"].execute.inArguments.length > 0) {
        activityConfiguration = inArguments[0];
    }
});

connection.on('requestedSchema', function (eventDefinitionModel) {
    dataExtensionSchema = eventDefinitionModel?.schema;
    connection.trigger('ready');
});

function save() {
    payload["arguments"].execute.inArguments = [{
        subscriberKey: "{{" + dataExtensionSchema[0]?.key + "}}", // "{{Event.DEAudience-7b26660e-68f6-4db1-64f2-c5338b7f76ca.SubscriberKey}}"
        productId: document.getElementById('productId').value
    }];
    payload["metaData"].isConfigured = true;
    connection.trigger('updateActivity', payload);
}
...

activity.js

router.post('/execute', function (req, res) {
    jwt.verify(getToken(req.body), process.env.JWT, async (err, decoded) => {
        if (err) return res.status(401).json(err);

        if (decoded && decoded.inArguments && decoded.inArguments.length > 0) {
            // code logic - program never gets here
        } else {
            res.status(400).end("Something went wrong. Please try again.");
        }
    })
})

function getToken(requestBody) {
    return jwt.sign(requestBody, process.env.JWT);
}
...
0

So, firstly if u debug your final payload after saving your activity, does it show

subscriberKey: "{{Event.DEAudience-7b26660e-68f6-4db1-64f2-c5338b7f76ca.SubscriberKey}}"

Also try verifying your req.body by converting to string utf-8 and check.

If payload is not a buffer or a string, it will be coerced into a string using JSON.stringify.

var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'shhhhh');

And this is how I configured.

const JWT = require(Path.join(__dirname, '..', 'lib', 'jwtDecoder.js'));

JWT(req.body, process.env.My_JWT_Key, async function(err, decoded) {
        if (err) {
            console.log("Invalid JWT::" + req.body);
            return res.status(401).end();
        } else {
            console.log("JWT Authentication Successful:", decoded);
        }
});

JwtDecoder.js

 'use strict';
module.exports = (body, secret, cb) => {
    console.log('inside jwtDecoder');
    if (!body) {
        return cb(new Error('invalid jwt data'));
    }

    require('jsonwebtoken').verify(body.toString('utf8'), secret, { algorithm: 'HS256' }, cb);
};

Try printing your req.body inside execute function. If it sends an encoded JWT try to decode it online at JWT by pasting your secret key (sandbox only). Then you can confirm that your JB execution is working as expected and need to look at logic for signing JWT and decoding.

Try this and let me know in case of further queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.