This guide fetches live prices with requests, reads each field and skips a price that has gone stale. It takes about ten minutes.
Get a key
Sign up for a free key. The Free plan has 50,000 calls a month and 100 calls a minute, needs no card, and every plan stops at its limit with no extra charge.
Install the library and put the key in an environment variable. Your code reads it from there, so it never sits in a file.
pip install requestsexport COINPRICE_API_KEY="your key"Make your first request
Send a GET request to /v2/simple/price with the key in the Authorization header. ids takes coin ids separated by commas, up to 250 in one call. An id the API does not know is left out of the answer, not an error, and an answer with none still counts as a call.
import osimport requestsAPI = "https://api.coinprice.dev/v2"HEADERS = {"Authorization": f"Bearer {os.environ['COINPRICE_API_KEY']}"}def get_prices(ids): response = requests.get( f"{API}/simple/price", headers=HEADERS, params={ "ids": ",".join(ids), "vs_currencies": "usd", "include_24hr_change": "true", "include_last_updated_at": "true", }, timeout=10, ) if not response.ok: raise SystemExit(response.json()["message"]) return response.json()prices = get_prices(["bitcoin", "ethereum"])print(prices){'bitcoin': {'last_updated_at': 1795494725, 'usd': 60439.05, 'usd_24h_change': -1.2396356881006931}, 'ethereum': {'last_updated_at': 1795494725, 'usd': 1698.455891, 'usd_24h_change': -1.8185536860928275}}Read the response
usdis the price in US dollars, a JSON number.usd_24h_changeis the change over 24 hours in percent. It isnullwhen there is not enough history to work it out.last_updated_atis when the price was seen, in Unix seconds.
To get another currency, change vs_currencies. The response then holds that code in place of usd.
Check how old a price is
A coin with no new price keeps its last price and its old last_updated_at. A price counts as stale after 60 seconds. Thin coins can be minutes or hours old; that is real, not an outage. Add import time at the top and replace the last two lines with this.
for coin, row in get_prices(["bitcoin", "ethereum"]).items(): age = time.time() - row["last_updated_at"] state = "stale" if age > 60 else "fresh" change = row["usd_24h_change"] change_text = f"{change:+.2f}%" if change is not None else "n/a" print(f"{coin}: ${row['usd']:,.2f}, {change_text} in 24h, {state}, {age:.0f} s old")bitcoin: $60,439.05, -1.24% in 24h, fresh, 11 s oldethereum: $1,698.46, -1.82% in 24h, fresh, 11 s oldGet candles
/v2/coins/{id}/ohlc returns rows of [time in ms, open, high, low, close], oldest first. interval is 1m, 5m, 15m, 1h, 4h or 1d, and limit is up to 5,000. A request returns at most 5,000 rows, and a longer window returns the newest rows, so page with from and to.
response = requests.get( f"{API}/coins/bitcoin/ohlc", headers=HEADERS, params={"interval": "1h", "limit": 3}, timeout=10,)for time_ms, open_, high, low, close in response.json(): print(time_ms, open_, high, low, close)1795482000000 61245.22893 61596.66445 60987.03237 61006.006241795485600000 61024.45 61024.45 60010.43654 60015.430941795489200000 60013.43351 60323.55 59395.53754 60263.4388Handle errors
The function above stops with the API message on any error status. This is what a wrong key prints.
That API key is not valid.An error body is flat JSON: error is a string code, next to message and request_id. A 429 has two codes. For rate_limited, wait Retry-After seconds and retry. For quota_exceeded, stop and tell the user: the month's calls are used up, there is no Retry-After, and it resets on the 1st of the month UTC. The full list of errors is in the API reference.