Help

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

Topic Labels: Scripting extentions
Solved
Jump to Solution
1570 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Nathan_Beckstea
6 - Interface Innovator
6 - Interface Innovator

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!");
1 Solution

Accepted Solutions
Mariusz_S
7 - App Architect
7 - App Architect

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()
        });
    })
);

See Solution in Thread

2 Replies 2
Mariusz_S
7 - App Architect
7 - App Architect

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!