Installation
1
Choose your installation method
You can use the button via npm package, CDN, or by copying the files to your project.Option A: Via npmOption B: Via CDN (no installation needed)Option C: Copy to your project
After npm install, copy
node_modules/@ton-pay/ui/dist/ton-pay-embed.js to your public assets folder.2
Create TonConnect manifest
Create a
tonconnect-manifest.json file in your public directory:Learn more about the manifest format in the TonConnect documentation.
Integration Guide
There are two ways to integrate the TonPay button in vanilla JavaScript:Option 1: Simple Embed (Quickest)
The simplest way - just add a div and script tag. Perfect for static sites and simple integrations.1
Add container div
Place a div where you want the button to appear:
2
Add the embed script
Add the script tag with your configuration as URL parameters:
3
Add TonConnect import map
For ES modules support, add the import map before your scripts:
4
Create payment handler
Implement your payment logic:
The
callback parameter in the embed script should match your global function name (handlePayment in this example).Option 2: Using TonPay Client (More Control)
For more control over the payment flow, use thecreateTonPay client with your own button or UI elements.
1
Set up HTML
Create your HTML structure:
2
Import and initialize
Import the TonPay client and create an instance:
3
Implement payment logic
Add your payment handler:
Integration with createTonPayTransfer
To create properly formatted payment messages with tracking, usecreateTonPayTransfer from the API package:
1
Add API package script
Include the API package via CDN:
2
Use in payment handler
Store
reference and bodyBase64Hash to track payment status via webhooks. See the Webhooks guide for details.Configuration
Embed Script Parameters
Configure the button appearance using URL parameters:| Parameter | Type | Default | Description |
|---|---|---|---|
containerId | string | "ton-pay-btn" | Target element ID to render the button |
preset | "default" | "gradient" | - | Built-in theme preset |
bgColor | string | "#0098EA" | Background color (hex) or CSS gradient (URL-encoded) |
textColor | string | "#FFFFFF" | Text and icon color |
variant | "long" | "short" | "long" | Button text variant |
text | string | - | Custom button text (overrides variant) |
loadingText | string | "Processing..." | Text shown during loading |
borderRadius | number | 8 | Border radius in pixels |
fontFamily | string | "inherit" | CSS font-family value |
width | number | 300 | Button width in pixels |
height | number | 44 | Button height in pixels |
showMenu | boolean | true | Show dropdown menu with wallet actions |
callback | string | - | Global function name to call on button click |
Examples
JavaScript API
The embed script exposes a globalTonPayEmbed object for programmatic control:
Methods
TonPayEmbed.mount(config)
Update button configuration dynamically:
TonPayEmbed.setCallback(functionName)
Change the callback function:
TonPayEmbed.setAddress(address)
Update the displayed wallet address (for menu):
TonPayEmbed.click()
Programmatically trigger button click:
Example: Dynamic Configuration
TonPayClient API
When usingcreateTonPay, you get access to these methods and properties:
Properties
address: string | null
Get the currently connected wallet address:
Methods
waitForWalletConnection(): Promise<string>
Wait for wallet connection, opening the modal if needed:
pay(getMessage): Promise<PayResult>
Execute a payment transaction:
disconnect(): Promise<void>
Disconnect the current wallet:
Best Practices
1. Handle errors gracefully
1. Handle errors gracefully
Always wrap payment calls in try-catch blocks and display clear error messages to users.
2. Show loading states
2. Show loading states
Update your UI during payment processing to prevent multiple clicks and provide feedback.
3. Validate before payment
3. Validate before payment
Check amounts, addresses, and user input before calling the payment function.
4. Use HTTPS in production
4. Use HTTPS in production
TonConnect requires HTTPS for security. Use HTTPS for your manifest URL and callback URLs.
5. Store tracking identifiers
5. Store tracking identifiers
Save
reference and bodyBase64Hash from createTonPayTransfer to track payments via webhooks.6. Test thoroughly
6. Test thoroughly
Test with testnet first, try different wallets, and handle all error scenarios before going live.
Framework Integration
The vanilla JavaScript button works with any framework:With WordPress
With Plain HTML/CSS/JS
Simply copy the complete example above into your HTML file.With Build Tools (Webpack, Vite, etc.)
🔗 Related Resources
Create Transfer
Build payment messages with createTonPayTransfer
Webhooks
Track payment status with webhooks
React Button
Using TonPay with React applications
API Reference
Complete API documentation
Troubleshooting
Button not showing
Button not showing
Callback not working
Callback not working
- Ensure the callback function is defined on
windowobject - Function name must match the
callbackparameter exactly - Define the callback before the embed script runs
Import map errors
Import map errors
- Add the import map before any module scripts
- Check that
@tonconnect/uiversion is compatible - Verify import map syntax is correct
CORS errors
CORS errors
- Serve your manifest file from the same domain or enable CORS
- Use HTTPS in production
- Check that CDN resources are accessible