所有对 TheRPC API 的请求都需要使用 API 密钥进行身份验证。本指南解释如何安全获取和使用您的 API 密钥。
在每个请求中使用 Bearer 身份验证方案包含您的 API 密钥:
Authorization: Bearer YOUR_API_KEY
使用 curl:
curl --request POST 'YOUR_API_ENDPOINT' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data '{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}'
使用 JavaScript:
const response = await fetch('YOUR_API_ENDPOINT', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${API_KEY}`,
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 1,
}),
});
常见身份验证错误:
{
"jsonrpc": "2.0",
"error": {
"code": -32001,
"message": "Invalid authentication credentials"
},
"id": 1
}
# .env 文件
THERPC_API_KEY=your_api_key_here
Node.js:
require('dotenv').config();
const API_KEY = process.env.THERPC_API_KEY;
Python:
import os
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv('THERPC_API_KEY')