import MoonPayProtocol from '@tetherto/wdk-protocol-fiat-moonpay'
const signUrl = async (urlForSignature) => {
const response = await fetch('https://your-backend.example.com/moonpay/sign-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ urlForSignature })
})
if (!response.ok) {
throw new Error(`Failed to sign MoonPay URL: ${response.status} ${response.statusText}`)
}
const { signedUrl } = await response.json()
return signedUrl
}
// Initialize protocol
const moonpay = new MoonPayProtocol(undefined, {
apiKey: 'YOUR_PUBLISHABLE_API_KEY',
signUrl,
environment: 'sandbox'
})
// Get a buy quote
const buyQuote = await moonpay.quoteBuy({
fiatCurrency: 'usd',
cryptoAsset: 'eth',
fiatAmount: 100
})
// Generate a buy widget URL
const { buyUrl } = await moonpay.buy({
fiatCurrency: 'usd',
cryptoAsset: 'eth',
fiatAmount: 100,
recipient: '0xabc'
})
console.log('Buy URL:', buyUrl)
// Get a sell quote
const sellQuote = await moonpay.quoteSell({
fiatCurrency: 'usd',
cryptoAsset: 'eth',
cryptoAmount: 100
})
// Generate a sell widget URL
const { sellUrl } = await moonpay.sell({
fiatCurrency: 'usd',
cryptoAsset: 'eth',
cryptoAmount: 100,
refundAddress: '0xabc'
})