Skip to content

One-Time Payments

One-time payments are perfect for occasional API usage or when you don't want to manage payment channels.

Best for one-time payment, for e.g. downloading a certain resource

How it works & Architecture

One Time flow

For API Consumers

Make Payment

// Make direct payment using your wallet to the provider
// Store transaction hash for API call
const txHash = "0x...";

or You can subscribe to the provider using Pipegate Hub

or Just use the cast CLI tool to send a transaction, no need to use any platform

terminal
# Send 1 USDC to the provider
cast send $USDC_ADDRESS "transfer(address,uint256)" $0xPROVIDER 1000000 --rpc-url $RPC_URL --private-key $PRIVATE_KEY

Setup API Client

import { ClientInterceptor } from "pipegate-sdk";
 
const pipeGate = new ClientInterceptor();
 
const api = axios.create({
  baseURL: "https://api.example.com",
});
 
api.interceptors.request.use(
  pipeGate.createOneTimePaymentRequestInterceptor(txHash).request
);

Make API Call

try {
  const response = await api.get("/endpoint");
} catch (error) {
  if (error.message.includes("invalid payment")) {
    // Handle payment verification error
  }
}

For API Providers

Configure Payment Settings

Set the token you want to recieve and the amount you want to charge for each call. User will be allowed to access the API for a certain period of time after payment which can be configured

use pipegate::middleware::one_time_payment::{types::OneTimePaymentConfig};
use pipegate::utils::{Address, Url, U256};
 
let rpc_url: Url = "https://base-rpc.publicnode.com".parse().unwrap();
 
let onetime_payment_config = OneTimePaymentConfig {
    recipient: Address::from_str("YOUR_ADDRESS").unwrap(),
    token_address: Address::from_str("USDC_ADDRESS").unwrap(),
    amount: U256::from(1000000), // 1 USDC each call
    period: U256::from(3600), // Access until 1 hour post payment
    rpc_url: rpc_url.to_string(),
};

Setup Middleware

use pipegate::middleware::one_time_payment::{OneTimePaymentMiddlewareLayer};
 
let app = Router::new()
    .route("/", get(root))
    .layer(OnetimePaymentMiddlewareLayer::new(onetime_payment_config));

Best Practices

  • Verify transaction confirmations
  • Handle payment expiration
  • Implement rate limiting
  • Store transaction hashes to prevent reuse