$ npm install @tetherto/wdk-protocol-fiat-moonpayNote: This package is in beta. Please test in a dev setup first.
A simple way to integrate MoonPay widget for on-ramp and off-ramp services. You can generate signed or unsigned URLs for the MoonPay widget, get quotes for buying and selling crypto, and read protocol-related data. This package can be used in both frontend and backend environments.
This is part of WDK (Wallet Development Kit). WDK helps you build safe, nonβcustody wallets. Read more at https://docs.wallet.tether.io.
npm install @tetherto/wdk-protocol-fiat-moonpay
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'
})
Main class for MoonPay integration.
new MoonPayProtocol(account, config)
Parameters:
account (IWalletAccount | IWalletAccountReadOnly | undefined): The wallet account to use to interact with the protocolconfig (object): The protocol config
apiKey (string): Your publishable Moonpay API key.signUrl (function, optional): Callback used to sign buy/sell URLs via a trusted provider (e.g., a backend service). If not provided, the protocol returns unsigned URLs.cacheTime (number, optional): The duration in milliseconds to cache supported currencies.environment ("production" | "sandbox", optional): The environment to use for MoonPay endpoints and widget URLs. Defaults to "production". Use "production" for live transactions and "sandbox" for testing with non-real funds.| Method | Description | Returns |
|---|---|---|
buy(options) | Generates a widget URL to purchase crypto | Promise<BuyResult> |
sell(options) | Generates a widget URL to sell crypto | Promise<SellResult> |
quoteBuy(options) | Gets a quote for a crypto asset purchase | Promise<MoonPayBuyQuote> |
quoteSell(options) | Gets a quote for a crypto asset sale | Promise<MoonPaySellQuote> |
getTransactionDetail(txId, direction) | Retrieves the details of a transaction | Promise<MoonPayTransactionDetail> |
getSupportedCryptoAssets() | Retrieves a list of supported crypto assets | Promise<MoonPaySupportedCryptoAsset[]> |
getSupportedFiatCurrencies() | Retrieves a list of supported fiat currencies | Promise<MoonPaySupportedFiatCurrency[]> |
getSupportedCountries() | Retrieves a list of supported countries | Promise<MoonPaySupportedCountry[]> |
buy(options)Generates a widget URL to purchase crypto.
Options:
fiatCurrency (string): The fiat currency code (e.g., 'usd').cryptoAsset (string): The crypto asset code (e.g., 'eth').fiatAmount (number, optional): The amount in fiat currency.cryptoAmount (number, optional): The amount in crypto asset.recipient (string, optional): The wallet address to receive funds. If not provided, uses the account address.config (object, optional): Additional MoonPay widget parameters.sell(options)Generates a widget URL to sell crypto.
Options:
fiatCurrency (string): The fiat currency code (e.g., 'usd').cryptoAsset (string): The crypto asset code (e.g., 'eth').fiatAmount (number, optional): The amount in fiat currency.cryptoAmount (number, optional): The amount in crypto asset.refundAddress (string, optional): The wallet address for refunds. If not provided, uses the account address.config (object, optional): Additional MoonPay widget parameters.quoteBuy(options)Gets a quote for a crypto asset purchase.
Options:
fiatCurrency (string): The fiat currency code.cryptoAsset (string): The crypto asset code.fiatAmount (number, optional): The amount in fiat currency.cryptoAmount (number, optional): The amount in crypto asset.config (object, optional): Additional MoonPay quote parameters.quoteSell(options)Gets a quote for a crypto asset sale.
Options:
fiatCurrency (string): The fiat currency code.cryptoAsset (string): The crypto asset code.cryptoAmount (number): The amount in crypto asset (Required).config (object, optional): Additional MoonPay quote parameters.getTransactionDetail(txId, direction)Retrieves the details of a transaction.
Parameters:
txId (string): The transaction ID.direction ('buy' | 'sell'): The direction of the transaction. Defaults to 'buy'.getSupportedCryptoAssets()Retrieves a list of supported crypto assets.
getSupportedFiatCurrencies()Retrieves a list of supported fiat currencies.
getSupportedCountries()Retrieves a list of supported countries.
apiKey can be retrieved through MoonPay dashboard.sandbox environment. Read more here.secretKey safe on your backend. Expose a backend signing API to clients, and have signUrl call that API to retrieve the signed MoonPay URL.# Install dependencies
npm install
# Build TypeScript definitions
npm run build:types
# Lint code
npm run lint
# Fix linting issues
npm run lint:fix
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For support, please open an issue on the GitHub repository.