> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# getMoonPayUrl

# getMoonPayUrl

Returns a signed [MoonPay](https://www.moonpay.com/) 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

```javascript theme={"system"}
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

| Parameter       | Type                       | Description                                                                                                                                                             |
| --------------- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `chain`         | `Chain`                    | The blockchain to fund (e.g., `'EVM'`, `'SOL'`).                                                                                                                        |
| `walletAddress` | `string`                   | The wallet address that will receive the purchased crypto.                                                                                                              |
| `token`         | `string` (optional)        | Token symbol or MoonPay currency code to lock in the widget (e.g., `'USDC'`, `'usdc_polygon'`).                                                                         |
| `currency`      | `string` (optional)        | ISO 4217 fiat currency code used as the quote currency (e.g., `'USD'`, `'EUR'`).                                                                                        |
| `tokenAmount`   | `number` (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`). |
| `networkId`     | `string` (optional)        | The network ID to scope the purchase to (e.g., `'1'` for Ethereum mainnet, `'137'` for Polygon).                                                                        |
| `merchantName`  | `string` (optional)        | Your app or merchant name shown to the user in the widget.                                                                                                              |
| `client`        | `DynamicClient` (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

```javascript theme={"system"}
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:

```javascript theme={"system"}
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

```javascript theme={"system"}
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

| Error                              | Description                                                                                                                                           |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MoonPayProviderNotAvailableError` | MoonPay is not available for the requested environment, chain, or token, or no onramp URL was returned.                                               |
| `MoonPayInvalidUrlError`           | The 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`](/javascript/reference/client/get-moonpay-currencies) to discover supported tokens.

## React

`useGetMoonPayUrl` is a mutation hook — call `mutate` to generate a URL on demand:

```tsx theme={"system"}
import { useGetMoonPayUrl } from "@dynamic-labs-sdk/react-hooks";

function BuyWithMoonPay({ walletAddress }) {
  const { mutate: getMoonPayUrl, isPending } = useGetMoonPayUrl();

  return (
    <button
      disabled={isPending}
      onClick={() =>
        getMoonPayUrl(
          { chain: "EVM", walletAddress, token: "ETH", currency: "USD" },
          {
            onSuccess: (url) => {
              if (new URL(url).protocol === "https:") {
                window.open(url, "_blank");
              }
            },
          },
        )
      }
    >
      {isPending ? "Loading..." : "Buy with MoonPay"}
    </button>
  );
}
```

## Related functions

* [getMoonPayCurrencies](/javascript/reference/client/get-moonpay-currencies) - List crypto currencies supported by MoonPay for a chain
* [getCoinbaseBuyUrl](/javascript/reference/client/get-coinbase-buy-url) - Get a Coinbase onramp buy URL
* [createCryptoDotComPayment](/javascript/reference/client/create-crypto-dot-com-payment) - Create a Crypto.com payment
