The quickest way to start building on Bitcoin with Quicknode is by sending a JSON-RPC request to your endpoint. In this quickstart, you’ll create an endpoint, copy its provider URL, and make your first request. Code samples are available in cURL as well as popular SDKs and programming languages.
Get Your Bitcoin Endpoint
如需了解 Quicknode 儀表板的詳細操作指南,請參閱我們的 指南
發送您的第一個請求
Your endpoint is ready. Now, let's make your first call to the Bitcoin blockchain. We'll use the getblock 此方法可根據特定區塊的雜湊值,擷取該區塊的詳細資訊。 請選擇您偏好的語言或 SDK,並依照以下步驟發送您的第一個請求.
- cURL
Python
- JavaScript
- Ruby
檢查 cURL 的安裝狀況
大多數基於 *nix 的系統預設即支援 cURL。請開啟終端機,並執行以下指令以檢查 cURL 的版本:
curl --version
傳送一個 JSON-RPC 請求
在您的終端機中,複製並貼上以下 cURL 指令以擷取封鎖資訊:
curl YOUR_QUICKNODE_ENDPOINT_URL/ \
-X POST \
-H "Content-Type: application/json" \
--data '{"id": 1, "jsonrpc": "2.0", "method": "getblock", "params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"]}'
範例回應
{
"result": {
"hash": "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09",
"confirmations": 912736,
"size": 216,
"height": 1000,
"version": 1,
"versionHex": "00000001",
"merkleroot": "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33",
"tx": [
"fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33"
],
"time": 1232346882,
"mediantime": 1232344831,
"nonce": 2595206198,
"bits": "1d00ffff",
"difficulty": 1,
"chainwork": "000000000000000000000000000000000000000000000000000003e903e903e9",
"nTx": 1,
"previousblockhash": "0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d",
"nextblockhash": "00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6"
},
"error": null,
"id": 1
}
設定您的專案
為您的 Python 專案建立一個新目錄:
mkdir bitcoin-python-quickstart
cd bitcoin-python-quickstart
建立並啟用虛擬環境
建立一個虛擬環境來管理依賴項:
python3 -m venv venv
source venv/bin/activate
安裝請求
安裝 requests 函式庫以進行 HTTP 請求:
pip 安裝 requests
建立一個 Python 腳本(app.py)
建立一個包含以下程式碼的 Python 檔案:
import requests
import json
# Your Quicknode endpoint URL
url = "YOUR_QUICKNODE_ENDPOINT_URL/"
# JSON-RPC request payload for getblock
payload = json.dumps({
"id": 1,
"jsonrpc": "2.0",
"method": "getblock",
"params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"]
})
headers = {
'Content-Type': 'application/json'
}
# Make the request
response = requests.request("POST", url, headers=headers, data=payload)
result = response.json()
print("Block information:")
print(json.dumps(result, indent=2))
執行該腳本
執行您的 Python 腳本:
python app.py
設定您的專案
建立一個新目錄並初始化一個 Node.js 專案:
mkdir bitcoin-js-quickstart
cd bitcoin-js-quickstart
npm init -y
建立一個 JavaScript 檔案(app.js)
建立一個名為 app.js 的檔案,並加入以下程式碼(使用原生 https 模組):
const https = require('https');
// Your Quicknode endpoint URL
const data = JSON.stringify({
"id": 1,
"jsonrpc": "2.0",
"method": "getblock",
"params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"]
});
const options = {
hostname: '{your-endpoint-name}.quiknode.pro',
path: '/{your-token}/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
let result = '';
res.on('data', (chunk) => {
result += chunk;
});
res.on('end', () => {
console.log('Block information:');
console.log(JSON.parse(result));
});
});
req.on('error', (error) => {
console.error('Error:', error);
});
req.write(data);
req.end();
執行該腳本
執行您的 JavaScript 腳本:
node app.js
設定您的專案
為您的 Ruby 專案建立一個新目錄:
mkdir bitcoin-ruby-quickstart
cd bitcoin-ruby-quickstart
檢查 Ruby 的安裝狀況
請確認您的系統上是否已安裝 Ruby。若尚未安裝,請從 https://ruby-lang.org 進行安裝:
ruby --version
建立一個 Ruby 腳本(main.rb)
建立一個名為 main.rb 的檔案,並輸入以下程式碼:
require "uri"
require "json"
require "net/http"
# Your Quicknode endpoint URL
url = URI("YOUR_QUICKNODE_ENDPOINT_URL/")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"
request.body = JSON.dump({
"id": 1,
"jsonrpc": "2.0",
"method": "getblock",
"params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"]
})
response = https.request(request)
result = JSON.parse(response.read_body)
puts "Block information:"
puts JSON.pretty_generate(result)
執行該腳本
執行您的 Ruby 腳本以擷取區塊資訊:
Ruby main.rb
如果您想進一步了解如何發送 API 請求,請參閱我們的指南和 範例應用程式。
我們 ❤️ 您的回饋!
如果您對這份文件有任何意見或疑問,請告訴我們。我們非常樂意聽取您的意見!