I have a simple front end, and when the user inputs an amount , on blur i want to show the amount of token B they will be buying with the input amount.
for that I'm trying to use 0x price api
const handleBlur = async () => {
/**@DEV on blur will use the 0x api to fetch token price */
if(amount1 !== 0 && selectedToken1 !== "" && selectedToken2!== ""){
const params = {
chainId:8453,
buyToken: selectedToken2.address,
sellToken:selectedToken1.address,
sellAmount:amount1
}
const headers = {'0x-api-key': `${process.env.ZERO_X_API_KEY}`};
const resp = await fetch(`https://api.0x.org/swap/permit2/price?${qs.stringify(params)}`,{headers});
console.log(resp.json);
}
}
This is the function that handles the fetch request to the 0x API. also here you can find the documentation of the API and its params https://0x.org/docs/api