Uniswap API – How to Query for Pair ID of a Chosen Token

pairingspriceswapsuniswap

I am using the Uniswap v2 api subgraph but I am running into a bit of an issue that I cannot resolve.

Basically I want to query the api to get the latest token swaps of a chosen token and Eth. Using Eth's current price I can accurately get a real-time price of the given token.

But in order to make pair information requests to the api you need to know the pair ID to begin with.

How can I possibly get the pair ID if the pair ID is not stored or apart of the chosen token's id? I can dynamically get the token's ID from another API and I can use that to query info from Uniswap's api however there seems to be no way to get the actual pairs ID in order to determine the latest swaps.

Here is a sandbox of Uniswaps subgraph API in case anyone wants to try and see if they can pull a coin's pair ID address.

https://thegraph.com/explorer/subgraph/uniswap/uniswap-v2

Edit: to be clear I can get the Pair ID addresses manually by going to uniswap.info however this is not what I want to do. I want to be able to dynamically search for any erc-20 token and then use that token to pull the latest swaps using the pair ID.

Best Answer

You can write a query to filter all the liquidity pairs on Uniswap and get the pair address

    {  pairs (where :{token0 : "0x0f7f961648ae6db43c75663ac7e5414eb79b5704", token1 :"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}) { // token1 is the ethereum address
    id
    createdAtTimestamp
    volumeUSD
    //any other field you want
  }
}
Related Topic