Write

Subs sdk write methods

Create Plan

Initialize Subs Client

import { SubsSDK, PaymentType, PeriodType, SubsPayment, Keypair } from 'subs-sdk';

// Initialize with wallet private key (keep this secure!)
const subsClient = new SubsSDK("YOUR_WALLET_PRIVATE_KEY", "testnet"); // "testnet" or "mainnet"

Payment Configuration

// Get creator keypair
const creatorKeypair = Keypair.fromSecret("OWNER_SECRET_KEY");
const creatorAddress = creatorKeypair.publicKey();

// Configure payment type
const paymentType: PaymentType = {
  tag: "ERC20",      // Only ERC20 for the moment.
  values: undefined // For simple payment types
};

// Configure period type
const periodType: PeriodType = {
  tag: "DAYS", // Other options: ONETIME | MINUTES | HOURS | WEEK | MONTH | YEAR
  values: undefined
};

Payment Plans Setup

const payments: SubsPayment[] = [
  {
    name: paymentName,      // String
    owner: creatorAddress,  // String
    fee: BigInt(0),
    payment_type: paymentType,
    payment_tokens: [
      {
        token: 'CBIGUDYSW55TYQ47D3723KD3MUP6EABI5MVFTIMHE6CEUY55GCOW4YY5', // String
        price: BigInt(6 * 10 ** 18),  // 18 Is your token decimal
        first_amount: BigInt(0)       // You can customize the first amount.
      },
      {
        token: 'CAHVA6R5QJUTYRNG3GR7XPE63MRFWQH32CHBW6CANK3OKDR5TEOHN32V', // String
        price: BigInt(6000000000000000000), // Or leave 18 zeros for the decimals
        first_amount: BigInt(0)
      }
    ],
    trial_period: BigInt(0),
    period_type: periodType,
    limit_period: BigInt(3),
    loading_time: BigInt(3)
  },
  {
    name: paymentName,
    // ... similar structure with different prices, tokens, period_type, ...
  }
];

Execute Creation

await subsClient.createApp(
  nameApp,        // String
  payments,       // Configured payment plans array
  creatorAddress, // Owner's public address
);

Returns App Url

const result = await createApp("MyApp", payments, "G...ABC123");
console.log(result.url); 
// -> https://checkout.subsprotocol.com/#/testnet?owner=G...ABC123&chain=stellart&appId=...

Payment Struct

1. name

  • Type: string

  • Description: Unique identifier for the payment plan (e.g., Student, Premium).

  • Rules:

    • Must be unique across all your payment plans.


2. owner

  • Type: string (Stellar public key)

  • Description: Revenue beneficiary address.

  • Default: Use your creatorAddress (if not specified).

  • Revenue Sharing:

    • If owner ≠ creatorAddress, fees are split:

      • owner earns fee%

      • creatorAddress earns 100% - fee%.


3. fee

  • Type: BigInt (basis points: 10 = 1%)

  • Description: Revenue share for owner (only if owner ≠ creatorAddress).

  • Example:

fee: BigInt(50), // owner gets 5%, creator gets 95%

4. payment_type

  • Type: PaymentType

  • Options:

{ tag: "ERC20", values: undefined } // Currently only ERC20-like tokens
  • Future: May support NFTs or other assets via values.


5. payment_tokens

  • Type: Array<Token>

  • Structure:

    {
      token: string,   // Stellar Asset ID (e.g., 'CBIGUDYSW55TYQ...')
      price: BigInt,   // Don't forget you token decimals
      first_amount: BigInt // Initial discounted or additional charge
    }
  • Rules:

    • If the first_amount is set to 0 no change.

    • But you can customize the First payment more or less then the price

    • Use valid Stellar Token Address otherwise the payment won't work.

  • Example:

    payment_tokens: [{
      token: 'CBIGUDYSW55TYQ4...MVFTIMHE6CEUY55GCOW4YY5', //Token address
      price: BigInt(6 * 10 ** 18), // 18 Is your token decimal
      first_amount: BigInt(0) // No initial discount
    }]

6. period_type

  • Type: PeriodType

  • Options:

    { tag: "ONETIME" | "MINUTES" | "HOURS" | "DAYS" | "WEEK" | "MONTH" | "YEAR" }
  • Example:

    { tag: "DAYS", values: undefined }} // Daily subscriptions

7. trial_period

  • Type: BigInt

  • Description: Free trial duration (units depend on period_type):

    • DAYS, WEEK, MONTH, YEAR → Trial in days.

    • HOURS → Trial in hours.

    • MINUTES → Trial in minutes.

  • Example:

    trial_period: BigInt(7) // 7-day trial for "DAYS" period_type


8. limit_period

  • Type: BigInt

  • Description: Number of billing cycles users must commit to.

  • Example:

    limit_period: BigInt(3) // 3 cycles (3 days for "DAYS" period_type)


9. loading_time

  • Type: BigInt

  • Description: Grace period (in days) for late payments before subscription deactivation.

  • Example:

    loading_time: BigInt(3) // 3-day grace period

Last updated