# Subscription Checkout SDK

### Getting Started <a href="#getting-started" id="getting-started"></a>

#### Install sdk

```
npm install --save  @moveflow/sdk-aptos

or

yarn add  @moveflow/sdk-aptos
```

#### Intitialization

To get started, you'll need to initialize the SDK . The following code snippet demonstrates how to do this:

```typescript
import { AptosAccount, Network } from "aptos";
const youMnemonic = "youMnemonic"
const alice = AptosAccount.fromDerivePath("m/44'/637'/0'/0'/0'", youMnemonic);

const { subscription } = new SDK(Network.TESTNET)

```

#### Write Operation

**Create Subscription**

This method allows users to create a subscription contract with various parameters, while performing validation checks on the input data. It ensures that the provided data is valid and that the user's balance is sufficient to create the subscription.

```typescript
const _input: CreatePayload = {
    recipient: alice.address.toString(),
    deposit_amount: 1,
    start_time: Math.floor(Date.now() / 1000 ).toString(),
    stop_time: Math.floor(Date.now() / 1000  + 60 * 60 * 24 * 30).toString(),
    rate_type: 'day',
    amount_type: 'fixed'
}

const output = subscription.create(_input);
```

CreatePayload is the input data for creating the subscription. Include fields:

* recipient (string): The recipient's address for the subscription.
* deposit (number): The deposit amount for the subscription.
* coinType (string): The token address of the token for the subscription, the default is aptos.
* startTime (number): The start time of the subscription.
* stopTime (number): The stop time of the subscription.
* interval (number): The interval for the subscription.
* fixedRate (string): The fixed rate for the subscription.

**Deposit Funds from sender**

This method allows the sender to deposit funds to a specific subscription, performing validation checks on the subscription ID and deposit amount. It also checks if the sender's balance is sufficient for the deposit.

```typescript
/**
 * Deposit funds from the sender to a subscription.
 *
 * @param {DepositFromSenderInput} input - The input data for depositing funds.
 * @returns {Promise<boolean>} Returns `true` if the deposit was successful.
 * @throws {Error} Throws an error if validation checks fail or the deposit operation fails.
 */
const depositeFromSender = async (input: DepositFromSenderInput): Promise<boolean> => {
  // ... code ...
};

```

**Example usage:**

```typescript

const depositData = {
  subscriptionId: 1, // ID of the subscription to deposit to
  amount: 0.1, // Amount to deposit in ETH
};

try {
        await subscription.deposit(depositData);
} catch (error) {
        //...
}

```

DepositFromSenderInput is the input data for depositing funds. Include fields:

* subscriptionId (number): The ID of the subscription to deposit to.
* amount (number): The amount to deposit to the subscription.

**Withdraw Funds from subscription**

This method initiates a withdrawal from the recipient's side of the subscription. It validates the subscription ID and withdrawal amount, ensuring they are valid and positive.

```typescript
/**
 * Withdraw funds from the recipient's side of the subscription.
 *
 * @param {WithdrawPayload} input - The input data for withdrawal.
 * @returns {Promise<boolean>} Returns `true` if the withdrawal was successful.
 * @throws {Error} Throws an error if validation checks fail or the withdrawal operation fails.
 */
const withdrawFromRecipient = async (input: WithdrawPayload): Promise<boolean> => {
  // ... code ...
};

```

**Example usage:**

```typescript
const _withdrawInput: WithdrawPayload = {
    subscription_id: 22,        // ID of the subscription to withdraw from
    withdraw_amount: 1          // Amount to withdraw in Aptos
}

try {
    const output = subscription.withdraw(_withdrawInput);
} catch (error) {
        //...
}

```

WithdrawPayload is the input data for withdrawing funds. Include fields:

* subscriptionId (number): The ID of the subscription to withdraw tokens from.
* amount (number): The amount of tokens to withdraw.

**Cancel a Subscription**

This method allows the sender or recipient to cancel a subscription. Before canceling a subscription, the recipient must have completed all withdrawals. After canceling the subscription, any remaining funds will be refunded to the sender.

```typescript
/**
 * Cancel the subscription.
 *
 * @param {subscriptionId} input - The Subscription id.
 * @returns {Promise<boolean>} Returns `true` if the withdrawal was successful.
 * @throws {Error} Throws an error if validation checks fail or the withdrawal operation fails.
 */
const withdraw = async (input: subscriptionId): Promise<boolean> => {
  // ... code ...
};
```

**Example usage:**

```
const specificSubscriptionId = "your subscription id";

try {
    const result = await subscription.withdraw(BigInt(specificSubscriptionId));
} catch (error) {
  // ...
}
```

* subscriptionId(number): subscriptionId is the only input field. The ID of the subscription to cancel.

#### Query Subscription API

Query subscription API

**List subscriptions of the specific sender**

The API list subscriptions created by a specific sender. const senderSubscriptionData = await subscription.getSubscriptionsBySender(\_sender); const subscriptions = senderSubscriptionData.subscriptionLists; The input parameters for list subscriptions created by a specific sender include:

* sender: the address of sender.

**List subscription of the specific recipient**

The API list subscriptions received by a specific recipient. The query API supports paginate. const recipientSubscriptionData = await subscription.getSubscriptionsByRecipient("0xRecipientAddress");

The input parameters for list subscriptions received by a specific recipient include:

* recipient: address of recipient.

### Github Link:&#x20;

<https://github.com/Move-Flow/sdk.js/tree/lyb/aptos-sdk>


---

# 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://moveflow.gitbook.io/moveflow/developer/subscription-checkout-sdk.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.
