Skip to main content

View example code

Explore the complete collection of h402 solana RPC integration examples in our repository.

H402 Solana RPC Proxy

Your personal, on-demand, account-free gateway to the Solana blockchain. This project is a simple but powerful proxy server for Solana RPC calls that uses the H402 protocol for micropayments. Think of it as your own personal Helius or QuickNode, but without the need to create an account, manage API keys, or commit to a monthly subscription. You pay for exactly what you use, on the fly. The flow varies depending on blockchain, token, crypto and other aspects

Why is this cool?

In the current Web3 landscape, accessing blockchain data usually means signing up for a service, getting an API key, and dealing with monthly bills. This project demonstrates a more open, decentralized, and flexible approach.
  • No Accounts, No Subscriptions: Ditch the tedious sign-up forms and monthly commitments. Your wallet is your account.
  • On-Demand, Per-Request Payments: Pay only for the specific RPC calls you make, when you make them.
  • Enhanced Privacy: No need to hand over personal information to a third-party service.
  • Perfect for High-Availability Systems: Use it as a primary RPC endpoint or as a pay-on-demand backup. If your main provider fails, you can instantly fall back to this proxy without any prior setup or cost commitment. This is the model we use for our resilient infrastructure.

The Power of H402 + Solana

This project leverages a powerful combination:
  1. The H402 Protocol: A standard for handling payments over HTTP. It allows the server to issue a 402 Payment Required challenge, which the client can programmatically pay and retry.
  2. The Solana Blockchain: With transactions that confirm in the blink of an eye and cost a fraction of a cent, Solana is the perfect payment rail for this kind of real-time, high-frequency use case.
The result is a seamless, fast, and incredibly efficient system for monetizing API access.

How It Works

Here’s how it works step by step:
  1. A client makes a standard RPC request to the proxy server (e.g., getAccountInfo).
  2. The proxy checks its configuration and determines the cost for that specific method.
  3. It responds with a standard 402 Payment Required header, detailing the exact amount of USDC to send and the address to send it to.
  4. An H402-aware client (like one using our @bit-gpt/h402-fetch library) automatically parses this challenge, crafts and signs the tiny payment transaction, and sends it on Solana.
  5. Once the payment is confirmed (typically in under a second), the client automatically retries the original RPC request.
  6. The proxy verifies the payment and forwards the RPC request to the upstream provider (e.g., Helius), returning the response to the client.

Getting Started

Prerequisites

  • Node.js
  • pnpm is recommended (npm install -g pnpm)

1. Clone the Repository

git clone https://github.com/bit-gpt/h402-solana-rpc.git
cd h402-solana-rpc

2. Install Dependencies

npm i

3. Configure Your Environment

Copy the example environment file and fill in the details.
cp .env-example .env
You will need to edit the .env file: Server Config:
  • RPC_URL: The upstream Solana RPC endpoint you want to proxy.
  • PORT: The port your proxy server will run on.
  • SOLANA_PAY_TO_ADDRESS: The public key of the Solana wallet that will receive the payments.
Client Config (for testing):
  • SOLANA_PRIVATE_KEY: The base58-encoded private key of the Solana wallet that will send the payments.
  • RESOURCE_SERVER_URL: The full URL of your running proxy server (e.g., http://localhost:19782).
  • ENDPOINT_PATH: The path for the RPC endpoint on your proxy (/rpc).

4. Run the Proxy Server

pnpm dev
The server will start, listening on the PORT you configured.

5. Run the Client to Test

In a separate terminal, run the client to send a test getLatestBlockhash request:
pnpm client
You’ll see the JSON response from the server logged in your console, and if you check your server logs, you’ll see the request being processed after payment.

Future Roadmap

This is just the beginning! We’re planning to evolve this concept by adding:
  • A Credit-Based System: Allow users to pre-fund an account with a larger, one-time payment. The proxy will then deduct credits for each RPC call, offering even lower latency by removing the need for an on-chain transaction for every single request.
We believe this model can fundamentally change how we build and pay for decentralized infrastructure.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.