useTonPay hook provides a React interface for creating TON Pay transfers. It integrates with TON Connect to connect a wallet, sign transactions, and surface transfer errors through the hook state.
How useTonPay works
The useTonPay hook manages the client-side flow for sending a TON Pay transfer. It performs the following steps:
- Checks whether a wallet is connected and, if not, opens the TON Connect modal and waits for a successful connection.
- Calls an application-provided factory function that builds the transaction message, either in the client-side or by requesting it from a backend service.
- Sends the transaction message to the connected wallet for user approval and signing.
- Returns the transaction result along with tracking identifiers that can be used for reconciliation.
Integration approaches
useTonPay can be integrated in two ways, depending on where the transaction message is created.
Client-side message building
- Message construction happens in the browser.
- All transaction fields are provided by the client.
Server-side message building
- Message construction happens on the backend.
- Tracking identifiers are stored on the server before the message is returned to the client for signing.
Client-side implementation
Prerequisites
The application must be wrapped with the TON Connect UI provider:<APP_URL> with the public HTTPS origin that serves tonconnect-manifest.json.
Basic implementation
Response fields
Thepay function returns the following fields:
SendTransactionResponse
required
Transaction result returned by TON Connect. It contains the signed transaction bag of cells and additional transaction details.
TonPayMessage
required
The transaction message that was sent, including the recipient address, amount, and payload.
string
Unique tracking identifier for this transaction. Use it to correlate webhook notifications with orders.
string
Base64-encoded hash of the transaction body. 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
-
Persist tracking identifiers immediately.
-
Always validate or generate amounts server-side to prevent manipulation. Never trust amount values sent from the client.
-
Wrap the payment components with React error boundaries to handle failures.
-
Payment processing can take several seconds. Provide clear feedback to users during wallet connection and transaction signing.
-
Use environment variables for sensitive data and chain configuration.
Troubleshooting
If the hook throws TonConnect provider not found
If the hook throws TonConnect provider not found
Wrap the application with
TonConnectUIProvider:If the wallet connection modal does not open
If the wallet connection modal does not open
- Verify that the TON Connect manifest URL is accessible and valid.
- Check the browser console for TON Connect initialization errors.
- Ensure the manifest file is served with correct CORS headers.
- Open the manifest URL directly in the browser to verify it loads.
If the transaction fails with Invalid recipient address
If the transaction fails with Invalid recipient address
- Verify that the recipient address is a valid TON address.
- Ensure the address format matches the selected chain; mainnet or testnet.
- Verify that the address includes the full base64 representation with workchain.
If the factory function throws Failed to create TON Pay transfer
If the factory function throws Failed to create TON Pay transfer
- Check whether the optional API key is missing or invalid for endpoints that require authentication.
- Verify network connectivity.
- Validate parameter values. For example, non-negative amounts and valid addresses.
- Confirm the correct chain configuration; mainnet or testnet.
If the user rejects the transaction and no error is reported
If the user rejects the transaction and no error is reported
- Note that TON Connect may not emit an explicit error on rejection.
- Add timeout handling:
Optional API key configuration
When usinguseTonPay with server-side message building, the optional API key can be included in the backend endpoint to enable dashboard features and webhooks:
Testnet configuration
A complete description of testnet configuration, including obtaining testnet TON, testing jetton transfers, verifying transactions, and preparing for mainnet deployment, is available in the Testnet configuration section.Next steps
Webhook integration
Receive real-time notifications when payments complete.
Transaction status
Query payment status using reference or body hash.