Skip to main content
Portal’s Web SDK provides token swapping functionality through the portal.trading.zeroX API. This integration allows you to retrieve swap quotes, inspect available liquidity sources, and execute token swaps using 0x.

Overview

Using the 0x integration, you can:
  • Fetch indicative prices for token swaps
  • Fetch swap quotes between supported tokens
  • Retrieve available liquidity sources
  • Execute swaps by signing and submitting transactions
All swap execution is performed by submitting the transaction data returned by 0x using portal.request.

Prerequisites

Before using the 0x API, make sure you have:
  • A properly initialized Portal client
  • An active wallet with sufficient balance on the source network
    (see Create a wallet)
  • 0x integration enabled in your Portal Dashboard OR have a 0x API Key available

Using a Custom 0x API Key (Optional)

By default, Portal uses the 0x API Key that can be added through the Portal Dashboard to communicate with the 0x integration. If you already have a 0x API key and want to use it (for example, to manage your own rate limits or analytics), you can optionally include it in the request body via the options parameter.
await portal.trading.zeroX.getQuote(request);

Getting a Price (Indicative)

Use portal.trading.zeroX.getPrice to retrieve an indicative price for a token swap without generating executable transaction data. This method is useful for displaying prices, estimating swap outcomes, or building preview experiences without committing to a quote.
async function getZeroXPrice(portal: Portal) {
  const request: ZeroExPriceRequest = {
    chainId: 'eip155:8453', // BASE network
    sellToken: 'ETH',
    buyToken: 'USDC',
    sellAmount: '10000000000000', // 0.00001 ETH
  };

  const response = await portal.trading.zeroX.getPrice(request);
  const price = response.data?.price;
}

Getting a Swap Quote

Use portal.trading.zeroX.getQuote to fetch a swap quote from 0x.
async function getZeroXQuote(portal: Portal) {
  const takerAddress = await portal.getEip155Address();

  const request: ZeroExQuoteRequest = {
    chainId: 'eip155:8453', // BASE network
    sellToken: 'ETH',
    buyToken: 'USDC',
    sellAmount: '10000000000000', // 0.00001 ETH
  };

  const response = await portal.trading.zeroX.getQuote(request);
  const quote = response.data?.quote;

  console.log(quote);
}

Getting Liquidity Sources

You can query available liquidity sources supported by 0x using portal.trading.zeroX.getSources. For full request and response details, refer to the Client API documentation.
async function getZeroXSources(portal: Portal) {
  const response = await portal.trading.zeroX.getSources('eip155:1');

  const sources = response.data?.rawResponse;
}

Executing the Swap

Once you receive a quote containing transaction data, execute the swap by sending the transaction through portal.request.
Note
The transaction data returned by 0x may include gas parameters such as gas or gasPrice. These fields are optional — you can omit them and let Portal estimate gas automatically, or include them if you prefer to use 0x’s suggested values.
async function executeSwap(
  portal: Portal,
  fromAddress: string,
  quoteResponse: ZeroExQuoteResponse,
  chainId: string
) {
  const ethTx: ETHTransactionParam = {
    from: fromAddress,
    to: quoteResponse.data?.quote.transaction.to,
    value: quoteResponse.data?.quote.transaction.value ?? '0x0',
    data: quoteResponse.data?.quote.transaction.data,
  };

  const response = await portal.request({ chainId, method: 'eth_sendTransaction', params: [ethTx] });
}

Supported Networks

The portal.trading.zeroX API supports a predefined set of EIP-155 networks. Requests using unsupported chains will fail.
NetworkEIP-155 Chain ID
Ethereumeip155:1
Optimismeip155:10
BSCeip155:56
Unichaineip155:130
Polygoneip155:137
Worldchaineip155:480
Mantleeip155:5000
Baseeip155:8453
Monad Testneteip155:10143
Modeeip155:34443
Arbitrumeip155:42161
Avalancheeip155:43114
Inkeip155:57073
Lineaeip155:59144
Berachaineip155:80094
Blasteip155:81457
Scrolleip155:534352

Next Steps