This article is for anyone who needs to get accurate cryptocurrency prices directly from decentralized exchanges (DEXs). Whether you are tracking your portfolio, analyzing market trends, or building crypto applications, reliable DEX price data is essential for making informed decisions.
There are several traditional methods that allow one to get crypto/token prices from DEXs:
- Querying DEX subgraphs (e.g. Uniswap via The Graph)
- Calling DEX smart contracts directly (via RPC/archive node)
- Using DEX-specific SDKs
Limitations of Traditional Methods for Pulling Crypto Prices From DEXs
Traditional approaches come with significant limitations. Each DEX requires a separate, protocol-specific integration. While some methods offer partial USD conversion through stable pair routing, you often need to write custom logic to ensure accurate pricing. This complexity multiplies as you add more chains and DEXs, leaving you to manage a tangled web of RPC nodes, subgraph endpoints, and multiple SDKs, which increases both development effort and infrastructure costs.
What Is the Best Way to Pull Crypto Prices From DEXs?
The best way to pull crypto prices from DEXs is by using CoinGecko’s on-chain API. It offers a unified solution for retrieving DEX price data from over 1,600 DEXs across 200+ networks and is always expanding its coverage. This includes popular DEXs like Uniswap and PancakeSwap, as well as rapidly emerging memecoin launchpads like Pump.fun and LetsBonkFun, ensuring you only need one API to get the latest on-chain data.
Quick Comparison: CoinGecko API vs Traditional Methods
Feature | CoinGecko API | Traditional Methods (Subgraphs, RPC, DEX SDKs) |
---|---|---|
DEX & Chain Support | Unified access to 1,600+ DEXs across 200+ chains. | Protocol-specific; requires separate integration for each. |
Price Conversion | Provides direct price conversion against 45+ fiat currencies (e.g., USD, EUR, JPY) and 15+ crypto assets (e.g., BTC, ETH, SOL). | Manual conversion logic required via stablecoin pairs. |
Integration Effort | Fast; single API integration. | Slower; requires managing multiple endpoints & libraries. |
Scalability | Seamless; CoinGecko manages integrations with latest DEXs and networks. | High effort and cost to add new DEXs and chains. |
Prerequisites
To follow this guide, you’ll need a CoinGecko API Key. If you don’t have one, read this guide on how to get your free Demo API key. While most examples in this article use a free Demo API key, some require exclusive endpoints that are only available with a paid API plan.
You’ll also need Python 3.7+ installed on your system, and basic Python knowledge with familiarity using the requests library and handling JSON.
Install required dependencies
pip install requests python-dotenv matplotlib pandas numpy
How to Find DEX Pool Addresses
To get price data for a specific pool, you first need its address. You can use CoinGecko’s API to search for pools by name or discover top pools on a specific network or DEX. The API also returns USD price data for both base and quote tokens by default, which may be all you need for many use cases.
Search for a Pool
To search for a pool, use the /onchain/search/pools API endpoint. This endpoint is useful when you know the name of the pool but don’t know the exact pool address.
The code above searches for USDC pools on Ethereum, loops through the first 3 results, and prints each pool’s name, address, and DEX information.
Example Output
Get Top Pools of a Network
To get the top pools of a specified network, use the /onchain/networks/{network}/pools API endpoint. This endpoint will return the top pools by transaction volume in the last 24 hours of the specified network.
The code above fetches the top 5 pools on Ethereum and displays their names, liquidity values, DEX names, and 24-hour trading volumes.
Example Output
Get Top Pools of a DEX
We can use the /onchain/networks/{network}/dexes/{dex}/pools to get the top pools from a specific DEX like Uniswap or PancakeSwap.
This code fetches the first page of pools from the Uniswap V3 DEX on Ethereum. It displays the names, addresses, fee percentages, and total value locked (TVL) in USD for the first 3 pools returned.
Example Output
Note: If you don’t see data for newer pools, try another network or timeframe as on-chain coverage and data freshness can vary.
Other Pool Discovery Methods
CoinGecko’s On-chain API also offers several other endpoints for comprehensive pool discovery:
- Trending Pools: /onchain/networks/{network}/trending_pools
- New Pools: /onchain/networks/{network}/new_pools
- Pool Filters: /onchain/pools/megafilter (exclusive endpoint for paid API plans)
How to Pull Current & Historical Token Price Data From DEXs
You can use CoinGecko’s on-chain API to fetch real-time and historical token price data. You can get token price data in two main ways: by using the token’s address or by querying a specific pool address.
Using a token address is the most convenient method. The API automatically finds the most liquid pool for that token and returns its price, so you don’t need to know which specific DEX pool to query. This section covers the token address method.
The following section will cover how to get price data by querying a specific DEX pool address.
Current Token Prices by Token Address
The /onchain/simple/networks/{network}/token_price/{addresses} endpoint is the most straightforward way to get current real-time prices for one or more tokens in a single API call. It aggregates data from multiple DEXs on a given network to provide an accurate market price based on liquidity and pool activity, saving you from having to query individual DEX pools.
The response includes current USD prices, 24-hour volume, and price changes for all requested tokens on the specified network.
Example Output
Common use cases include:
- Portfolio tracking: Monitor multiple token prices in real-time for investment management
- Price alerts: Set up notifications when tokens hit target prices for trading decisions
- Trading bots: Get reliable price feeds for automated trading strategies
Historical Token Prices by Token Address
The /onchain/networks/{network}/tokens/{token_address}/ohlcv/{timeframe} endpoint can be used to retrieve historical OHLCV data using just a token’s contract address. However, this is an exclusive endpoint available only on paid API plans. This quality-of-life endpoint simplifies the data retrieval process by automatically providing price data from the token’s most liquid pool without the need of specifying a pool’s contract address.
For free Demo API users, the same historical data can be obtained by using the historical pool data endpoint, which requires a specific pool address. This alternative method is covered in the next section.
Example Output
Common use cases include:
- Technical analysis: Building indicators like moving averages, RSI, and MACD for trading decisions
- Backtesting strategies: Testing trading strategies against historical price movements
- Risk assessment: Analyzing volatility patterns and price correlations for portfolio management
Building a Token Price Chart
Visualizing historical data is a great way to understand a token’s price action. The following build_token_chart Python function takes the OHLCV data we fetched, organizes it into a pandas DataFrame, and then uses matplotlib to plot the closing price against the date. The resulting chart is saved as a PNG image, providing a clear view of the token’s price trend over the selected period.
Example Chart Output
How to Pull Current & Historical Token Price Data From DEX Pools
You can use CoinGecko’s On-chain API to get both current and historical price data for a specific DEX pool by providing its unique contract address.
While fetching prices by token address offers convenience, this pool-based method gives you more granular control. It is ideal when you need to analyze a token’s performance on a particular DEX. For free Demo API users, the historical endpoint in this section is the alternative for getting OHLCV data.
Current Pool Data by Pool Address
The /onchain/networks/{network}/pools/{address} endpoint can be used to get detailed real-time price and volume data for a single liquidity pool by specifying a unique pool address.
Example Output
This endpoint returns detailed pool information including base/quote tokens, current price, liquidity, and trading volume.
Historical Pool Data by Pool Address
The /onchain/networks/{network}/pools/{pool_address}/ohlcv/{timeframe} endpoint can be used to retrieve historical OHLCV data for a specific pool address and is available on all API plans, including the free Demo plan. It serves as the alternative for Demo API users to fetch historical token prices from a DEX.
Example Output
Building a Pool Price Chart
With historical pool data, you can create more advanced visualizations. The build_pool_chart function below uses pandas and matplotlib to plot both the pool’s price and its trading volume on a single chart and saves it as a PNG image, offering a more complete picture of its market activity.
Example Chart Output
Summary and Conclusion
In this guide, we demonstrated how CoinGecko’s on-chain API simplifies the process of fetching DEX price data. We walked through discovering pool and token addresses, pulling current prices with built-in USD conversion, and accessing historical OHLCV data to build price charts.
Unlike traditional methods that require protocol-specific integrations, the CoinGecko API provides unified access to thousands of DEXs and offers a scalable architecture that grows automatically with new chains and DEXs.
To access exclusive endpoints like historical token data by token address, and to benefit from higher rate limits, consider subscribing to a paid API plan.