Published: 07 Nov 2024 › Updated: 07 Nov 2024
CoinGecko API查询币价 / nuxt#13
最近要在前端中集成$AIJoe的充值功能,少不了得实时查询价格。这里要用到两个价格:AIJoe/ETH和
ETH/USD的价格。AIJoe/ETH可以从合约中查到,ETH/USD就要借助工具了,这里选了CoinGecko API。
https://www.coingecko.com/en/developers/dashboard
CoinGecko API 提供免费和付费两种计划。所有 CoinGecko 用户均可免费使用演示版 API 计划,通话费率限制为 30 次/分钟,每月通话上限为 10,000 次。付费计划的起价为 129 美元/月,提供更高的通话费率限制(500 次/分钟)和每月 500,000 次的通话上限。免费版的应该够用了!
案例代码如下:
import fetch from "node-fetch"
const url = 'https://api.coingecko.com/api/v3/coins/id';
const options = {method: 'GET', headers: {accept: 'application/json'}};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error(err));
eg:
async function getBitcoinPrice (){
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/bitcoin`,
{
headers: {
'x-cg-demo-api-key': 'xxxx', //这里填入你的api key
'Content-Type': 'application/json',
'Accept': 'application/json',
},
method: "GET",
})
console.log(265, "response:", response)
let res = await response.json()
console.log(699, "res:", res)
}
//这里查询到的结果稍有点乱,建议采用以下方法
//用sample 直接查询币价
//https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd
async function getEthereumPrice (){
const response = await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd`,
{
headers: {
'x-cg-demo-api-key': 'xxxx',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
method: "GET",
})
let res = await response.json()
console.log(699, "res:", res) //699 res: { ethereum: { usd: 2823.98 } }
let c = res.ethereum.usd
console.log(988, "price:", c) //2823.98
}
//这个方法简单直接地就输出了结果,非常好!
//查询代币价格
https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&vs_currencies=usd
CoinGecko的功能还蛮多的,可以自己建个查询网站或是集成进应用,可以方便很多。
Leave CoinGecko API查询币价 / nuxt#13 to:
Read more #cn posts
Best Posts From lemooljiang
We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.