Skip to content

Client SDK Guide

Detailed guide for using the TypeScript SDK in your applications. The SDK provides a set of tools for managing payment channels, request interceptors, and error handling.

Installation & Setup

npm
npm install pipegate-sdk

SDK Configuration

Create a .env file in your project root containing your private key:

PRIVATE_KEY=your_private_key_here
import { ClientInterceptor } from "pipegate-sdk";
 
const pipeGate = new ClientInterceptor();

Core Features

Request Interceptors

Choose the interceptor that fits the payment method for the provider you are interacting with.

import axios from "axios";
 
// Create API client
const api = axios.create({
  baseURL: "https://api.example.com",
});
 
// Add payment channel interceptor
api.interceptors.request.use(
  pipeGate.createPaymentChannelRequestInterceptor(channelId).request
);
 
// Add one-time payment interceptor
api.interceptors.request.use(
  pipeGate.createOneTimePaymentRequestInterceptor(txHash).request
);
 
// Add stream interceptor
api.interceptors.request.use(
  pipeGate.createStreamRequestInterceptor(streamSender).request
);

For detailed guide on using and configuring the interceptor, refer to the guides of the respective payment methods.

Channel Management

Channels can also be created with the API providers using the SDK.

// Create channel
const channel = await pipeGate.createPaymentChannel({
  recipient: "0x...",
  duration: 2592000,
  tokenAddress: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
  amount: "100",
});
 
// Add to state
await pipeGate.addNewChannel(channel.channelId, channel);
 
// Get channel state
const state = pipeGate.getChannelState(channel.channelId);

Error Handling

try {
  const response = await api.get("/endpoint");
} catch (error) {
  if (error.message.includes("insufficient balance")) {
    // Handle low balance
  } else if (error.message.includes("expired channel")) {
    // Handle expired channel
  }
}

Best Practices

  1. Environment Variables
    • Store private keys securely
    • Use environment-specific RPC URLs
  2. Channel Management
    • Monitor channel balances
    • Handle channel expiration
    • Implement proper error handling
  3. Request Handling
    • Set appropriate timeouts
    • Implement retry logic
    • Handle network errors

TypeScript Types

interface ChannelParams {
  recipient: string;
  duration: number;
  tokenAddress: string;
  amount: string;
}
 
interface ChannelState {
  channelId: string;
  balance: string;
  nonce: number;
  expiration: number;
}