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();
运行
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
运行该脚本
Execute your Ruby script to retrieve block information:
ruby main.rb
如果您想进一步了解如何发送 API 请求,请查看我们的指南和示例应用。
我们 ❤️ 您的反馈!
如果您对本文档有任何反馈或疑问,请告诉我们。我们非常期待您的反馈!