接入教程
1. 访问Covalent官网并注册账户以获取免费的API密钥。
2. 在您的项目中安装Covalent SDK或直接通过HTTP请求调用其统一API端点。
3. 使用您获得的API密钥进行身份验证,通过设置`Authorization: Bearer YOUR_API_KEY`请求头。
4. 调用`/v1/{chain_id}/address/{address}/balances_v2/`端点,传入实际的网络ID和钱包地址,以查询指定钱包的代币余额。
5. 调用`/v1/{chain_id}/transaction/{tx_hash}/`端点,通过提供交易哈希来获取特定交易的详细数据。
6. 利用`/v1/{chain_id}/events/topics/{topic}/`端点,根据事件主题日志查询和监控智能合约事件。
7. 根据您的需求组合调用其他端点,例如`/v1/{chain_id}/address/{address}/transactions_v2/`来获取钱包的交易历史记录。
8. 参考官方文档处理响应数据,并将其整合到您的应用中进行区块链数据分析或展示。
使用Covalent API获取以太坊钱包余额
import requests
url = "https://api.covalenthq.com/v1/1/address/YOUR_WALLET_ADDRESS/balances/"
params = {
"key": "YOUR_API_KEY",
"nft": "false",
"no-spam": "true"
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
print(f"Wallet balance fetched successfully.")
# Process data['data']['items'] for token balances
else:
print(f"Error: {response.status_code}")
使用Covalent API查询交易历史
<?php
$apiKey = 'YOUR_API_KEY';
$chainId = 1; // Ethereum
$address = 'YOUR_WALLET_ADDRESS';
$url = "https://api.covalenthq.com/v1/{$chainId}/address/{$address}/transactions_v2/"
. "?key={$apiKey}&page-size=10&page-number=0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
$data = json_decode($response, true);
echo "Transaction history retrieved.";
// Process $data['data']['items'] for transactions
} else {
echo "Failed to fetch data.";
}
?>
通过Covalent API获取代币价格
const axios = require('axios');
const apiKey = 'YOUR_API_KEY';
const chainId = 1;
const contractAddress = 'TOKEN_CONTRACT_ADDRESS';
const url = `https://api.covalenthq.com/v1/pricing/historical_by_addresses_v2/${chainId}/USD/${contractAddress}/`;
axios.get(url, {
params: {
key: apiKey,
'from': '2023-01-01',
'to': '2023-01-31'
}
})
.then(response => {
console.log('Token prices fetched successfully.');
// Process response.data.data for price history
})
.catch(error => {
console.error('Error fetching prices:', error.response?.status || error.message);
});
常见问题
Covalent API是否需要API密钥?
是的,使用Covalent API需要注册并获取API密钥。您可以在Covalent官方网站上注册账户,然后在开发者仪表板中生成密钥。免费套餐通常提供一定额度的请求次数。
Covalent支持哪些区块链网络?
Covalent支持众多主流区块链网络,包括以太坊、Polygon、Avalanche、BNB Chain、Arbitrum、Optimism等。具体支持的链列表和链ID请查阅官方文档。
API请求频率有限制吗?
是的,Covalent API根据您的套餐等级设有请求频率限制(速率限制)。免费套餐通常有每分钟或每秒的请求次数限制。超过限制可能会导致请求被暂时阻止。详细限制信息请查看您的账户配额或官方文档。
Aitishiku.com