- Fetch a quote from a trading integration on your
Portalinstance —portal.trading.zeroXfor same-chain swaps, orportal.trading.lififor cross-chain swaps and bridges. - Execute the quote’s transaction — either with
portal.request(...), or with theexecute()helper in the@portal-hq/swapspackage.
The legacy quote and liquidity-source methods on
@portal-hq/swaps — swaps.getQuote() and swaps.getSources(), along with the QuoteArgs and QuoteResponse types — have been removed. Use portal.trading.zeroX.getQuote() and portal.trading.zeroX.getSources() instead. See the 0x guide for the full quote, price, and liquidity-source API.Installing Swaps support
The@portal-hq/swaps package is only needed for its execute() helper. If you send the swap transaction with portal.request(...) directly, you can skip this step.
Run the following in your project directory.
- yarn
- npm
Initializing Swaps
InitializingSwaps requires an existing Portal instance.
The constructor still accepts a first
apiKey argument so that existing new Swaps(apiKey, portal) call sites keep compiling, but the SDK no longer uses it — you can pass an empty string. The parameter will be removed in the next major version.Quotes are authenticated with the Portal client API key you already pass to the Portal constructor, and 0x credentials are configured in the Portal Dashboard (see the 0x integration guide).Fetching a swap quote
Useportal.trading.zeroX.getQuote() to fetch an executable quote. The response wraps the quote in data.rawResponse, whose transaction field holds the transaction to broadcast.
portal.trading.zeroX.getSources('eip155:1'). Both methods are documented in full in the 0x guide.
Executing a swap
If you’re selling ERC20 tokens, the token must have an allowance for the 0x spender contract of at least the amount you’re selling before the swap transaction will succeed. If you’re selling the chain’s native asset (for exampleETH), this step is not necessary.
When you’re ready to execute the swap, send the quote’s transaction with the Provider on your existing Portal instance.
execute() sends an eth_sendTransaction request through the provider and resolves with the transaction hash.
You can send the same transaction without the @portal-hq/swaps package by calling portal.request(...) directly, which also lets you pass request options such as a signature approval memo.
Next steps
- Swap tokens with 0x — quotes, indicative prices, liquidity sources, and the high-level
tradeAsset()flow - Swap and bridge tokens with Li.Fi — cross-chain routes