Skip to main content
The 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:
  1. Checks whether a wallet is connected and, if not, opens the TON Connect modal and waits for a successful connection.
  2. Calls an application-provided factory function that builds the transaction message, either in the client-side or by requesting it from a backend service.
  3. Sends the transaction message to the connected wallet for user approval and signing.
  4. 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:
The TON Connect manifest file must be publicly accessible and include valid application metadata. Replace <APP_URL> with the public HTTPS origin that serves tonconnect-manifest.json.

Basic implementation

Response fields

The pay 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

The useTonPay 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

Wrap the application with TonConnectUIProvider:
  1. Verify that the TON Connect manifest URL is accessible and valid.
  2. Check the browser console for TON Connect initialization errors.
  3. Ensure the manifest file is served with correct CORS headers.
  4. Open the manifest URL directly in the browser to verify it loads.
  1. Verify that the recipient address is a valid TON address.
  2. Ensure the address format matches the selected chain; mainnet or testnet.
  3. Verify that the address includes the full base64 representation with workchain.
  1. Check whether the optional API key is missing or invalid for endpoints that require authentication.
  2. Verify network connectivity.
  3. Validate parameter values. For example, non-negative amounts and valid addresses.
  4. Confirm the correct chain configuration; mainnet or testnet.
To inspect the underlying API error:
  1. Note that TON Connect may not emit an explicit error on rejection.
  2. Add timeout handling:

Optional API key configuration

When using useTonPay 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.