> ## Documentation Index
> Fetch the complete documentation index at: https://docs.solanatracker.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana RPC 快速开始：发起第一个请求

> 使用 curl 和 Node.js 调用 Solana Tracker RPC，配置 API 密钥，并处理 HTTP 与 JSON-RPC 错误。

本指南通过 HTTP 读取账户的 SOL 余额。准备服务端脚本和[控制台](https://www.solanatracker.io/solana-rpc)中的 RPC API 密钥。

## 调用 getBalance

设置密钥后执行：

```bash theme={null}
export SOLANA_RPC_API_KEY="YOUR_API_KEY"
curl "https://rpc-data.solanatracker.io/?api_key=${SOLANA_RPC_API_KEY}" \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"getBalance","params":["So11111111111111111111111111111111111111112",{"commitment":"confirmed"}]}'
```

将示例地址替换为钱包或账户公钥。响应包含 `result.context.slot` 和 `result.value`。余额单位为 **lamports**，1 SOL 等于 1,000,000,000 lamports；此方法不返回 SPL 代币持仓。

## 使用 Node.js

使用 Node.js 20 或更新版本。将代码保存为 `balance.mjs`，在相同环境变量下运行。辅助函数同时检查 HTTP 错误和 JSON-RPC 错误，后者也可能出现在 HTTP 200 响应中。

```javascript balance.mjs theme={null}
const apiKey = process.env.SOLANA_RPC_API_KEY;
if (!apiKey) throw new Error("Set SOLANA_RPC_API_KEY before starting");
const endpoint = new URL("https://rpc-data.solanatracker.io/");
endpoint.searchParams.set("api_key", apiKey);

async function rpc(method, params = []) {
  const response = await fetch(endpoint, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
    signal: AbortSignal.timeout(15_000),
  });
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  const body = await response.json();
  if (body.error) {
    throw new Error(`RPC ${body.error.code}: ${body.error.message}`);
  }
  return body.result;
}

const balance = await rpc("getBalance", [
  "So11111111111111111111111111111111111111112",
  { commitment: "confirmed" },
]);
console.log({ slot: balance.context.slot, lamports: balance.value });
```

```bash theme={null}
node balance.mjs
```

将 API 密钥保存在服务端环境变量中。不要把包含密钥的地址写入前端代码或日志。

## 选择确认级别

* `processed`：节点最新处理的状态，可能回滚。
* `confirmed`：已获得超级多数质押投票的状态，本例用于读取当前数据。
* `finalized`：确认程度最高，但比链尖更滞后。

比较相关数据时使用相同确认级别。独立请求仍可能返回不同的上下文 slot；需要在同一响应读取多个账户时，使用 [getMultipleAccounts](/cn/solana-rpc/http/getmultipleaccounts)。

## 处理失败

遇到 HTTP 429 时降低并发，使用带随机抖动和次数上限的退避重试读取请求。参数或认证错误需要修正请求或配置。交易提交超时后先检查签名状态，再决定如何重试；重新构建和签名会产生另一笔交易。

## 下一步

* [读取代币账户](/cn/solana-rpc/http/gettokenaccountsbyownerv2)。
* [订阅数据变化](/cn/solana-rpc/subscriptions)。
* [查看请求计费和限流](/cn/solana-rpc/credits-and-rate-limits)。
