Embed the widget
Add a LayerHop swap card to any site with an iframe, the @layerhop/widget React component, mount() or a script tag, then theme it, listen to its events and earn partner fees.
To embed LayerHop, put an iframe of https://layerhop.com/embed on your page and configure it with URL parameters; no package or API key is needed. The widget is a chromeless swap card covering 71 chains: your users pay from any supported chain and token, sign in their own wallet, and LayerHop charges a 0.15% fee on the widget fee channel.
What can the widget do?#
- Any route LayerHop quotes. Cross-chain and same-chain swaps across EVM chains and Solana, best route first. See Meta-aggregation.
- Your brand. Five presets, every color, radius, font, density, width, shadow, title, logo and button label. See the widget customization reference.
- Your flow. Lock the destination for deposits, allow-list chains and tokens, fix the amount or recipient, hide routes and settings.
- Your revenue. Pass your partner id as
integratorand a registered partner earns its own fee on top of LayerHop's. See Earn fees as a partner. - Your analytics. Typed
postMessageevents for quotes, routes, wallets, submissions and order status.
The parameters and events are a public contract (embed protocol 1.1): they are only ever added to, never changed.
1. Design it in the widget builder#
Open the widget builder. It previews the real widget as you change the theme, chains, tokens and behaviour, and generates ready-to-paste iframe, React, mount() and script-tag code for your settings.
2. Add the iframe#
The iframe works on any site and needs nothing else:
<iframe
id="layerhop"
src="https://layerhop.com/embed?srcChain=base&srcToken=USDC&dstChain=solana&dstToken=SOL&theme=dark"
title="LayerHop swap"
style="width: 100%; max-width: 480px; height: 640px; border: 0; color-scheme: light"
allow="clipboard-write"
></iframe>Note
Keep
color-scheme: lighton the iframe. The widget's page is transparent and its root resolves to the light scheme; the card's own dark or light theme is unaffected. Withcolor-scheme: normal, a page that declares a dark scheme (a<meta name="color-scheme" content="dark">or:root { color-scheme: dark }) makes the iframe dark, and browsers then paint an opaque white box behind the card.@layerhop/widgetsets this for you.
Every parameter, with its range and default, is listed in the widget customization reference. Invalid values are ignored and fall back to defaults, so they never break the card.
3. Or use the @layerhop/widget package#
@layerhop/widget wraps the iframe for you: it validates options (an invalid option throws a LayerHopWidgetError), sets the iframe attributes, filters messages by origin, auto-resizes the iframe and gives you typed callbacks.
Install it with npm install @layerhop/widget.
React (React 18 or later):
import { LayerHopWidget } from "@layerhop/widget/react";
export function Swap() {
return (
<LayerHopWidget
srcChain="base"
dstChain="solana"
dstToken="SOL"
preset="ocean"
theme="auto"
fromChains={["base", "arbitrum"]}
integrator="myapp"
onRoutes={(e) => console.log(e.count, "routes, best:", e.provider)}
onSubmitted={(e) => console.log("submitted", e.srcTxHash)}
onStatus={(e) => console.log(e.state)}
/>
);
}The iframe reloads only when the resulting URL changes; callbacks can change freely. Changing baseUrl, autoResize, height, maxWidth, title or liveUpdates remounts it.
Any framework, with mount():
import { mount } from "@layerhop/widget";
const widget = mount("#swap", {
dstChain: "base",
dstToken: "USDC",
lockDst: true,
onQuote: (q) => console.log(q.provider, q.dstAmount),
});
widget.update({ theme: "light" }); // reloads only if the URL changes
widget.destroy(); // removes the iframe and the listenerScript tag. The dist/embed.js bundle exposes window.LayerHop.mount and mounts every element marked data-layerhop-widget once the DOM is ready:
<div
data-layerhop-widget
data-src-chain="base"
data-dst-chain="solana"
data-preset="lime"
data-from-chains="base,arbitrum"
data-hide-routes
></div>
<script src="/path/to/embed.js"></script>
<script>
document.querySelector("[data-layerhop-widget]")
.addEventListener("layerhop:status", (e) => console.log(e.detail.state));
</script>Every option is a kebab-case data attribute (borderColor → data-border-color); events are re-dispatched as bubbling layerhop:<type> DOM events with the payload in event.detail.
4. Listen to events#
The widget posts { source: "layerhop", type, payload } to your window. Check the origin and the source before you trust a message:
const LAYERHOP_ORIGIN = "https://layerhop.com";
const frame = document.getElementById("layerhop") as HTMLIFrameElement;
window.addEventListener("message", (event) => {
if (event.origin !== LAYERHOP_ORIGIN) return;
const msg = event.data;
if (!msg || msg.source !== "layerhop") return;
switch (msg.type) {
case "resize":
frame.style.height = `${msg.payload.height}px`;
break;
case "submitted":
console.log("submitted on chain", msg.payload.srcChainId, msg.payload.srcTxHash);
break;
case "status":
if (msg.payload.state === "filled") console.log("swap complete");
break;
case "error":
console.warn("LayerHop", msg.payload.code, msg.payload.message);
break;
}
});The full list is ready, quote, routes, submitted, status, error, resize, walletConnected, walletDisconnected and amountChanged; payloads are in the event table. error.code uses the same error codes as the API.
5. Control it from your page (optional)#
To change the amount, tokens, theme or colors without reloading, add allowParentOrigin=<your origin> to the embed URL and post a setParams message to the iframe:
frame.contentWindow!.postMessage(
{ source: "layerhop-host", type: "setParams", payload: { amount: "250", dstToken: "USDT", accent: "38bdf8" } },
"https://layerhop.com",
);The widget accepts the message only from that exact origin and only from its parent window. Locks and allow-lists still apply, and the integrator, recipient, locks and fees can't be changed this way. With the package, pass liveUpdates: true and call widget.update(): it sends setParams for the live keys and reloads only for the rest. Details: setParams contract.
6. Earn partner fees#
Add your partner id to the embed URL (integrator=myapp) or the integrator option. When the id belongs to a registered partner, your fee is added on top of LayerHop's 0.15%. The user sees it in the route's Fees total before signing, and by name ("Partner fee (myapp)") in the fee details. Unknown ids are ignored, so attribution still works before you are approved. Apply for the partner program in the dashboard; see Earn fees as a partner.
The "Powered by LayerHop" link#
Every embed shows a small "Powered by LayerHop" line under the swap. It is an ordinary link to layerhop.com with utm_source=widget, utm_medium=embed and utm_campaign set to your partner id (or, without one, the host of the page that embeds the widget). Partners on a plan with white-label can hide it with hidePoweredBy=1; the widget only honours that after LayerHop confirms the plan. See Plans and limits.
Example: deposit into your app on Base#
The user pays from any supported chain and token; the output always lands as USDC on Base at your deposit address, which they cannot change:
<iframe
src="https://layerhop.com/embed?dstChain=base&dstToken=USDC&lockDst=1&recipient=0xYourDepositAddress&lockRecipient=1&title=Deposit%20to%20Acme&buttonLabel=Deposit&integrator=acme"
title="Deposit to Acme"
style="width: 100%; max-width: 480px; height: 640px; border: 0; color-scheme: light"
allow="clipboard-write"
></iframe>Without lockRecipient=1, a prefilled recipient that differs from the connected wallet is shown with a warning and stays editable.
More recipes#
Same-chain swaps on Solana only, best route only:
https://layerhop.com/embed?fromChains=solana&toChains=solana&srcChain=solana&srcToken=USDC&dstChain=solana&dstToken=SOL&hideRoutes=1Stablecoins in, USDC on Base out:
https://layerhop.com/embed?fromTokens=USDC,USDT&fromChains=ethereum,arbitrum,optimism,polygon&toTokens=base:USDCYour brand on top of a preset:
https://layerhop.com/embed?preset=midnight&accent=ff5c8a&accentText=ffffff&font=manrope&radius=20&radiusInner=12&logo=https://acme.xyz/logo.svg&title=Acme%20Swap&shadow=softJust a "Buy $TOKEN" button for a project: use the buy button script, which opens this widget in a window with your token locked.
Follow the visitor's light or dark setting: add theme=auto; with a preset, the widget switches between the preset's two palettes.
Is the widget safe for my users?#
- Non-custodial. The widget never holds funds or keys; the user's own wallet signs every transaction. See Non-custodial by design.
- No secrets in events. Payloads carry quotes, hashes, states and public addresses only.
- Check origins. Accept messages only from
https://layerhop.comwithsource: "layerhop", and setallowParentOriginonly to your own exact origin. - Fees are visible. LayerHop's fee and any partner fee are shown before the user signs.
Related#
- Buy button for your token: one script tag that opens this widget with your token locked, from a "Buy $TOKEN" button.
- Widget customization reference: every parameter, option and event.
- Deep links: the same parameters on
layerhop.com/swap. - Fees & channels and Earn fees as a partner.
Last updated