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

# getSwapTokens

> List the tokens your users can swap on a network, or search for one by name, symbol, or address.

## Overview

`getSwapTokens` returns the tokens that can be swapped on one network. Without a search, it returns a curated list of popular tokens for that network, such as the native token and major stablecoins. With a search, it returns only the tokens that match, with curated tokens first.

Use the result to build a token picker, then pass the chosen token's `address` to [`getSwapQuote`](https://www.dynamic.xyz/docs/javascript/reference/client/get-swap-quote). A listed token can be swapped on that network, but a specific pair is not guaranteed: the quote is the final check, because routes depend on liquidity and amount.

## Usage

```javascript theme={"system"}
import { getSwapTokens } from '@dynamic-labs-sdk/client';

const { tokens } = await getSwapTokens({
  chain: 'EVM',
  networkId: '8453', // Base
});

console.log(tokens.map((token) => token.symbol));
```

## Parameters

| Parameter | Type | Description |
| - | - | - |
| `chain` | `Chain` | The chain to list tokens for (for example, `'EVM'` or `'SOL'`). |
| `networkId` | `string` | The network to list tokens for (for example, `'1'` for Ethereum or `'137'` for Polygon). |
| `search` | `string` (optional) | Text to look for in token names, symbols, or addresses. When set, only matching tokens are returned, curated ones first. |
| `client` | `DynamicClient` (optional) | The Dynamic client instance. Only required when using multiple clients. |

Use [`getSwapChains`](https://www.dynamic.xyz/docs/javascript/reference/client/get-swap-chains) to find the `chain` and `networkId` values that support swaps.

## Returns

`Promise<SwapTokensResponse>`: an object with a `tokens` array. Each entry is a `SwapCatalogToken`:

| Field | Type | Description |
| - | - | - |
| `address` | `string` | The token address to pass to `getSwapQuote`. |
| `chain` | `Chain` | The chain the token is on. |
| `networkId` | `string` | The network the token is on. |
| `symbol` | `string` | The token symbol, such as `'USDC'`. |
| `name` | `string` | The token name, such as `'USD Coin'`. |
| `decimals` | `number` | The number of decimals, used to convert amounts to the smallest unit. |
| `logoURI` | `string` (optional) | A URL for the token's logo. |
| `priceUSD` | `string` (optional) | An indicative USD price. Use the quote for the actual price. |
| `coinKey` | `string` (optional) | A short key identifying the asset across networks, such as `'USDC'`. |

`tokens` is an empty array when nothing matches the search, when the network has no swappable tokens, or when no swap provider is available for your environment or the user's region.

## Errors

| Error | When |
| - | - |
| `SwapTokensUnavailableError` | The token list is temporarily unavailable. Retrying later usually works. |

Other failures keep their original error. These include an invalid `chain`, a `search` longer than 256 characters, and network timeouts.

```javascript theme={"system"}
import {
  getSwapTokens,
  SwapTokensUnavailableError,
} from '@dynamic-labs-sdk/client';

try {
  const { tokens } = await getSwapTokens({ chain: 'EVM', networkId: '1' });
} catch (error) {
  if (error instanceof SwapTokensUnavailableError) {
    // Ask the user to try again later.
  }

  throw error;
}
```

## Examples

### Search for a token

Search by symbol, name, or a pasted token address. Debounce the search in your UI, so a request is not sent on every keystroke.

```javascript theme={"system"}
import { getSwapTokens } from '@dynamic-labs-sdk/client';

const { tokens } = await getSwapTokens({
  chain: 'EVM',
  networkId: '1',
  search: 'usd',
});
```

### Convert an amount for the quote

`getSwapQuote` takes amounts in the token's smallest unit, so use the token's `decimals` to convert the user's input.

```javascript theme={"system"}
import { getSwapQuote } from '@dynamic-labs-sdk/client';

const quoteSwap = async ({ walletAccount, fromToken, toToken, amount }) => {
  // For example, '1.5' with 6 decimals becomes '1500000'.
  const [whole, fraction = ''] = amount.split('.');
  const smallestUnit = BigInt(
    whole + fraction.padEnd(fromToken.decimals, '0').slice(0, fromToken.decimals)
  ).toString();

  return getSwapQuote({
    from: {
      address: walletAccount.address,
      amount: smallestUnit,
      chain: fromToken.chain,
      networkId: fromToken.networkId,
      tokenAddress: fromToken.address,
    },
    to: {
      address: walletAccount.address,
      chain: toToken.chain,
      networkId: toToken.networkId,
      tokenAddress: toToken.address,
    },
  });
};
```

## React

`useGetSwapTokens` returns the same list in `data`. It stays disabled until both `chain` and `networkId` are defined, so you can pass them before the user picks a network.

```tsx theme={"system"}
import { SwapTokensUnavailableError } from '@dynamic-labs-sdk/client';
import { useGetSwapTokens } from '@dynamic-labs-sdk/react-hooks';

function SwapTokenList({ chain, networkId, search }) {
  const { data, isLoading, error, refetch } = useGetSwapTokens({
    chain,
    networkId,
    search,
  });

  if (isLoading) {
    return <p>Loading tokens...</p>;
  }

  if (error instanceof SwapTokensUnavailableError) {
    return <button onClick={() => refetch()}>Tokens are unavailable. Retry</button>;
  }

  if (data?.tokens.length === 0) {
    return <p>No tokens found.</p>;
  }

  return (
    <ul>
      {data?.tokens.map((token) => (
        <li key={token.address}>
          {token.name} ({token.symbol})
        </li>
      ))}
    </ul>
  );
}
```

## Related

* [`getSwapChains`](https://www.dynamic.xyz/docs/javascript/reference/client/get-swap-chains): list the networks that support swaps
* [`getSwapQuote`](https://www.dynamic.xyz/docs/javascript/reference/client/get-swap-quote): quote a swap between two tokens
* [Building a swap UI](https://www.dynamic.xyz/docs/javascript/building-ui/swap-ui.md): a full swap flow


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.