# Write

## Create Plan

#### Initialize Subs Client

```typescript
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

```typescript
// 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

```typescript
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

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

```

### Returns App Url

```typescript
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**:

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

***

#### **4. `payment_type`**

* **Type**: `PaymentType`
* **Options**:

```typescript
{ 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**:

  ```typescript
  {
    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`&#x20;
  * Use valid Stellar Token Address otherwise the payment won't work.
* **Example**:

  ```typescript
  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**:

  ```typescript
  { tag: "ONETIME" | "MINUTES" | "HOURS" | "DAYS" | "WEEK" | "MONTH" | "YEAR" }
  ```
* **Example**:

  ```typescript
  { 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**:

  ```typescript
  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**:

  ```typescript
  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**:

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.subsprotocol.com/developer-docs/subs-sdk/write.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
