Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt

Use this file to discover all available pages before exploring further.

getMoonPayUrl

Returns a signed MoonPay URL for funding a wallet with a fiat-to-crypto purchase. Open the returned URL in a new tab or popup to let the user complete the purchase on MoonPay’s hosted widget. The URL is generated through Dynamic’s onramp API and is pre-signed for your environment, so you don’t need to handle MoonPay credentials directly. Requires a MoonPay onramp to be enabled and configured for your environment in the Dynamic dashboard.

Usage

import { getMoonPayUrl } from "@dynamic-labs-sdk/client";

const url = await getMoonPayUrl({
  chain: "EVM",
  walletAddress: "0x742d35Cc6634C0532925a3b844Bc9e7595f7ABCD",
  token: "USDC",
  currency: "USD",
  tokenAmount: 100,
});

// Open MoonPay in a new tab
window.open(url, "_blank");

Parameters

ParameterTypeDescription
chainChainThe blockchain to fund (e.g., 'EVM', 'SOL').
walletAddressstringThe wallet address that will receive the purchased crypto.
tokenstring (optional)Token symbol or MoonPay currency code to lock in the widget (e.g., 'USDC', 'usdc_polygon').
currencystring (optional)ISO 4217 fiat currency code used as the quote currency (e.g., 'USD', 'EUR').
tokenAmountnumber (optional)Pre-filled fiat amount in the quote currency. This is the amount the user spends in fiat, not the crypto amount they receive (maps to MoonPay’s quoteCurrencyAmount).
networkIdstring (optional)The network ID to scope the purchase to (e.g., '1' for Ethereum mainnet, '137' for Polygon).
merchantNamestring (optional)Your app or merchant name shown to the user in the widget.
clientDynamicClient (optional)The Dynamic client instance. Only required when using multiple clients.

Returns

Promise<string> - A promise that resolves to the signed MoonPay URL.

Examples

Open in a popup window

import { getMoonPayUrl } from "@dynamic-labs-sdk/client";

const BuyWithMoonPayButton = ({ walletAddress }) => {
  const handleBuy = async () => {
    const url = await getMoonPayUrl({
      chain: "EVM",
      walletAddress,
      token: "ETH",
      currency: "USD",
    });

    // Open MoonPay in a popup window
    window.open(url, "_blank", "width=500,height=700");
  };

  return <button onClick={handleBuy}>Buy with MoonPay</button>;
};

Pre-fill a purchase amount

Lock the widget to a specific token and pre-fill the fiat amount the user will spend:
import { getMoonPayUrl } from "@dynamic-labs-sdk/client";

const url = await getMoonPayUrl({
  chain: "EVM",
  walletAddress: "0x742d35Cc6634C0532925a3b844Bc9e7595f7ABCD",
  token: "USDC",
  networkId: "137", // Polygon
  currency: "USD",
  tokenAmount: 50, // User spends $50 USD
});

window.location.href = url;

Handle errors

import { getMoonPayUrl } from "@dynamic-labs-sdk/client";

const openMoonPay = async (walletAddress) => {
  try {
    const url = await getMoonPayUrl({
      chain: "EVM",
      walletAddress,
      token: "USDC",
      currency: "USD",
    });

    window.open(url, "_blank");
  } catch (error) {
    // MoonPay may not be available for this environment, chain, or token
    console.error("Could not open MoonPay:", error.message);
  }
};

Errors

ErrorDescription
MoonPayProviderNotAvailableErrorMoonPay is not available for the requested environment, chain, or token, or no onramp URL was returned.
MoonPayInvalidUrlErrorThe onramp API returned a URL that failed host/scheme validation. The URL is verified to use https: and a moonpay.com host before it is returned.

Prerequisites

  • MoonPay must be enabled and configured as an onramp provider for your environment in the Dynamic dashboard.
  • The requested chain and token must be supported by MoonPay. Use getMoonPayCurrencies to discover supported tokens.