How-To

How to Check Any Cryptocurrency Price From Your Linux Terminal

1

If you’re looking to get the latest cryptocurrency prices from your Linux or Mac terminal, then by following this simple tutorial, you can be up and running in less than five minutes.

This simple project uses an application programming interface (API) to pull the latest crypto prices. For this tutorial, you’ll use the API provided by CoinMarketCap. While free to use, you need to register on the site first.

While this project doesn’t assume you’re a rockstar coder, it is a technical tutorial. I will guide you every step of the way, but I recommend you are at least interested in the technical side of things.

1. Register with the CoinMarketCap Developer Portal

As mentioned above, you need a free account to access CoinMarketCap’s data. While CoinMarketCap does charge for API access, the free tier provides basic information and 10,000 requests per month. To max this out, you’d need to access the price every five minutes for an entire month!

The paid-for tiers are for websites or other services that make money. If you want to make your own application using this API, you may want to pay for an account later on, but for now, the free tier is more than good enough for most hobby uses.

CoinMarketCap API Registration

Start by visiting the CoinMarketCap API signup page and filling in your information. You only need to provide your name, email address, and create a password. Make sure you select the Basic plan, which is free.

Once registered, confirm your email address by clicking the button in the email you should have received from CoinMarketCap. You’ll then have access to the CoinMarketCap API dashboard, which shows you statistics about your account. You can see your API key, most recent requests, and the total remaining credits, which get used up when making requests.

Copy Your Access Key

Your access key is like a password. With this key, CoinMarketCap knows who you are. They use this to limit how many times you can access the API. This helps protect CoinMarketCap’s resources from abuse and is an industry-standard way of securing an API. Suffice to say, plenty of people are often looking to steal API keys to use for their own purposes. Treat your API key like a password.

Never share or store it unencrypted such as in an email or public website. Don’t expose it to website visitors or share it on Instagram. Don’t worry though, it’s easy to change or disable if you have a problem.

CoinMarketCap API Dashboard

From your API Dashboard, hover over the API Key section on the top left and choose Copy Key to copy your unique API key to your clipboard. Notice the “Regenerate Key” and “Disable Key” buttons also live here, should you need them later on. Store this on your computer, as you’ll need it for the next step. Or, come copy it later on now you know where to find it.

2. Basic Command-Line Usage

Now that you have an account and an API key, open up your terminal. Here’s the code you need to get started with the Bitcoin price but don’t run it yet:

curl -H "X-CMC_PRO_API_KEY: "YOUR_KEY_HERE" -H "Accept: application/json" -d "symbol=BTC" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest

So, to run this, replace “YOUR_KEY_HERE” with your API key from the previous step. Paste this into your terminal and press return. You’ll see a lot of data come back, including the current Bitcoin price, number of trading pairs, and much more. Let’s explore that command.

The word “curl” (spelled cURL) stands for “client URL”. It’s a software tool to perform web-based commands, a bit like a virtual web browser. It’s a very popular tool for this kind of task.

CoinMarketCap API Response

Everything that follows “curl” are the options for your command. These tell cURL what API to use, what API key to use, and various other options.

The -H option configures the request headers. Headers are a secure way of sending extra data with the request, sent as key-value pairs. In this instance, the header key is “CMC_PRO_API_KEY“, and the value of this is “YOUR_KEY_HERE“, or your real API key. This is how CoinMarketCap knows who you are. It’s the preferred way of sharing your API key.

Next is another header, configured with the -H flag. This looks like “Accept: application/json“. It tells CoinMarketCap how to return the data—JSON in this case.

The -d option sends our request data to CoinMarketCap. This is where you can specify what cryptocurrency you want to see. The value of “symbol=BTC” tells CoinMarketCap to send the data it has for Bitcoin. You can pass in a comma-separated list of all the cryptocurrencies you’d like to see like this: “symbol=BTC,ETH,XRP“.

Finally, the -G option configures cURL to perform a GET request instead of a POST request. This is like loading a webpage and putting all your options in the URL bar, instead of pressing a button to make your Amazon order (where the website shares your data without changing the URL). It sends this to “https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest“, which is the CoinMarketCap URL which returns the basic information you need

Reading the Response Data

This API response is in the JSON format. This is a machine-readable format, comprising of keys and values, like your headers above.

To keep things simple, the current Bitcoin price lives nested inside the data section, under BTC -> quote -> USD.

While parsing this data and extracting specific information is a bigger task than this article allows time for, that’s possible with a little programming knowledge.

Troubleshooting

If this won’t work for you, you may need to perform some basic troubleshooting or “debugging” steps. The first place to look is your API key—have you swapped out “YOUR_KEY_HERE” for your API key from step one?

Next, the CoinMarketCap response provides useful information alongside your data. If you make a bad request, your data will contain a status code, error, and message. This may explain the problem, but often the biggest hint here is the status code.

Webpages and APIs use standard status codes known as HTTP status codes. This expresses the outcome of a request, and you may already know more than you think. The 404 status code means the URL is wrong. Maybe it has moved, or you made a typo. These status codes are grouped into similar categories, based on the first character of the code:

  1. 200 series codes: everything went well.
  2. 400 series codes: you are in the wrong/made an invalid request.
  3. 500 series codes: the server is in the wrong/broken.

In most cases, double-checking your request and correcting any issues will resolve the problem. A response code of 429 means Too Many Requests. You’ve used your API key too much recently, and need to stop using it for a brief time.

3. Future Expansion

One you know the very basics, you can begin to build upon this. CoinMarketCap provide many options (although not all are free). You can retrieve historical prices, different fiat prices, many other cryptocurrencies, and far more.

You could automate this into a desktop widget or command-line script. You could build this into your website, a Twitter bot, or a physical desktop display. In all cases, retrieving different data involves changing the URL (the “-G” option), and some of your payload (the “-d” option).

The comprehensive API reference explains all the possible endpoints, payloads, and data available. By using your new skills here, and by referencing this guide, you can extract almost any cryptocurrency data you need.

What Will You Build?

Now that you know the basics of connecting to a crypto API, and pulling back data based on your needs, you’re no longer bound by the limitations of other apps. You are free to build your own desktop monitors, services, or physical products. Remember to keep your API key safe (although private key security levels are a bit too far here).

If you’re bold enough, why not combine this with an automated trade on a decentralized exchange and try to beat the market?

We earn commission if you purchase items using an affiliate link. We only recommend products we trust. See our affiliate disclosure.

Joe Coburn
Joe is a senior software developer with a degree in computer science from the University of Lincoln, UK. He is currently a Senior Writer for Blocks Decoded. Formerly, he was on the editorial team. As a writer at MakeUseOf, Joe has seen his work shared by Adobe, the Arduino Foundation, and Lifehacker. He has collaborated with Anker, BenQ, iStock, Ledger, Ultimate Ears, and many more. Joe loves all aspects of blockchain and cryptocurrencies, particularly the technical details.
Advertisement

1 Comment

  1. I tried this, copied code put my api key between the quotes “YOUR_KEY_HERE“ would not work. had to ctrl-c to get my terminal cursor back, after inspecting the code I noticed there were an uneven number of quotes so I removed the second quote — “X-CMC_PRO_API_KEY: “YOUR_KEY_HERE” -H ”
    “X-CMC_PRO_API_KEY: YOUR_KEY_HERE” -H ”
    then tada it works. Was this a test, then I think I passed.

Leave a reply

Your email address will not be published. Required fields are marked *

You may also like

Advertisement