useTonPay hook provides a streamlined interface for sending TON Pay transfers in React applications. It handles wallet connection, transaction signing, and error management automatically, abstracting the complexity of TonConnect integration.
How useTonPay Works
TheuseTonPay hook simplifies the payment process by managing the entire transaction lifecycle:
1
Wallet Connection
If the user’s wallet is not connected, the hook automatically opens the TonConnect modal and waits for a successful connection.
2
Message Creation
Your application provides a factory function that builds the transaction
message. This can be done client-side or by calling your backend server.
3
Transaction Signing
The hook sends the transaction to the user’s wallet for approval and
signature.
4
Result Handling
Upon completion, the hook returns the transaction result along with tracking identifiers for payment reconciliation.
Integration Approaches
You can integrateuseTonPay using two different approaches depending on your application architecture:
Client-Side Message Building
Build the payment message directly in the browser. This approach is faster to implement and reduces server load, but requires the client to handle all transaction details. Best suited for:- Applications without backend infrastructure
- Rapid prototyping and development
- Simple payment flows without complex business logic
Server-Side Message Building
Delegate message creation to your backend server. The server builds the transaction, stores tracking identifiers, and returns the message to the client for signing. Best suited for:- Production applications requiring audit trails
- Complex payment workflows with validation
- Centralized logging and transaction management
- Applications where tracking identifiers must be persisted before user action
Client-Side Implementation
Prerequisites
Ensure your application is wrapped with the TonConnect UI provider:The TonConnect manifest file must be publicly accessible and properly
configured with your application’s metadata. See TonConnect documentation for
manifest requirements.
Basic Implementation
Understanding the Response
Thepay function returns an object containing:
Transaction result from TonConnect containing the signed transaction BOC (Bag
of Cells) and other transaction details.
The transaction message that was sent, including the recipient address,
amount, and payload.
Unique tracking identifier for this transaction. Use this to correlate webhook
notifications with your orders.
Base64-encoded hash of the transaction body. This can be used for advanced
transaction verification.
Server-Side Implementation
Backend Endpoint
Create an API endpoint that builds the transaction message and stores tracking data:Frontend Implementation
Error Handling
TheuseTonPay hook throws errors in the following scenarios:
Wallet Connection Errors
Transaction Errors
Best Practices
Always store tracking identifiers
Always store tracking identifiers
The
reference and bodyBase64Hash returned by createTonPayTransfer are essential for matching webhook notifications to your orders. Store these immediately in your database, preferably before the user signs the transaction.Validate amounts on the server
Validate amounts on the server
Never trust amount values sent from the client. Always validate or generate amounts server-side to prevent manipulation.
Implement proper error boundaries
Implement proper error boundaries
Wrap your payment components with React error boundaries to gracefully handle failures.
Show loading states
Show loading states
Payment processing can take several seconds. Provide clear feedback to users during wallet connection and transaction signing.
Use environment-specific configuration
Use environment-specific configuration
Always use environment variables for sensitive data and chain configuration.
Troubleshooting
Hook throws 'TonConnect provider not found'
Hook throws 'TonConnect provider not found'
Ensure your application is wrapped with
TonConnectUIProvider:Wallet connection modal does not open
Wallet connection modal does not open
- Verify the TonConnect manifest URL is accessible and valid - Check browser console for TonConnect initialization errors - Ensure the manifest file is served with correct CORS headers - Test the manifest URL directly in your browser
Transaction fails with 'Invalid recipient address'
Transaction fails with 'Invalid recipient address'
- Verify the recipient address is a valid TON address (starts with EQ, UQ, or 0:) - Ensure you’re using the correct address format for your chain (mainnet vs testnet) - Check that the address includes the full base64 format with workchain
Factory function throws 'Failed to create TON Pay transfer'
Factory function throws 'Failed to create TON Pay transfer'
Common causes:
- Invalid API key or missing API key for authenticated endpoints
- Network connectivity issues
- Invalid parameter values (negative amounts, invalid addresses)
- Wrong chain configuration (mainnet vs testnet)
User rejects transaction but no error is shown
User rejects transaction but no error is shown
TonConnect may not always throw an error on rejection. Implement timeout handling:
API Key Configuration
The TON Pay API key is optional but enables essential merchant features including transaction visibility in the dashboard, webhook notifications, and centralized wallet management. When usinguseTonPay with server-side message building, optionally include the API key in your backend endpoint:
For complete details about API key configuration, benefits, and security best
practices, see the API Key
Configuration section.
Testnet Configuration
Before deploying to production, test your payment integration on the TON testnet.Basic Testnet Setup
Configure your environment to use testnet:For complete testnet configuration guide, including how to get testnet TON,
test jetton transfers, verify transactions, and transition to production, see
the Testnet Configuration
section.