> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microapp.io/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Microapp subscriptions for any web app.

The TypeScript Payments SDK enables you to subscribe users without requiring a particular frontend framework.

## Getting Started

### 1. Install the SDK

Install the SDK with your preferred package manager.

<CodeGroup>
  ```bash npm theme={null}
  npm install @microapp-io/payments
  ```

  ```bash yarn theme={null}
  yarn add @microapp-io/payments
  ```

  ```bash pnpm theme={null}
  pnpm add @microapp-io/payments
  ```
</CodeGroup>

### 2. Import and Initialize the Payments SDK

To use the Payments SDK in your code, import it in any file where you need to use it.

```typescript theme={null}
import { Payments } from '@microapp-io/payments';

const payments = new Payments();
```

For local development, we recommend using the `sandbox` feature on the `Payments` class to return mocked subscriptions:

```typescript theme={null}
import { Payments, MicroappSubscriptionPlanCycle } from '@microapp-io/payments';

const payments = new Payments({
  sandbox: {
    enabled: process.env.NODE_ENV !== 'production',
    subscription: () => ({
      id: 'some-subscription-id',
      appId: 'some-app-id',
      user: {
        id: 'some-user-id',
        email: 'email@example.com',
        name: 'FirstName LastName',
      },
      active: true,
      subscriptionPlan: {
        id: 'some-subscription-plan-id',
        name: 'free',
        priceInCents: 0,
        cycle: SubscriptionPlanCycle.ONE_TIME,
        features: [
          {
            name: 'some-feature-name',
            description: 'some-feature-description'
          },
        ],
        createdAt: new Date(),
        updatedAt: new Date(),
      },
      createdAt: new Date(),
      updatedAt: new Date(),
    }),
  },
});
```

<Warning>
  The `sandbox` prop should only be used for local development. Do not use it in production.
</Warning>

### 3. Check if the User is subscribed

The `hasSubscription` method returns the user's subscription status: `true` if subscribed and `false` otherwise.

```typescript theme={null}
import { Payments } from '@microapp-io/payments';

const hasSubscription = await payments.hasSubscription();

if (hasSubscription) {
  console.log('User is subscribed');
} else {
  console.log('User is not subscribed');
}
```

### 4. Get the User's subscription information

The `getSubscription` method returns the user's subscription information.

```typescript theme={null}
import { Payments } from '@microapp-io/payments';

const userSubscription = await payments.getSubscription();

console.log('User subscription:', userSubscription);
```

### 5. Prompt the User to subscribe

If the user is not subscribed, you can prompt them to subscribe by calling the `requireSubscription` method.

```typescript theme={null}
import { Payments } from '@microapp-io/payments';

await payments.requireSubscription();
```

### 6. Get Notified when the User is subscribed

You can get notified when the user subscribes or changes its subscription using `onUserSubscribed` method.

```typescript theme={null}
import { Payments } from '@microapp-io/payments';

const unsubscribeCallback = payments.onUserSubscribed((userSubscription) => {
  console.log(`User just subscribed`, userSubscription);
});

await payments.requireSubscription();

// Unsubscribe the callback
unsubscribeCallback();
```
