Can't access the data from my API queries within the JavaScript query

Hi everyone, I'm currently trying to create a Retool app that fetches real-time data from multiple sources. I'm facing an issue with referencing my API queries within a JavaScript query.

Here's a quick rundown of what I've done so far:

  1. I've set up three API queries (BTCZAR, BTCUSD, and USDZAR) to fetch BTC/ZAR price from the VALR exchange, BTC/USD price from Kraken, and USD/ZAR exchange rate from an exchange rate API. These queries seem to run fine individually, returning the expected data.
  2. I've created a JavaScript query, "calculateArbitrage", which is supposed to extract the necessary data from the API responses, perform arbitrage calculations, and store the data in temporary state variables (btc_zar, btc_usd, usd_zar).

Here's the code I've been trying to use:

const btc_zar_value = BTCZAR.data.lastTradedPrice;
const btc_usd_value = BTCUSD.data.result.XXBTZUSD.c[0];
const usd_zar_value = USDZAR.data.rates.ZAR;

btc_zar.setValue(btc_zar_value);
btc_usd.setValue(btc_usd_value);
usd_zar.setValue(usd_zar_value);

const zar_usd = 1 / usd_zar_value;
const btc_usd_converted = btc_zar_value * zar_usd;
const arbitrage_percentage = ((btc_usd_value - btc_usd_converted) / btc_usd_converted) * 100;

return {
'Opportunity': arbitrage_percentage > 0 ? 'Buy on VALR, Sell on Kraken' : 'Buy on Kraken, Sell on VALR',
'Potential Profit (%)': Math.abs(arbitrage_percentage)
};

However, when I try to run the "calculateArbitrage" query, I'm getting an error message saying that BTCZAR is not defined. It seems like I'm not able to access the data from my API queries within the JavaScript query.

Any help on how to correctly reference my API queries within the JavaScript query would be greatly appreciated. Thank you!

Welcome to the forum @oswald

Are you sure you are using a JS query and not a JS transformer? The rules on how you reference Retool's state are different with each. (Tip for writing transformers - Retool scope - #3 by victoria, Transform data with JavaScript)

If your REST query is named exactly BTCZAR then the js query should be seeing it. Also check with the State viewer to make sure .data.lastTradedPrice actually exists prior to triggering your query, it may not actually be there or have a different spelling.