The quickest way to start building on Starknet 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 Starknet Endpoint
如需了解 Quicknode 仪表盘的详细操作指南,请参阅我们的 指南
发送您的第一个请求
Your endpoint is ready. Now, let's make your first call to the Starknet blockchain. We'll use the starknet_blockNumber 该方法返回最新的区块号。 选择您首选的语言或 SDK,并按照以下步骤发送您的第一个请求.
- cURL
- JavaScript
Python
- Ruby
检查 cURL 的安装情况
大多数基于 *nix 的系统默认都支持 cURL。打开终端,运行以下命令检查 cURL 的版本:
curl --version
发送一个 JSON-RPC 请求
在终端中,复制并粘贴以下 cURL 命令以获取最新的区块号:
curl -X POST YOUR_QUICKNODE_ENDPOINT_URL/rpc/v0_8 \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"starknet_blockNumber","params":[],"id":1}'
示例回答
{
"id": 1,
"jsonrpc": "2.0",
"result": 2004149
}
设置您的项目
创建一个新目录并初始化一个 Node.js 项目:
mkdir starknet-js-quickstart
cd starknet-js-quickstart
npm init -y
创建一个 JavaScript 文件(app.js)
Add the code into your JavaScript file and execute the file in your terminal with the `node fileName.js` to retrieve the current block number.
const https = require('https');
const data = JSON.stringify({
"id": 1,
"jsonrpc": "2.0",
"method": "starknet_blockNumber",
"params": []
});
const options = {
hostname: '{your-endpoint-name}.quiknode.pro',
path: '/{your-token}/rpc/v0_8',
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();
运行该脚本
执行您的 JavaScript 脚本:
node app.js
设置您的项目
为您的 Python 项目创建一个新目录:
mkdir starknet-python-quickstart
cd starknet-python-quickstart
创建并激活虚拟环境
创建一个虚拟环境来管理依赖项:
python3 -m venv venv
source venv/bin/activate
安装请求
安装 requests 库以进行 HTTP 请求:
pip 安装 requests
编写一个 Python 脚本(app.py)
To run the Python code below, add the code to a file and execute it with the `python file.py` command in your terminal window.
import requests
import json
url = "YOUR_QUICKNODE_ENDPOINT_URL/rpc/v0_8"
payload = json.dumps({
"id": 1,
"jsonrpc": "2.0",
"method": "starknet_blockNumber",
"params": []
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
运行该脚本
运行您的 Python 脚本:
python app.py
设置您的项目
为您的 Ruby 项目创建一个新目录:
mkdir starknet-ruby-quickstart
cd starknet-ruby-quickstart
检查 Ruby 安装情况
请确认您的系统上已安装 Ruby。如果尚未安装,请从 https://ruby-lang.org 安装:
ruby --version
创建一个 Ruby 脚本(main.rb)
创建一个名为 main.rb 的文件,并输入以下代码:
require "uri"
require "json"
require "net/http"
url = URI("YOUR_QUICKNODE_ENDPOINT_URL/rpc/v0_8")
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": "starknet_blockNumber",
"params": []
})
response = https.request(request)
result = JSON.parse(response.read_body)
puts "Block number: #{result["result"]}"
运行该脚本
Execute your Ruby script to see the current block number:
ruby main.rb
如果您想进一步了解如何发送 API 请求,请查看我们的指南和示例应用。
我们 ❤️ 您的反馈!
如果您对本文档有任何反馈或疑问,请告诉我们。我们非常期待您的反馈!