I want to build a JavaScript action in Zapier that pulls influence scores from an API for Twitter usernames. The process has two steps.
First, I need to get the user ID from a username:
https://api.socialmetrics.com/v2/lookup.json/twitter?username=" + twitter_handle + "&apikey=" + my_api_key
This returns JSON like:
{"user_id":"12345678901234567","platform":"tw"}
Then I fetch the actual score using that ID:
https://api.socialmetrics.com/v2/profile.json/" + user_data.user_id + "/rating?apikey=" + my_api_key
Which gives me:
{"rating":72.45123456789012,"changes":{"daily":0.12345678901234,"weekly":0.98765432109876,"monthly":2.13579246813579},"tier":"70-79"}
I need to extract the rating value from this response. Here’s my current JavaScript code:
var my_api_key = '<api_key_here>';
fetch("https://api.socialmetrics.com/v2/lookup.json/twitter?username="+twitter_handle+"&apikey="+my_api_key)
.then(function(response) {
return response.json();
})
.then(function(user_data) {
console.log(user_data);
if(user_data.user_id) {
return fetch("https://api.socialmetrics.com/v2/profile.json/"+user_data.user_id+"/rating?apikey="+my_api_key)
}
}).then(function(response) {
return response.json();
}).then(function(result) {
callback(null, result.rating)
}).catch(callback);
In Zapier’s input section I’m passing:
twitter_handle: [username_value]
But I keep getting this error:
SyntaxError: Invalid or unexpected token
What am I doing wrong with my JavaScript syntax?