Historical Bitcoin price data and fiat currency conversions
The Medusa Pricing API provides access to historical Bitcoin price data across multiple fiat currencies. This is essential for calculating the fiat value of Bitcoin transactions at the time they occurred.
import medusa from '@/api/medusa'// Get Bitcoin prices from January 1, 2024const timestamp = new Date('2024-01-01').getTime()const prices = await medusa.getBitcoinPricesAt(timestamp)console.log(`BTC price on Jan 1, 2024:`)console.log(` USD: $${prices.USD}`)console.log(` EUR: €${prices.EUR}`)console.log(` GBP: £${prices.GBP}`)
Object containing the satoshi amount converted to multiple fiat currencies at the historical rate
Usage
Response Example
Transaction History
import medusa from '@/api/medusa'// Convert 50,000 sats to fiat at a specific dateconst sats = 50000const timestamp = new Date('2024-01-01').getTime()const fiatValues = await medusa.getPricesAt(sats, timestamp)console.log(`50,000 sats on Jan 1, 2024:`)console.log(` USD: $${fiatValues.USD}`)console.log(` EUR: €${fiatValues.EUR}`)console.log(` GBP: £${fiatValues.GBP}`)
The Medusa API is used throughout Medusa Wallet to display historical fiat values for Lightning payments:
import lnbits from '@/api/lnbits'import medusa from '@/api/medusa'import { SATOSHIS_IN_BITCOIN } from '@/constants/btc'// Fetch paymentsconst payments = await lnbits.getPayments(inkey)// Each payment already includes fiatSnapshot from Medusa APIpayments.forEach(payment => { console.log(`Payment: ${payment.sats} sats`) console.log(`Value at time: $${payment.fiatSnapshot.USD}`)})
The LNbits API automatically batches and caches Medusa price requests to optimize performance:
import { getHistoricalPricesMap } from '@/utils/medusa'// Group payments by rounded timestampsconst historicalPricesMap = getHistoricalPricesMap( payments.map((payment) => ({ id: payment.payment_hash, timestamp: new Date(payment.time).getTime() })))// Fetch prices once per unique timestampfor (const [timestamp, ids] of Object.entries(historicalPricesMap)) { const fiatSnapshot = await medusa.getBitcoinPricesAt(Number(timestamp)) // Use fiatSnapshot for all payments in this timestamp bucket}