Skip to main content
Portal’s React Native 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
    (see 0x Integration) 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.
import { ZeroXQuoteRequest } from '@portal-hq/trading'

const request: ZeroXQuoteRequest = {
  chainId: 'eip155:1',
  sellToken: 'ETH',
  buyToken: 'USDC',
  sellAmount: '100000000000000', // 0.0001 ETH
}

const response = await portal.trading.zeroX.getQuote(request)
console.log('Quote received:', response.data?.rawResponse)

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, {
  zeroXApiKey: 'YOUR_0X_API_KEY',
})


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.
import { ZeroXPriceRequest } from '@portal-hq/trading'

async function getZeroXPrice(portal: Portal) {
  const request: ZeroXPriceRequest = {
    chainId: 'eip155:1',
    sellToken: 'USDT',
    buyToken: 'USDC',
    sellAmount: '100000000000000000000000', // 100,000 USDT in wei
  }

  const response = await portal.trading.zeroX.getPrice(request)

  if (response.error) {
    console.error('ZeroX error:', response.error)
    return
  }

  const price = response.data?.rawResponse
}

Getting a Swap Quote

Use portal.trading.zeroX.getQuote to fetch a swap quote from 0x.
import { ZeroXQuoteRequest, ZeroXTransactionRequest } from '@portal-hq/trading'

async function getZeroXQuote(portal: Portal) {
  const takerAddress = await portal.getAddress('eip155:1')

  const request: ZeroXQuoteRequest = {
    chainId: 'eip155:1',
    sellToken: 'ETH',
    buyToken: 'USDC',
    sellAmount: '1000000000000000000',
  }

  const response = await portal.trading.zeroX.getQuote(request)

  if (response.error) {
    console.error('ZeroX error:', response.error)
    return
  }

  const quote = response.data?.rawResponse

  if (!quote?.transaction) {
    return
  }

  const transaction: ZeroXTransactionRequest = {
    from: takerAddress,
    to: quote.transaction.to,
    value: quote.transaction.value ?? '0x0',
    data: quote.transaction.data,
    gas: quote.transaction.gas,
    gasPrice: quote.transaction.gasPrice,
  }

  return transaction
}

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', {
    zeroXApiKey: 'YOUR_KEY',
  })

  if (response.error) {
    console.error('ZeroX error:', response.error)
    return
  }

  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.
import { ETHTransactionParam } from '@portal-hq/provider'
import { ZeroXTransactionRequest } from '@portal-hq/trading'

async function executeSwap(
  portal: Portal,
  transaction: ZeroXTransactionRequest,
  chainId: string
) {
  const ethTx: ETHTransactionParam = {
    from: transaction.from,
    to: transaction.to,
    value: transaction.value ?? '0x0',
    data: transaction.data,
    gas: transaction.gas,
    gasPrice: transaction.gasPrice,
  }

  const response = await portal.request(
    chainId,
    'eth_sendTransaction',
    [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
Ethereum Mainneteip155:1
Optimismeip155:10
BNB Smart Chaineip155:56
Polygoneip155:137
Baseeip155:8453
Arbitrum Oneeip155:42161
Avalanche C-Chaineip155:43114
Scrolleip155:534352
Lineaeip155:59144

Next Steps