Skip to main content
Solved

Simple API payload won't load into Airtable. Cryptocurrency data


So I’m looking to create a simple chart of crypto projects I’m interested in tracking. I’m using a variation of @Nikolay_Tsenkov elegant stock price updater he posted on the Example Script Showcase. I can get it to work with a few securities sites, it pulls stocks in beautifully but when using CoinGecko’s API I keep getting an “Undefined” error.

Upon inspection I can see that I’m getting the payload back correctly but no matter how I try to load it, it doesn’t seem to want to go into the table. Is it because the payload is nested within the id? I’m totally stumped on this at this point and could really use some fresh eyes to get me sorted.

Here’s an example of the URL string. the `${coin}’ is obvously for pulling the id in from Airtable, in this example I’m using “cardano”:

GET: https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd

RETURNS:
{ "cardano": { "usd": 1.15 } }

All I want is the “1.15” to come into my price field, but NOOOO… Anybody? Please???

Here’s @Nikolay_Tsenkov’s modified code:

let table = base.getTable("Monitored Projects"),
    records = await table.selectRecordsAsync();

await Promise.all(

    records.records.map(async record => {

        let coin = record.getCellValue("id");

        if (!coin) {

            output.text("Can't update without ID...");

            return;
        }

        output.text(`Updating price of ${coin}...`);

        // make request
        var requestResult = await fetch(
            `https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`).then(r => r.json());

        // print fetched dat
        output.markdown(`${coin}'s price right now is: **${requestResult.usd}**`)

        // update record in table
        return await table.updateRecordAsync(record, {
            "Price": requestResult.usd,
            "Price Date": new Date()
        });
    })
);

output.markdown("# Done!");

Best answer by Mariusz_S

Hey @Nathan_Beckstead,
This should work:

let table = base.getTable("Monitored Projects"),
records = await table.selectRecordsAsync();

await Promise.all(

    records.records.map(async record => {

        let coin = record.getCellValue("id");

        if (!coin) {

            output.text("Can't update without ID...");

            return;
        }

        output.text(`Updating price of ${coin}`);

        // make request
        var requestResult = await fetch(
            `https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`).then(r => r.json());
        // print fetched dat
        const price = ((requestResult[coin] || {}).usd);
        output.markdown(`${coin}'s price right now is: **${price}**`)

        // update record in table
        return await table.updateRecordAsync(record, {
            "Price": price,
            "Price Date": new Date()
        });
    })
);
View original
Did this topic help you find an answer to your question?

2 replies

  • Inspiring
  • 39 replies
  • Answer
  • March 11, 2021

Hey @Nathan_Beckstead,
This should work:

let table = base.getTable("Monitored Projects"),
records = await table.selectRecordsAsync();

await Promise.all(

    records.records.map(async record => {

        let coin = record.getCellValue("id");

        if (!coin) {

            output.text("Can't update without ID...");

            return;
        }

        output.text(`Updating price of ${coin}`);

        // make request
        var requestResult = await fetch(
            `https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`).then(r => r.json());
        // print fetched dat
        const price = ((requestResult[coin] || {}).usd);
        output.markdown(`${coin}'s price right now is: **${price}**`)

        // update record in table
        return await table.updateRecordAsync(record, {
            "Price": price,
            "Price Date": new Date()
        });
    })
);

Mariusz_S wrote:

Hey @Nathan_Beckstead,
This should work:

let table = base.getTable("Monitored Projects"),
records = await table.selectRecordsAsync();

await Promise.all(

    records.records.map(async record => {

        let coin = record.getCellValue("id");

        if (!coin) {

            output.text("Can't update without ID...");

            return;
        }

        output.text(`Updating price of ${coin}`);

        // make request
        var requestResult = await fetch(
            `https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`).then(r => r.json());
        // print fetched dat
        const price = ((requestResult[coin] || {}).usd);
        output.markdown(`${coin}'s price right now is: **${price}**`)

        // update record in table
        return await table.updateRecordAsync(record, {
            "Price": price,
            "Price Date": new Date()
        });
    })
);

You are WONDERFUL!!! Thank you so much for taking the time to help me, I appreciate it very much!


Reply