Skip to content
LayerHop

LayerHop for developers · JavaScript SDK

The JavaScript and TypeScript SDK for cross-chain swaps

@layerhop/sdk is the TypeScript client for the LayerHop API. It has no runtime dependencies and works in Node, browsers and edge runtimes. Create a LayerHop client, call quote(), buildTx() and reportSubmitted(), and iterate watch() until the order is final; the SDK returns unsigned transactions and your wallet signs them.

Install
npm install @layerhop/sdk

How to use the SDK

  1. 1

    Install and create a client

    npm install @layerhop/sdk, then new LayerHop({ apiKey, channel: "api" }). The API key is optional; baseUrl defaults to the hosted API.

  2. 2

    Get a quote

    lh.quote({ srcChain, srcToken, amount, dstChain, dstToken, sender, recipient }) returns { best, quotes, errors }. Amounts are base-unit strings.

  3. 3

    Build the transaction

    lh.buildTx(best.quoteId) returns an EVM or Solana transaction; lh.simulate(quoteId) dry-runs it first.

  4. 4

    Have the user's wallet sign and send it

    For an EVM chain, send tx.approval first when it is present, then the main transaction (to, data, value) on tx.chainId. For Solana, sign the base64 VersionedTransaction; when submitViaApi is true, send the signed bytes through LayerHop instead of an RPC node.

  5. 5

    Report and track

    lh.reportSubmitted(quoteId, txHash), then for await (const s of lh.watch(provider, txHash)) until the state is final.

Code

Copy, then replace the addresses and the key.

Quote, build, report and track
import { LayerHop } from "@layerhop/sdk";

const lh = new LayerHop({
  apiKey: process.env.LAYERHOP_API_KEY, // optional, sent as x-api-key
  channel: "api",                       // fee channel; baseUrl defaults to https://api.layerhop.com
});

// 1. Quote: 100 USDC on Base -> SOL on Solana (amounts are base-unit strings)
const { best, quotes, errors } = await lh.quote({
  srcChain: "base", srcToken: "USDC", amount: "100000000",
  dstChain: "solana", dstToken: "SOL",
  sender: "0x1111111111111111111111111111111111111111",      // pays on Base
  recipient: "9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM", // receives on Solana
  slippageBps: 50,
});
console.log(best.provider, best.dstAmount, best.dstAmountMin, best.estimatedSeconds);
console.log("LayerHop fee (bps):", best.fees.ourFeeBps);

// 2. Build the unsigned transaction (right after the user confirms)
const tx = await lh.buildTx(best.quoteId);

// 3. Optional dry run before asking for a signature
await lh.simulate(best.quoteId);

// 4. The wallet signs and sends tx (see the quickstart for viem and Solana), then:
await lh.reportSubmitted(best.quoteId, srcTxHash);

// 5. Track until the order is final
for await (const status of lh.watch(best.provider, srcTxHash)) {
  console.log(status.state, status.dstTxHash ?? ""); // pending -> filled / refunded / failed
}
Sign and send on EVM with viem
import { createWalletClient, createPublicClient, custom, http, erc20Abi } from "viem";
import { base } from "viem/chains";

const wallet = createWalletClient({ chain: base, transport: custom(window.ethereum!) });
const pub = createPublicClient({ chain: base, transport: http() });
const [account] = await wallet.requestAddresses();

if (tx.vm === "evm") {
  if (tx.approval) {
    const { token, spender, amount } = tx.approval;
    const allowance = await pub.readContract({
      address: token as `0x${string}`, abi: erc20Abi, functionName: "allowance",
      args: [account!, spender as `0x${string}`],
    });
    if (allowance < BigInt(amount)) {
      const approveHash = await wallet.writeContract({
        account: account!, address: token as `0x${string}`, abi: erc20Abi,
        functionName: "approve", args: [spender as `0x${string}`, BigInt(amount)],
      });
      await pub.waitForTransactionReceipt({ hash: approveHash });
    }
  }
  srcTxHash = await wallet.sendTransaction({
    account: account!, to: tx.to as `0x${string}`, data: tx.data as `0x${string}`, value: BigInt(tx.value),
  });
}

What works today, and what doesn't

So you know before you build.

Works today

  • Every public API call, typed: quotes (also exact output and live streams), orders, tracking, checkout and more
  • React hooks (@layerhop/react) and wallet packages for wagmi and Solana are separate packages that build on it
  • A CLI and a local mock API for development without funds: npx @layerhop/cli dev
  • Works without an API key under the per-IP rate limit

Good to know

  • It never signs or holds keys: signing is your wallet library's job
  • A quote expires within about 30 to 60 seconds; build right after the user confirms
  • The providers are mainnet only; use an lh_test_ key or the local mock API to develop without real funds

Questions

Do I need an API key?

Not to try it: requests without a key work under a per-IP rate limit. For production, sign in to the dashboard with a wallet and create a key (lh_live_…, sent in the x-api-key header). A key gets its own rate limit, shows your usage and logs, and lets your orders trigger webhooks. lh_test_ keys give sandbox quotes and simulated orders.

What does it cost?

LayerHop's fee on a trade made through the API channel is 0.10% of the input amount, reported on every quote as fees.ourFeeBps next to the provider's fees and gas. Partners can add their own fee on top.

Does it hold keys or sign transactions?

No. It returns unsigned transactions and your own wallet library (viem, ethers, web3.py, go-ethereum, alloy, a Solana wallet, a KMS) signs and broadcasts them. LayerHop never holds funds or keys.

Does it work in the browser?

Yes: it has no runtime dependencies and works in Node, browsers and edge runtimes.

Is there a React version?

Yes, @layerhop/react provides hooks on top of the SDK, and there is a React Native package. They are documented in the docs.