Overview
The Moment React SDK enables seamless payment integration into any React application. Whether you need a modal overlay or an inline embedded checkout, the SDK provides a simple, declarative API with React hooks to handle the entire payment flow. Built with React and modern hooks, the SDK integrates naturally with your React workflow and state management.Embedded Modal :
Key Features
- React Hooks API – Declarative interface with
useCheckouthook - Flexible Display Options – Modal or inline embedded checkout
- TypeScript Support – Full type definitions included
- Flexible Callbacks – Handle success, failure, cancellation, and retry events
- Cross-Origin Safe – Secure iframe communication via
postMessage - Environment Detection – Automatic sandbox / production switching
- PCI Compliant – Sensitive payment data handled entirely in Moment’s secure iframe
Display Variants
| Variant | Availability | Description |
|---|---|---|
modal | Available | Opens checkout in a centered modal overlay (default) |
inline | Coming Soon | Renders checkout inside a specified container element |
How It Works
- Page loads —
MomentProvidermounts (SDK init), thenMomentCheckoutProvidermounts, registering yourfetchClientToken,fetchSessionStatus, and event callbacks (onSuccess,onFailure, etc.) - Customer clicks Pay —
checkout.launch()is called from your component - SDK calls
fetchClientToken— your callback creates a session on your backend (with asuccess_url) and returns theclient_token - Checkout opens in your chosen variant (modal or inline)
- Customer submits payment — Moment takes over the full page and redirects to the authentication page
- Customer completes authentication on the external page
- Customer is redirected back to your
success_urlwith?session_id=abc123appended - Page loads again —
MomentCheckoutProviderdetects thesession_idin the URL automatically and calls yourfetchSessionStatus('abc123')callback - Your callback fetches the status from your backend, which queries Moment API
- SDK routes to the right handler —
onSuccess,onFailure,onExpired, oronCancel— and removessession_idfrom the URL silently - Webhooks are delivered to your backend regardless of outcome
Installation
NPM
Yarn
pnpm
Quick Start
1. Initialize the SDK
Set up two providers at the root of your app:MomentProvider initializes the SDK instance (equivalent to Moment(clientKey) in vanilla JS), and MomentCheckoutProvider configures the checkout product (equivalent to moment.checkout(config)). You provide two fetch callbacks — the SDK calls them automatically at the right moments, so you never manage tokens or session IDs manually.
Backend endpoints:
- Node.js
- Python
- Go
- Firebase
- Supabase
- Lambda
server.js
MomentProvider + MomentCheckoutProvider:
- Fetch (Native)
- Axios
- Firebase
- Supabase
- Lambda
App.jsx
2. Open Checkout
AdduseCheckout to any component and call checkout.launch() to trigger checkout:
Checkout.jsx
API Reference
MomentProvider (SDK level)
Initializes the Moment SDK instance — equivalent toMoment(clientKey) in vanilla JS. Provides the SDK context to all product providers nested below it. Must be the outermost Moment wrapper in your app.
| Prop | Type | Required | Description |
|---|---|---|---|
clientKey | string | Yes | Your publishable key (pk_test_... or pk_live_...). Determines sandbox vs. production environment. |
MomentCheckoutProvider (checkout product)
Initializes thecheckout product — equivalent to moment.checkout(config) in vanilla JS. Must be a child of MomentProvider and a parent of any component using useCheckout.
| Prop | Type | Required | Description |
|---|---|---|---|
fetchClientToken | function | Yes | Async function returning Promise<string> — a fresh client token. Called by the SDK when checkout.launch() is invoked. |
fetchSessionStatus | function | No | Async function receiving a sessionId string, returning Promise<{ status: string, ... }>. Called when a session_id is found in the URL after a redirect-based payment. Required only if you use redirect mode. |
onSuccess | function | No | Global callback fired when payment succeeds (both embedded and redirect flows) |
onFailure | function | No | Global callback fired when payment fails |
onCancel | function | No | Global callback fired when user cancels |
onAttemptFailed | function | No | Global callback fired when a payment attempt fails (user can retry) |
onExpired | function | No | Global callback fired when the payment session expires |
useCheckout(config)
React hook that returns acheckout object for triggering checkout from any component. Must be used inside MomentCheckoutProvider. Callbacks defined here are component-level and fire alongside any global callbacks on MomentCheckoutProvider.
| Parameter | Type | Required | Description |
|---|---|---|---|
onSuccess | function | No | Component-level callback fired when payment succeeds |
onFailure | function | No | Component-level callback fired when payment fails |
onCancel | function | No | Component-level callback fired when user cancels |
onAttemptFailed | function | No | Component-level callback fired when a payment attempt fails (user can retry) |
onExpired | function | No | Component-level callback fired when the payment session expires |
checkout object — checkout.launch(), checkout.close(), and checkout.submit().
checkout.launch(options)
Opens the payment checkout. Internally callsfetchClientToken from MomentCheckoutProvider to retrieve a fresh token, then opens checkout. The display variant (modal or inline) is determined from the JWT payload variant claim, which is set when creating the session on your backend.
| Parameter | Type | Required | Description |
|---|---|---|---|
options.container | string or Element | No | CSS selector or DOM element — for inline variant only |
Promise that resolves with { checkoutUrl, variant } once the checkout is open.
Examples:
checkout.close()
Manually closes the modal or inline checkout.checkout.submit() Coming soon
Triggers payment submission from outside the checkout UI — useful when you want an external “Pay” button rather than relying on the checkout’s built-in submit. Only available for the inline variant.
Callback Strategies
The Moment React SDK offers flexible callback handling at both the global (MomentCheckoutProvider) and component (useCheckout hook) levels. Understanding when to use each approach helps you write cleaner, more maintainable code.
Global Callbacks (MomentCheckoutProvider Level)
Define callbacks once at theMomentCheckoutProvider level to apply consistent behavior across your entire application.
✅ Best for:
- App-wide analytics tracking
- Global error logging
- Consistent redirect patterns
- Shared notification systems
App.jsx
Component-Level Callbacks (useCheckout Hook)
Override or supplement global callbacks with component-specific behavior. ✅ Best for:- Component-specific state updates
- Different flows for different payment contexts
- Local UI feedback
- Context-aware error handling
ProductCheckout.jsx
Callback Precedence
Important: Component-level callbacks do not override global callbacks. Both will fire:- Component-level callback fires first (if defined)
- Global callback fires second (if defined)
Example: Multi-Level Callback Architecture
App.jsx
SubscriptionPage.jsx
Advanced Usage
Custom Hook for Payment Logic
hooks/usePayment.ts
Using the Custom Hook
ProductCheckout.tsx
Security Best Practices
- Never expose your secret key (
sk_...) in frontend code - Always create sessions server-side using your secret key
- Validate webhooks to confirm payment status server-side
- Use HTTPS for all pages that include the SDK
- Handle token expiry by requesting a new token if needed
Next Steps
- View Sample Applications for end-to-end examples
- Create your first Payment Session
- Configure Webhooks
- Test your integration in Sandbox mode

