接入教程
1. 访问Bitquery官网注册账户
2. 在GraphQL IDE中编写查询语句
3. 选择目标区块链网络
4. 执行查询获取链上数据
5. 将API集成到您的应用中
使用Python查询以太坊交易数据
python
import requests
import json
url = 'https://graphql.bitquery.io/'
headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'YOUR_API_KEY'
}
query = '''
{
ethereum {
transactions(limit: 5) {
hash
block {
timestamp
}
sender {
address
}
}
}
}
'''
response = requests.post(url, json={'query': query}, headers=headers)
data = response.json()
print(json.dumps(data, indent=2))
使用PHP获取代币交易信息
php
<?php
$url = 'https://graphql.bitquery.io/';
$headers = [
'Content-Type: application/json',
'X-API-KEY: YOUR_API_KEY'
];
$query = '{
ethereum {
dexTrades(limit:5) {
sellAmount
buyAmount
trade {
amount
}
}
}
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
print_r($data);
?>
使用JavaScript查询区块链区块信息
javascript
const fetch = require('node-fetch');
const url = 'https://graphql.bitquery.io/';
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'YOUR_API_KEY'
};
const query = `
{
bitcoin {
blocks(limit: 3) {
height
timestamp {
iso8601
}
transactionCount
}
}
}
`;
fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({ query: query })
})
.then(response => response.json())
.then(data => console.log(JSON.stringify(data, null, 2)))
.catch(error => console.error('Error:', error));
常见问题
Bitquery API是否需要认证?
是的,使用Bitquery API需要API密钥进行认证。您可以在其官方网站注册账户并获取API密钥。
Bitquery支持哪些区块链?
Bitquery支持多种主流区块链,包括以太坊、比特币、币安智能链(BSC)、Polygon等,并提供统一的GraphQL接口进行查询。
Bitquery的API调用有速率限制吗?
是的,Bitquery API有速率限制,具体限制取决于您的订阅计划。免费计划通常有较低的请求速率限制,付费计划则提供更高的限制。
Aitishiku.com