Skip to main content

Setup

Set up your payment handler in just 3 lines:
import { payer } from "@faremeter/rides";

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

await payer.addLocalWallet(PAYER_KEYPAIR);
@faremeter/rides automatically detects your network, looks up USDC, and sets up payments using your existing wallet. All examples below use payer.fetch().

Example: Chat Completion with GPT-5

Call the RedPill chat completions endpoint with GPT-5:
const proxyUrl = "https://redpill.api.corbits.dev";

const requestBody = {
  model: "openai/gpt-5",
  messages: [
    {
      role: "user",
      content: "What is the capital of France?"
    }
  ],
  max_completion_tokens: 100
};

const response = await payer.fetch(`${proxyUrl}/v1/chat/completions`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

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("Chat completion:", data);
Response:
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "openai/gpt-5",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 7,
    "total_tokens": 20
  }
}
The response includes:
  • id: Unique identifier for this completion
  • model: The model used for the completion
  • choices: Array of completion choices (usually one)
  • message: The assistant’s response with role and content
  • usage: Token usage statistics for billing and monitoring

Payment Flow

When you make a request, the following happens automatically:
  1. Initial Request: Client sends chat completion request
  2. 402 Response: Proxy returns payment requirements
{
  "accepts": [{
    "network": "solana-mainnet-beta",
    "maxAmountRequired": 100000,
    "asset": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
    "payTo": "9f8qtNZUwqr1ZWZmm6ETec6wmQmaxiPahERnzVRrZ98i"
  }]
}
  1. Payment: Payment handler processes USDC transfer (0.10 USDC)
  2. Success: Proxy fulfills original request with 200 OK
All of this is handled automatically by payer.fetch() or fetchWithPayer!

External Resources