# JavaScript crypto prices

How-to. Fetch live prices and candles in Node.js with the built-in fetch.

This guide uses Node.js 18 or later, which has `fetch` built in, so there is nothing to install. Run the code on a server: a key in browser code is visible to everyone who opens the page.

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

Put the key in an environment variable and save the code below as `prices.mjs`.

Shell:

```bash
export COINPRICE_API_KEY="your key"
node prices.mjs
```

## Make your first request

`getJson` sends the key in the `Authorization` header and throws the API message when the status is not 2xx. `ids` takes up to 250 coin ids separated by commas. 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.

JavaScript:

```javascript
const API = 'https://api.coinprice.dev/v2'
const headers = { Authorization: `Bearer ${process.env.COINPRICE_API_KEY}` }

async function getJson(path, params) {
  const response = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers })
  const body = await response.json()
  if (!response.ok) throw new Error(`${response.status} ${body.error}: ${body.message}`)
  return body
}

const prices = await getJson('/simple/price', {
  ids: 'bitcoin,ethereum',
  vs_currencies: 'usd',
  include_24hr_change: 'true',
  include_last_updated_at: 'true',
})
console.log(prices)
for (const [coin, row] of Object.entries(prices)) {
  const change = row.usd_24h_change === null ? 'n/a' : `${row.usd_24h_change.toFixed(2)}%`
  console.log(`${coin}: ${row.usd.toLocaleString('en-US', { style: 'currency', currency: 'USD' })} (${change} in 24h)`)
}
```

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
  }
}
bitcoin: $60,439.05 (-1.24% in 24h)
ethereum: $1,698.46 (-1.82% in 24h)
```

`usd` is the price in US dollars, `usd_24h_change` is the 24-hour change in percent and `last_updated_at` is when the price was seen, in Unix seconds. A change is `null` when there is not enough history.

## Get candles

`/v2/coins/{id}/ohlc` returns rows of `[time in ms, open, high, low, close]`, oldest first. The time is the start of the candle in UTC. A request returns at most 5,000 rows, and a longer window returns the newest rows, so page with `from` and `to`.

JavaScript:

```javascript
const API = 'https://api.coinprice.dev/v2'
const headers = { Authorization: `Bearer ${process.env.COINPRICE_API_KEY}` }
const res = await fetch(`${API}/coins/bitcoin/ohlc?interval=1h&limit=3`, { headers })
const candles = await res.json()
for (const [time, open, high, low, close] of candles) console.log(new Date(time).toISOString(), open, high, low, close)
```

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

```text
2026-11-24T01:00:00.000Z 61245.22893 61596.66445 60987.03237 61006.00624
2026-11-24T02:00:00.000Z 61024.45 61024.45 60010.43654 60015.43094
2026-11-24T03:00:00.000Z 60013.43351 60323.55 59395.53754 60263.4388
```

## Handle errors

An error response is JSON with `error`, `message` and `request_id`. This is what `getJson` throws for a wrong key.

Output, real run:

```text
Error: 401 unauthorized: That API key is not valid.
```

`request_id` is the id of that call. 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 call the API from the browser?** Keep the key on a server and have your page call your server. A key in browser code can be copied by any visitor.

**How do I get several coins in one call?** Put their ids in `ids`, separated by commas. One request prices up to 250 coins and counts as one call.
