Skip to main content

Method

async getTokens(chainIndex: string): Promise<APIResponse<TokenData>>
Retrieves a list of all supported tokens for a specific blockchain network.

Parameters

chainIndex
string
required
Chain identifier (e.g., “1” for Ethereum, “501” for Solana, “137” for Polygon)

Response

code
string
Response code (“0” indicates success)
msg
string
Response message
data
TokenData[]
Array of token information objects

Example

import { OKXClient } from '@okxweb3/okx-api';

const client = new OKXClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id'
});

// Get all tokens on Ethereum
const tokens = await client.dex.getTokens('1');

console.log(`Found ${tokens.data.length} tokens`);

// Find a specific token
const usdc = tokens.data.find(t => t.tokenSymbol === 'USDC');
console.log('USDC:', usdc);

// List all token symbols
const symbols = tokens.data.map(t => t.tokenSymbol);
console.log('Symbols:', symbols);

Response Example

{
  "code": "0",
  "msg": "",
  "data": [
    {
      "decimals": "18",
      "tokenContractAddress": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
      "tokenLogoUrl": "https://static.okx.com/cdn/wallet/logo/eth.png",
      "tokenName": "Ethereum",
      "tokenSymbol": "ETH"
    },
    {
      "decimals": "6",
      "tokenContractAddress": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "tokenLogoUrl": "https://static.okx.com/cdn/wallet/logo/usdc.png",
      "tokenName": "USD Coin",
      "tokenSymbol": "USDC"
    },
    {
      "decimals": "6",
      "tokenContractAddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
      "tokenLogoUrl": "https://static.okx.com/cdn/wallet/logo/usdt.png",
      "tokenName": "Tether USD",
      "tokenSymbol": "USDT"
    }
  ]
}

Supported Chains

The method supports all chains configured in the OKX DEX aggregator:
  • Ethereum (1): Mainnet EVM tokens
  • Solana (501): SPL tokens
  • Polygon (137): Polygon PoS tokens
  • BSC (56): Binance Smart Chain tokens
  • Arbitrum (42161): Arbitrum One tokens
  • Optimism (10): Optimism tokens
  • Base (8453): Base tokens
  • Avalanche (43114): Avalanche C-Chain tokens
  • And many more…

Use Cases

Token Selector UI

const tokens = await client.dex.getTokens('1');

// Build token selector dropdown
const tokenOptions = tokens.data.map(token => ({
  label: `${token.tokenSymbol} - ${token.tokenName}`,
  value: token.tokenContractAddress,
  icon: token.tokenLogoUrl,
  decimals: parseInt(token.decimals)
}));

Token Validation

const tokens = await client.dex.getTokens('1');

function isTokenSupported(address: string): boolean {
  return tokens.data.some(
    t => t.tokenContractAddress.toLowerCase() === address.toLowerCase()
  );
}

function getTokenInfo(address: string) {
  return tokens.data.find(
    t => t.tokenContractAddress.toLowerCase() === address.toLowerCase()
  );
}

Search and Filter

const tokens = await client.dex.getTokens('1');

// Search by symbol or name
function searchTokens(query: string) {
  const q = query.toLowerCase();
  return tokens.data.filter(
    t => t.tokenSymbol.toLowerCase().includes(q) ||
         t.tokenName.toLowerCase().includes(q)
  );
}

const stablecoins = searchTokens('usd');
console.log('Stablecoins:', stablecoins.map(t => t.tokenSymbol));

Caching Recommendations

Token lists don’t change frequently. Consider caching the results:
class TokenCache {
  private cache = new Map<string, { data: TokenData[], timestamp: number }>();
  private TTL = 3600000; // 1 hour

  async getTokens(chainIndex: string): Promise<TokenData[]> {
    const cached = this.cache.get(chainIndex);
    
    if (cached && Date.now() - cached.timestamp < this.TTL) {
      return cached.data;
    }

    const response = await client.dex.getTokens(chainIndex);
    this.cache.set(chainIndex, {
      data: response.data,
      timestamp: Date.now()
    });

    return response.data;
  }
}

Build docs developers (and LLMs) love