Skip to main content

Documentation Index

Fetch the complete documentation index at: https://build.onswig.com/llms.txt

Use this file to discover all available pages before exploring further.

This guide covers how to use the Swig Paymaster SDK to sponsor transaction fees for your users, enabling gasless transactions.

What is a Paymaster?

A paymaster is a service that pays transaction fees on behalf of users. Instead of requiring users to hold SOL for gas fees, the paymaster covers these costs, creating a seamless onboarding experience. Benefits:
  • Users don’t need SOL for transaction fees
  • Lower barrier to entry for new users
  • Better user experience for dApps

Prerequisites

Before using the paymaster SDK:
  1. Create a paymaster in the Developer Portal
  2. Fund your paymaster with SOL
  3. Get your API key from the Developer Portal
Paymasters require a paid subscription (PRO, ULTRA, or ENTERPRISE). See Subscriptions & Billing for details.

Installation

Choose the package that matches your Solana SDK version:
npm install @swig-wallet/paymaster-classic
For applications using @solana/web3.js version 1.x (the traditional Solana SDK).

Configuration

Create a paymaster client with your credentials:
import { createPaymasterClient } from '@swig-wallet/paymaster-classic';

const paymaster = createPaymasterClient({
  apiKey: process.env.SWIG_API_KEY!,
  paymasterPubkey: process.env.PAYMASTER_PUBKEY!,
  baseUrl: 'https://api.onswig.com',
  network: 'devnet',
});

Configuration Options

OptionTypeRequiredDescription
apiKeystringYesAPI key from Developer Portal
paymasterPubkeystringYesPaymaster public key
baseUrlstringYesPaymaster API URL (https://api.onswig.com)
network'mainnet' | 'devnet'YesSolana network
customRpcUrlstringNoCustom RPC endpoint (overrides default)
retryOptionsobjectNoRetry configuration for failed requests

Retry Options

Configure automatic retries for transient failures:
const paymaster = createPaymasterClient({
  apiKey: 'sk_your_api_key',
  paymasterPubkey: '...',
  baseUrl: 'https://api.onswig.com',
  network: 'mainnet',
  retryOptions: {
    maxRetries: 3,        // Number of retry attempts
    retryDelay: 1000,     // Initial delay in milliseconds
    backoffMultiplier: 2, // Exponential backoff multiplier
  },
});

Quick Start

Here’s a minimal example to sponsor a transaction:
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { createPaymasterClient } from '@swig-wallet/paymaster-classic';

// Initialize client
const paymaster = createPaymasterClient({
  apiKey: process.env.SWIG_API_KEY!,
  paymasterPubkey: process.env.PAYMASTER_PUBKEY!,
  baseUrl: 'https://api.onswig.com',
  network: 'devnet',
});

// User's keypair
const userKeypair = Keypair.generate();

// Create an instruction (memo example)
const instruction = new TransactionInstruction({
  keys: [{ pubkey: userKeypair.publicKey, isSigner: true, isWritable: false }],
  programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
  data: Buffer.from('Hello, gasless world!'),
});

// Create transaction - paymaster is set as fee payer
const transaction = await paymaster.createLegacyTransaction(
  [instruction],
  [userKeypair], // User signs here
);

// Paymaster signs and sends
const signature = await paymaster.signAndSend(transaction);
console.log(`https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Transaction Flow

The paymaster transaction flow works as follows:
┌─────────────┐    ┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   Create    │───▶│    User     │───▶│  Paymaster  │───▶│   Submit    │
│ Transaction │    │    Signs    │    │    Signs    │    │  to Network │
└─────────────┘    └─────────────┘    └─────────────┘    └─────────────┘
       │                  │                  │                  │
       ▼                  ▼                  ▼                  ▼
   Paymaster         User proves       Fee covered        Transaction
   as fee payer     authorization      by paymaster        confirmed
  1. Create Transaction: Build the transaction with the paymaster set as the fee payer
  2. User Signs: User signs to authorize the transaction
  3. Paymaster Signs: Paymaster validates and signs (covering the fee)
  4. Submit: Transaction is sent to the Solana network

Next Steps