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 "public"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
nameType:
stringDescription: Unique identifier for the payment plan (e.g.,
Student,Premium).Rules:
Must be unique across all your payment plans.
2. owner
ownerType:
string(Stellar public key)Description: Revenue beneficiary address.
Default: Use your
creatorAddress(if not specified).Revenue Sharing:
If
owner ≠ creatorAddress, fees are split:ownerearnsfee%creatorAddressearns100% - fee%.
3. fee
feeType:
BigInt(basis points: 10 = 1%)Description: Revenue share for
owner(only ifowner ≠ creatorAddress).Example:
fee: BigInt(50), // owner gets 5%, creator gets 95%4. payment_type
payment_typeType:
PaymentTypeOptions:
{ tag: "ERC20", values: undefined } // Currently only ERC20-like tokensFuture: May support NFTs or other assets via
values.
5. payment_tokens
payment_tokensType:
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_amountis set to 0 no change.But you can customize the First payment more or less then the
priceUse 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
period_typeType:
PeriodTypeOptions:
{ tag: "ONETIME" | "MINUTES" | "HOURS" | "DAYS" | "WEEK" | "MONTH" | "YEAR" }Example:
{ tag: "DAYS", values: undefined }} // Daily subscriptions
7. trial_period
trial_periodType:
BigIntDescription: 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
limit_periodType:
BigIntDescription: Number of billing cycles users must commit to.
Example:
limit_period: BigInt(3) // 3 cycles (3 days for "DAYS" period_type)
9. loading_time
loading_timeType:
BigIntDescription: Grace period (in days) for late payments before subscription deactivation.
Example:
loading_time: BigInt(3) // 3-day grace period
Last updated