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 method, which returns the latest block number. 請選擇您偏好的語言或 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 請求,請參閱我們的指南和 範例應用程式。
我們 ❤️ 您的回饋!
如果您對這份文件有任何意見或疑問,請告訴我們。我們非常樂意聽取您的意見!