Sticking to Uniswap SDK terms, a "pair price" strictly speaking does not exist. The closest thing to it is the MidPrice of a given Route.
However, your screenshot indicates that what you're really looking for is the ExecutionPrice of a Trade:
import { ChainId, Token, WETH, Fetcher, Trade, Route, TokenAmount, TradeType } from '@uniswap/sdk'
const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)
const pair = await Fetcher.fetchPairData(DAI, WETH[DAI.chainId])
const route = new Route([pair], WETH[DAI.chainId])
const trade = new Trade(route, new TokenAmount(WETH[DAI.chainId], '1000000000000000000'), TradeType.EXACT_INPUT)
console.log(trade.executionPrice.toSignificant(6))
You'll first need the address of the contract for the pair whose price(s) you want to access.
Two ways to do this:
Find the addresses of the two tokens separately, and feed them into the UniswapV2Factory
contract to get the address of the pair's contract; or...
Go to Uniswap, enter the two tokens into the usual interface, then visit the "View Pair Analytics" page, at the bottom of which will be the address of their contract and a link to Etherscan.
Once you know the pair's contract address, you can access the prices directly via Web3. Inside the pair's contract - of type UniswapV2Pair
- there are the following variables:
...
address public factory;
address public token0;
address public token1;
uint112 private reserve0; // uses single storage slot, accessible via getReserves
uint112 private reserve1; // uses single storage slot, accessible via getReserves
uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves
uint public price0CumulativeLast;
uint public price1CumulativeLast;
...
The final two represent the token prices (sort of... see below). They're public, so getter functions will automagically be available.
However... that's not the end of the story.
The prices in the contracts are cumulative prices, not the last spot price. That means you'll have to do some maths to make those values useful.
The Uniswap Oracles page has some details on how you can calculate a time-weighted average price (TWAP) suitable for your needs.
Best Answer
Pancake Swap uses the same formula as Uniswap, but they have a 0.2% fee instead of 0.3% fee, which affects the calculation.