# Python crypto prices

How-to. Fetch the latest price, check how old it is and pull candles with the requests library.

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](/signup) 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.

Shell:

```bash
pip install requests
export 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.

Python:

```python
import os
import requests

API = "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)
```

Output, real run, 2026-11-24 04:32 UTC:

```text
{'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

- `usd` is the price in US dollars, a JSON number.
- `usd_24h_change` is the change over 24 hours in percent. It is `null` when there is not enough history to work it out.
- `last_updated_at` is 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.

Python:

```python
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")
```

Output, real run, 2026-11-24 04:32 UTC:

```text
bitcoin: $60,439.05, -1.24% in 24h, fresh, 11 s old
ethereum: $1,698.46, -1.82% in 24h, fresh, 11 s old
```

## Get 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`.

Python:

```python
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)
```

Output, real run, 2026-11-24 04:32 UTC:

```text
1795482000000 61245.22893 61596.66445 60987.03237 61006.00624
1795485600000 61024.45 61024.45 60010.43654 60015.43094
1795489200000 60013.43351 60323.55 59395.53754 60263.4388
```

## Handle errors

The function above stops with the API message on any error status. This is what a wrong key prints.

Output, real run:

```text
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](/docs).

## Questions

**Can I request prices in another currency?** Yes. Pass another code in vs_currencies. The codes are listed at /v2/simple/supported_vs_currencies.

**Why is usd_24h_change null?** There is not enough history yet to compare against the price 24 hours earlier.

**Does a stale price disappear?** No. It stays as the last price seen, with its old last_updated_at, so check the age before you use it.
