LayerHop for developers · Go SDK
The Go SDK for cross-chain swaps
layerhop-go is the Go client for the LayerHop API. It uses only the standard library, needs Go 1.23 or newer, and every method takes a context.Context. Call Quote, BuildTx and ReportSubmitted, then range over Watch, which returns an iter.Seq2; the SDK returns unsigned transactions and your wallet library signs them.
go get github.com/layerhop/layerhop-go@latestHow to use the SDK
- 1
Install and create a client
go get the module, then layerhop.New(layerhop.Options{APIKey: ...}). The key is optional; Channel defaults to api.
- 2
Get a quote
lh.Quote(ctx, layerhop.QuoteParams{...}) returns the response with Best and Quotes. Amounts are base-unit strings.
- 3
Build the unsigned transaction
lh.BuildTx(ctx, q.Best.QuoteID) returns the transaction for the quote.
- 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
Report and track
lh.ReportSubmitted(ctx, quoteID, txHash), then range over lh.Watch(ctx, provider, txHash, layerhop.WatchOptions{}) until the order is final or it times out.
Code
Copy, then replace the addresses and the key.
lh := layerhop.New(layerhop.Options{APIKey: os.Getenv("LAYERHOP_API_KEY")})
// 1. Quote: 100 USDC on Base -> SOL on Solana (amounts are base-unit strings)
q, err := lh.Quote(ctx, layerhop.QuoteParams{
SrcChain: "base", SrcToken: "USDC", Amount: "100000000",
DstChain: "solana", DstToken: "SOL",
Sender: "0xYourEvmAddress", Recipient: "YourSolanaAddress",
})
if err != nil { return err }
// 2. Build the unsigned transaction
tx, err := lh.BuildTx(ctx, q.Best.QuoteID)
if err != nil { return err }
// 3. Sign and send with YOUR wallet library. EVM: send tx.Approval first when set, then
// {To: tx.To, Data: tx.Data, Value: tx.Value} on tx.ChainID. Solana: sign tx.SerializedTx;
// when tx.SubmitViaAPI is true, call lh.Submit(ctx, quoteID, signedBase64) instead of an RPC.
txHash := signAndSend(tx)
// 4. Report the broadcast
if _, err := lh.ReportSubmitted(ctx, q.Best.QuoteID, txHash); err != nil { return err }
// 5. Watch until filled / refunded / failed
for st, err := range lh.Watch(ctx, q.Best.Provider, txHash, layerhop.WatchOptions{}) {
if err != nil { return err }
fmt.Println(st.State)
}What works today, and what doesn't
So you know before you build.
Works today
- Typed models and a method for every public endpoint: quotes, orders, tracking, checkout, tokens, prices, balances
- Watch as a range-over-func iterator, with ErrWatchTimeout after 30 minutes by default
- Standard library only, with webhook signature verification
Good to know
- It never signs: use go-ethereum, solana-go or a KMS for that
- Quotes expire within about 30 to 60 seconds
- Providers are mainnet only; use an lh_test_ key 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.
Which Go version do I need?
Go 1.23 or newer, because Watch returns an iter.Seq2.
Does it have dependencies?
No, only the standard library.