> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portalhq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap Tokens with 0x

> Learn how to swap tokens using Portal's Web SDK with 0x integration

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](./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 have a 0x API key that you want to test with locally, you can optionally include it in the request body via the options parameter.

```ts theme={null}
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.

```ts theme={null}
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.

```ts theme={null}
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.

```ts theme={null}
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.

```ts theme={null}
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.

| Network       | EIP-155 Chain ID |
| ------------- | ---------------- |
| Ethereum      | `eip155:1`       |
| Optimism      | `eip155:10`      |
| BSC           | `eip155:56`      |
| Unichain      | `eip155:130`     |
| Polygon       | `eip155:137`     |
| Worldchain    | `eip155:480`     |
| Mantle        | `eip155:5000`    |
| Base          | `eip155:8453`    |
| Monad Testnet | `eip155:10143`   |
| Mode          | `eip155:34443`   |
| Arbitrum      | `eip155:42161`   |
| Avalanche     | `eip155:43114`   |
| Ink           | `eip155:57073`   |
| Linea         | `eip155:59144`   |
| Berachain     | `eip155:80094`   |
| Blast         | `eip155:81457`   |
| Scroll        | `eip155:534352`  |

***

## Next Steps

* Learn how to [sign Ethereum transactions](./sign-a-transaction)
* Explore how to [send tokens](./send-tokens)
