The quickest way to start building on Cosmos with Quicknode is by sending a JSON-RPC or REST/gRPC 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 Cosmos Endpoint
如需了解 Quicknode 儀表板的詳細操作指南,請參閱我們的 指南
發送您的第一個請求
Your endpoint is ready. Now, let's make your first call to the Cosmos blockchain. We’ll use the block_by_hash method, which returns details of a specificed block when given a hash parameter. Select your preferred language or tool and follow the steps to send your request.
- cURL
- Node.js
Python
- Ruby
檢查 cURL 的安裝狀況
大多數基於 *nix 的系統預設即支援 cURL。請開啟終端機,並執行以下指令以檢查 cURL 的版本:
curl --version
Query block by hash
Retrieve block information by hash from your Quicknode endpoint:
curl --location '<YOUR_QUICKNODE_ENDPOINT_URL>/block_by_hash?hash=0x645507DC48B4EF4BFF51826A0A5B73FD8CF926036AA4F6697D748FFC0B56D134'
Sample response
{
"jsonrpc": "2.0",
"id": -1,
"result": {
"block_id": { ... }
},
"block": {
"header": {
"version": {
"block": "11"
},
"chain_id": "cosmoshub-4",
"height": "20431644",
"time": "2024-05-15T01:23:57.786705041Z",
"last_block_id": { ... },
},
"data": {
"txs": [ ... ]
},
"evidence": {
"evidence": []
},
"last_commit": { ... }
}
}
}
設定您的專案
Create a directory and initialize a Node.js project:
mkdir cosmos-js-quickstart
cd cosmos-js-quickstart
npm init -y
Create app.js
Use native fetch (Node.js 18+):
const requestOptions = { method: 'GET', redirect: 'follow' };
async function main() {
try {
const url = '<YOUR_QUICKNODE_ENDPOINT_URL>/block_by_hash?hash=0x645507DC48B4EF4BFF51826A0A5B73FD8CF926036AA4F6697D748FFC0B56D134';
const res = await fetch(url, requestOptions);
const text = await res.text();
console.log('Block information:', text);
} catch (err) {
console.error('Error:', err);
}
};
}
main();
Run
Execute your script:
node app.js
設定您的專案
為您的 Python 專案建立一個新目錄:
mkdir cosmos-python-quickstart
cd cosmos-python-quickstart
建立並啟用虛擬環境
建立一個虛擬環境來管理依賴項:
python3 -m venv venv
source venv/bin/activate
安裝請求
安裝 requests 函式庫以進行 HTTP 請求:
pip 安裝 requests
建立一個 Python 腳本(app.py)
建立一個包含以下程式碼的 Python 檔案:
import requests
url = '<YOUR_QUICKNODE_ENDPOINT_URL>/block_by_hash?hash=0x645507DC48B4EF4BFF51826A0A5B73FD8CF926036AA4F6697D748FFC0B56D134'
resp = requests.get(url)
print('Block information:')
print(resp.text)
執行該腳本
執行您的 Python 腳本:
python app.py
設定您的專案
為您的 Ruby 專案建立一個新目錄:
mkdir cosmos-ruby-quickstart
cd cosmos-ruby-quickstart
Check Ruby Installation
請確認您的系統上是否已安裝 Ruby。若尚未安裝,請從 https://ruby-lang.org 進行安裝:
ruby --version
建立一個 Ruby 腳本(main.rb)
建立一個名為 main.rb 的檔案,並輸入以下程式碼:
require 'uri'
require 'net/http'
url = URI('<YOUR_QUICKNODE_ENDPOINT_URL>/block_by_hash?hash=0x645507DC48B4EF4BFF51826A0A5B73FD8CF926036AA4F6697D748FFC0B56D134')
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
req = Net::HTTP::Get.new(url)
res = https.request(req)
puts 'Block information:'
puts res.body
執行該腳本
執行您的 Ruby 腳本以擷取區塊資訊:
Ruby main.rb
如果您想進一步了解如何發送 API 請求,請參閱我們的指南和 範例應用程式。
我們 ❤️ 您的回饋!
如果您對這份文件有任何意見或疑問,請告訴我們。我們非常樂意聽取您的意見!