Embed the Swap to BSV widget in your application
Option 1: Redirect users to this page in a new tab
Option 2: Embed the widget in your app using an iframe with postMessage communication
Open the swap portal in a new tab with pre-filled parameters:
const swapUrl = new URL('https://swap.sendbsv.com/swap');
swapUrl.searchParams.set('bsvAddress', userWalletAddress);
swapUrl.searchParams.set('targetUsd', '10.00');
swapUrl.searchParams.set('context', 'Payment for service');
swapUrl.searchParams.set('returnUrl', window.location.href);
swapUrl.searchParams.set('state', sessionToken);
window.open(swapUrl.toString(), '_blank');bsvAddress - Required. User's BSV receiving addresstargetUsd - Optional. Target amount in USDcontext - Optional. Description shown to userreturnUrl - Optional. Where to send user after swapstate - Optional. Token echoed back to your apptheme - Optional. "light" or "dark"Embed the widget using an iframe with secure postMessage communication:
// Create iframe
const iframe = document.createElement('iframe');
const embedUrl = new URL('https://swap.sendbsv.com/embed');
embedUrl.searchParams.set('bsvAddress', userWalletAddress);
embedUrl.searchParams.set('targetUsd', '10.00');
embedUrl.searchParams.set('parentOrigin', window.location.origin);
embedUrl.searchParams.set('state', sessionToken);
iframe.src = embedUrl.toString();
iframe.width = '100%';
iframe.height = '600px';
iframe.style.border = 'none';
document.getElementById('swap-container').appendChild(iframe);
// Listen for messages
window.addEventListener('message', (event) => {
// Validate origin
if (event.origin !== 'https://swap.sendbsv.com') return;
if (event.data.channel !== 'SWAP_PORTAL_V1') return;
switch (event.data.type) {
case 'READY':
console.log('Widget ready');
break;
case 'CLOSE_REQUESTED':
// User clicked close
closeModal();
break;
case 'CONTINUE_REQUESTED':
// User indicated swap complete
closeModal();
checkWalletBalance();
break;
}
});event.origin matches the portal domainevent.data.channel === 'SWAP_PORTAL_V1'state matches your session tokenparentOrigin in productionREADY - Widget loaded and readyCLOSE_REQUESTED - User clicked close/cancelCONTINUE_REQUESTED - User indicated swap completeSee the embedded integration in action:
View Live Demo →