JavaScript Code in Zapier to Get Social Media Influence Ratings

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?

you’re missing error handling on that second fetch too. If the first API call fails or doesn’t return a user_id, your code’s gonna crash when it tries to call .then() on undefined. throw in a check after the second fetch - something like if(!response) throw new Error(‘no response’) before you parse the JSON.

The syntax error’s probably from how you’re defining the twitter_handle variable. In Zapier code steps, you can’t just assume variables exist - you need to reference input data through the inputData object.

Change your fetch call’s first line to:

fetch("https://api.socialmetrics.com/v2/lookup.json/twitter?username="+inputData.twitter_handle+"&apikey="+my_api_key)

I hit this same issue building API integrations in Zapier. Input data doesn’t become global variables - you have to grab them through inputData. Also double-check that your input field is named exactly twitter_handle in the Zapier interface. If it’s not, you’ll get undefined values that mess up your API calls.

I’ve hit this same Zapier JavaScript issue before. Your catch block is wrong - you’re passing callback directly instead of calling it. Change .catch(callback) to .catch(function(error) { callback(error) }). Also, your second .then() will break when user_id doesn’t exist because you’re not returning anything from the first fetch. The promise chain needs something to work with. Either return a value or throw an error there. Took me hours to debug this exact problem in my automations.