Skip to main content

Try it in Val Town

Try it now in Val Town

Interactive playground to test Corbits API calls
Click “Remix” to fork the playground, add your PAYER_KEYPAIR as an environment secret, and run live API calls.

Setup

1. Generate a Keypair

# Install Solana CLI
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

# Generate a new keypair
solana-keygen new --outfile ~/.config/solana/payer.json --no-bip39-passphrase

# View the keypair bytes
cat ~/.config/solana/payer.json
Copy the entire array output (e.g., [123,45,67,...]).

2. Add to Val Town

In the Val Town playground:
  1. Click the “Environment” tab
  2. Add a new secret:
    • Key: PAYER_KEYPAIR
    • Value: Paste the keypair array

3. Fund Your Wallet

Your wallet needs:
  • SOL: ~0.05 SOL for transaction fees
  • USDC: Start with 1-10 USDC for API requests (1 USDC = 1,000 requests)
Get your wallet address:
solana address -k ~/.config/solana/payer.json
Transfer SOL and USDC from an exchange or another wallet to this address.

Code Example

import {
  clusterApiUrl,
  Connection,
  Keypair,
  PublicKey,
} from "@solana/web3.js";
import { createLocalWallet } from "@faremeter/wallet-solana";
import { lookupKnownSPLToken } from "@faremeter/info/solana";
import { createPaymentHandler } from "@faremeter/payment-solana/exact";
import { wrap as wrapFetch } from "@faremeter/fetch";

// Load keypair from environment
const { PAYER_KEYPAIR } = process.env;
if (!PAYER_KEYPAIR) throw new Error("PAYER_KEYPAIR must be set");

const network = "mainnet-beta";

// Lookup USDC automatically
const usdcInfo = lookupKnownSPLToken(network, "USDC");
if (!usdcInfo) throw new Error("Could not find USDC mint");

const keypair = Keypair.fromSecretKey(
  Uint8Array.from(JSON.parse(PAYER_KEYPAIR)),
);
const connection = new Connection(clusterApiUrl(network));
const mint = new PublicKey(usdcInfo.address);

const wallet = await createLocalWallet(network, keypair);
const fetchWithPayer = wrapFetch(fetch, {
  handlers: [createPaymentHandler(wallet, mint, connection)],
});

// Make a paid API request
const url = "https://helius.api.corbits.dev";
const payload = {
  jsonrpc: "2.0",
  id: 1,
  method: "getBlockHeight",
};

const response = await fetchWithPayer(url, {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify(payload),
});

if (!response.ok) {
  const text = await response.text().catch(() => "");
  throw new Error(`HTTP ${response.status} ${response.statusText} ${text}`);
}

const data = await response.json();
console.log("Latest Block Height:", data.result);
// Expected response from Corbits Triton Val Town example

bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)

Balance of 5wjYK7BSesmte2HVpKHQYbAgdgd7Rw27ejW9UZjcCWc2 is 1020020 lamports!

Next steps