Buy and Sell
On-ramp, off-ramp, quotes, supported assets, widget options, and custom recipients.
This guide explains buying crypto (on-ramp), selling crypto (off-ramp), quotes, supported currencies, widget customization, and custom recipients. It assumes a MoonPayProtocol instance named moonpay.
Amounts use smallest units: fiat in minor units (cents), crypto in on-chain base units (for example wei for ETH).
Buy crypto (on-ramp)
You can build a signed purchase URL with buy() when you know the fiat spend:
const result = await moonpay.buy({
cryptoAsset: 'usdt',
fiatCurrency: 'usd',
fiatAmount: 10000n
})
window.open(result.buyUrl, '_blank')You can request a fixed crypto amount instead by passing cryptoAmount to buy():
const result = await moonpay.buy({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 100000000000000000n
})
window.open(result.buyUrl, '_blank')Sell crypto (off-ramp)
You can generate a sell widget URL with sell():
const result = await moonpay.sell({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 500000000000000000n
})
window.open(result.sellUrl, '_blank')Get price quotes
You can preview economics before opening the widget using quoteBuy():
const buyQuote = await moonpay.quoteBuy({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
fiatAmount: 10000n
})
console.log('Crypto amount:', buyQuote.cryptoAmount)
console.log('Fee:', buyQuote.fee)
console.log('Exchange rate:', buyQuote.rate)You can estimate proceeds for a sell with quoteSell():
const sellQuote = await moonpay.quoteSell({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 500000000000000000n
})
console.log('Fiat amount:', sellQuote.fiatAmount)Supported currencies and countries
You can list tradable assets with getSupportedCryptoAssets():
const cryptoAssets = await moonpay.getSupportedCryptoAssets()
console.log(cryptoAssets)You can list fiat currencies with getSupportedFiatCurrencies():
const fiatCurrencies = await moonpay.getSupportedFiatCurrencies()
console.log(fiatCurrencies)You can check regional availability with getSupportedCountries():
const countries = await moonpay.getSupportedCountries()
console.log(countries)Widget customization
You can pass UI options under config to buy() (see MoonPayBuyParams):
const result = await moonpay.buy({
cryptoAsset: 'usdt',
fiatCurrency: 'eur',
fiatAmount: 5000n,
config: {
colorCode: '#1f2937',
theme: 'dark',
language: 'de',
redirectURL: 'https://yourapp.com/payment-complete',
lockAmount: true,
email: 'user@example.com',
externalCustomerId: 'user_123'
}
})
window.open(result.buyUrl, '_blank')Custom recipient addresses
By default buy() credits the connected wallet. You can override the destination with recipient:
const result = await moonpay.buy({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
fiatAmount: 10000n,
recipient: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
})
window.open(result.buyUrl, '_blank')You can set a refund destination on sells with refundAddress on sell():
const result = await moonpay.sell({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 500000000000000000n,
refundAddress: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
})
window.open(result.sellUrl, '_blank')