<MoonPayProvider> and a useMoonPay() hook. The SDK ships with TypeScript types for autocomplete and inference.
The SDK is tested against:
- React Native 0.73+
- React
>=18 react-native-webview13.0+- iOS 14+ and Android API 26+
Install
Install the SDK package and its peer dependency:react-native-webview install guide to link the native module. On iOS, run pod install from your ios/ directory.
You also need a Web Crypto getRandomValues polyfill, which is a required setup
step on React Native. See Register a crypto polyfill
below.
Register a crypto polyfill
The SDK does not bundle a polyfill, because the right one depends on whether you run Expo or bare React Native. Import it as the very first line of your app entry, before you import the SDK or mount<MoonPayProvider>. A dedicated
polyfills.ts that you import first keeps the ordering explicit.
expo-crypto exposes a Web-Crypto-compatible getRandomValues that
works in Expo Go, but it does not install itself on the global. You register it
yourself with the snippet above. Import polyfills.ts before anything else. With
Expo Router, put the import at the top of your root layout, since the router owns
the entry point.
On bare React Native, react-native-get-random-values installs the global as a
side effect of the import. Keep it as the first import in index.js.
expo-standard-web-crypto offers a polyfillWebCrypto() one-liner as an
alternative on Expo. It falls back to an insecure Math.random when the
native RNG is unavailable, so prefer the expo-crypto snippet above, which
uses the native RNG directly.Conventions
Provider + hook
Mount<MoonPayProvider> near the top of your app tree and call useMoonPay()
from any descendant component to get the client. Pass the
sessionToken at mount,
or supply it later with initialize().
The SDK presents frames in two ways:
- Client methods such as
client.connect()orclient.setupWidget()open a full-screen modal managed by the provider. Hidden utility frames (connection check, headless buy, reset) render at zero size. - Inline components such as
<MoonPayConnect>or<MoonPayWidget>render the frame wherever you place them in your layout.
App.tsx
Result<T, E>
Most SDK functions return a Result<T, E> instead of throwing.
Use this pattern to branch on success vs failure: