'Pulling from cryptowatch API

I am trying to pull price data from the API for cryptowatch, when I go to the URL with my API key it works fine, but my program isn't successfully pulling it so I am getting my error of Could not set price feed for cryptowatch:" + cryptowatchMarketId

I'm pretty stuck on where to go from here.

    // Set initial prices
    const cryptowatchApiKey = process.env.CRYPTOWATCH_API_KEY || MM_CONFIG.cryptowatchApiKey;
    const cryptowatchMarkets = await fetch("https://api.cryptowat.ch/markets?apikey=" + cryptowatchApiKey).then(r => r.json());
    const cryptowatchMarketPrices = await fetch("https://api.cryptowat.ch/markets/prices?apikey=" + cryptowatchApiKey).then(r => r.json());
    for (let i in cryptowatchMarketIds) {
        const cryptowatchMarketId = cryptowatchMarketIds[i];
        try {
            const cryptowatchMarket = cryptowatchMarkets.result.find(row => row.id == cryptowatchMarketId);
            const exchange = cryptowatchMarket.exchange;
            const pair = cryptowatchMarket.pair;
            const key = `market:${exchange}:${pair}`;
            PRICE_FEEDS['cryptowatch:'+cryptowatchMarketIds[i]] = cryptowatchMarketPrices.result[key];
        } catch (e) {
            console.error("Could not set price feed for cryptowatch:" + cryptowatchMarketId);
        }
    }

    const subscriptionMsg = {
        "subscribe": {
            "subscriptions": []
        }
    }
    for (let i in cryptowatchMarketIds) {
        const cryptowatchMarketId = cryptowatchMarketIds[i];

        // first get initial price info

        subscriptionMsg.subscribe.subscriptions.push({
            "streamSubscription": {
                "resource": `markets:${cryptowatchMarketId}:book:spread`
            }
        })
    }
    let cryptowatch_ws = new WebSocket("wss://stream.cryptowat.ch/connect?apikey=" + cryptowatchApiKey);
    cryptowatch_ws.on('open', onopen);
    cryptowatch_ws.on('message', onmessage);
    cryptowatch_ws.on('close', onclose);
    cryptowatch_ws.on('error', console.error);

    function onopen() {
        cryptowatch_ws.send(JSON.stringify(subscriptionMsg));
    }
    function onmessage (data) {
        const msg = JSON.parse(data);
        if (!msg.marketUpdate) return;

        const marketId = "cryptowatch:" + msg.marketUpdate.market.marketId;
        let ask = msg.marketUpdate.orderBookSpreadUpdate.ask.priceStr;
        let bid = msg.marketUpdate.orderBookSpreadUpdate.bid.priceStr;
        let price = ask / 2 + bid / 2;
        PRICE_FEEDS[marketId] = price;
    }
    function onclose () {
        setTimeout(cryptowatchWsSetup, 5000, cryptowatchMarketIds);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source