I wanted to take a break from my CRM series and go through a good scripting example. My next CRM series post will utilize a more complex scripting example than I am doing here, so this is a good one to get started and is an interesting use case.
As Airtable puts it…
Scripting lets you write short scripts to reduce time spent on repetitive tasks and uncover deeper insights with advanced queries and custom reporting.Learn more on Airtable’s Developer Page
To get started, you can see some example scripts, install scripts from the marketplace, collaborate with others in the community, or keep reading this post for a primer.
Our goal is to have Airtable get current quotes for crypto. To get started, create a table called Coins. My table looks like below. If you want the script to work for you, please use the same field names.
A few nuances to this setup.
- Name & Ticker are Single Line Text fields
- Market is a formula field of
Ticker & "-USD". This is the market we will use in the API to obtain the price against the US Dollar. Here’s an example URL for Bitcoin.
- Latest Price is a currency field using the symbol $ and having a precision of 7 decimals (the max)
- Latest Price (As Of) will be a timestamp from when we obtain the pricing. This is a DAte field with the Time value turned on.
Next, we will create a custom automation so that we can schedule code to run.
I am going to name the automation Get Coin Prices and the trigger will be “at a scheduled time”. In this sample base, I am setting it to daily, but you can make it happen more often like every few hours.
Our Action will be to Run a script. As long as you setup your table like I showed you up above, you should be able to paste this code into your script. In this code, we are querying the Coins data, looping through each one, and using the Coinbase Pro API (which is free and doesn’t require authentication for price data) to get the market prices.
You can run a Test within the code editor. When you do, it should look like this.
As you can see, our table has been populated when we ran our test.
As you can see, we now have a price populated on each record and a timestamp for when we last obtained the price.
As you probably noticed in the image above, we did not get a price for Avalanche (AVAX). Why not? Well, if you go into the Automation, click on the script node and then work your way to the execution logs, you can find out.
The benefit of having some
console.log lines in your script is that you can review your execution logs and debug your script. As you can see, Avalanche is returning a 404 response rather than a 200. We are using the Coinbase Pro API. As it turns out, Coinbase does not support the AVAX coin. Thus, they are not able to obtain a quote for it.
I hope this was a good example for you. All in all, the script was pretty simple, but it needs to get tied into a framework for automating it to run. That is where the scheduled automations from Airtable come into play. Cool stuff.