The quickest way to start building on NEAR 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, JavaScript, Python, and Ruby.
Get Your NEAR Endpoint
如需了解 Quicknode 仪表盘的详细操作指南,请参阅我们的 指南
发送您的第一个请求
Your endpoint is ready. Now, let's make your first call to the NEAR blockchain. We’ll use the 状态 method, which returns general status information about the NEAR node including version, sync status, and validator information. 选择您首选的语言或 SDK,并按照以下步骤发送您的第一个请求.
- cURL
- Node.js
Python
- Ruby
检查 cURL 的安装情况
大多数基于 *nix 的系统默认都支持 cURL。打开终端,运行以下命令检查 cURL 的版本:
curl --version
发送一个 JSON-RPC 请求
In your terminal, copy and paste the following cURL command to retrieve the node status:
curl -X POST YOUR_QUICKNODE_ENDPOINT_URL/ \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"status","params":[],"id":1}'
示例回答
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"version": {
"version": "1.37.1",
"build": "crates-0.21.1-216-gd6385b0eb"
},
"chain_id": "mainnet",
"sync_info": {
"latest_block_hash": "44L5qF4NitTZgNd5haxFLgWaJJ3FD9VafPo4WdL1p3M8",
"latest_block_height": 106503813,
"latest_state_root": "7Bf2aQoAqo1FcYjSddJvPEkrSCsw5j9uEPMBbEE7Kk8",
"latest_block_time": "2024-01-15T12:00:00.000000000Z",
"syncing": false
}
}
}
设置您的项目
首先,使用 `node --version` 命令验证 Node.js 是否已安装。如果尚未安装,请从 https://nodejs.org 下载。然后创建一个新目录并初始化一个 Node.js 项目:
mkdir near-api-quickstart
cd near-api-quickstart
npm init -y
Create your script
Create a new file called index.js and add the following code to retrieve the node status:
const https = require('https');
const data = JSON.stringify({
"jsonrpc": "2.0",
"method": "status",
"params": [],
"id": 1
});
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(result);
});
});
req.on('error', (error) => {
console.error('Error:', error);
});
req.write(data);
req.end();
Run your script
Execute your script in the terminal to retrieve the node status:
node index.js
设置您的项目
First, verify Python is installed with python --version or python3 --version. Then create a new directory for your project:
mkdir near-api-quickstart
cd near-api-quickstart
创建并激活虚拟环境
创建一个虚拟环境来管理依赖项:
python3 -m venv venv
source venv/bin/activate
Install dependencies
安装 requests 库以进行 HTTP 请求:
pip 安装 requests
Create your script
Create a new file called main.py and add the following code to retrieve the node status:
import requests
import json
url = "YOUR_QUICKNODE_ENDPOINT_URL/"
payload = json.dumps({
"jsonrpc": "2.0",
"method": "status",
"params": [],
"id": 1
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Run your script
Execute your script in the terminal to retrieve the node status:
python main.py
设置您的项目
First, create a new directory for your project:
mkdir near-api-quickstart
cd near-api-quickstart
检查 Ruby 安装情况
请确认您的系统上已安装 Ruby。如果尚未安装,请从 https://ruby-lang.org 安装:
ruby --version
Create your script
Create a new file called main.rb and add the following code to retrieve the node status:
require "uri"
require "json"
require "net/http"
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({
"jsonrpc": "2.0",
"method": "status",
"params": [],
"id": 1
})
response = https.request(request)
puts response.read_body
Run your script
Execute your script in the terminal to retrieve the node status:
ruby main.rb
如果您想进一步了解如何发送 API 请求,请查看我们的指南和示例应用。
我们 ❤️ 您的反馈!
如果您对本文档有任何反馈或疑问,请告诉我们。我们非常期待您的反馈!