> Source: https://developers.terrawallet.us/docs/pay/subscriptions . Terra Wallet developer docs (early access).

# Subscriptions and plans

A **plan** is a price and an interval. A **subscription** puts a customer on a plan. In v1, every subscription is
**pay each period**: Terra creates a renewal invoice each period and the customer pays it from their own wallet.
Nothing is ever taken from a customer's wallet without their action.

> **Preview.** The API is in early access and may change before launch.

## Create a plan

`POST /v1/plans`:

| Field | Type | Required | Notes |
|---|---|---|---|
| `name` | string | yes | For example `Pro` |
| `amount` | decimal string | yes | The price per period, for example `"10.00"` |
| `currency` | string | yes | A fiat ISO code |
| `interval` | string | yes | `week`, `month` or `year` |
| `interval_count` | integer | yes | Periods between renewals: `1` with `month` is monthly, `3` with `month` is quarterly |
| `trial_days` | integer | no | A free trial before the first payment |

```js
// Preview: create a monthly plan (Node 18 or later).
const plan = await fetch("https://terrawallet.us/api/v1/plans", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.TERRA_SECRET_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": "plan-pro-monthly",
  },
  body: JSON.stringify({ name: "Pro", amount: "10.00", currency: "USD", interval: "month", interval_count: 1, trial_days: 14 }),
}).then((r) => r.json());
```

## Share a subscription link

Every plan has a subscription link: `https://pay.terrawallet.us/s/plan_...`. Customers open it, enter their email
and subscribe. No code is needed on your side.

## Subscribe a customer from your server

`POST /v1/subscriptions` with `plan` (a `plan_...` id) and `customer_email`. The response is the subscription with
its first invoice.

```js
const subscription = await fetch("https://terrawallet.us/api/v1/subscriptions", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.TERRA_SECRET_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": `subscribe-${user.id}-${plan.id}`,
  },
  body: JSON.stringify({ plan: plan.id, customer_email: user.email }),
}).then((r) => r.json());
```

## Renewals

- Renewal invoices are created automatically each period.
- The customer gets reminders 7, 3 and 1 days before each renewal, by email (and by Terra Wallet notification
  and webhook, per the design).
- After a missed renewal the subscription becomes `past_due`; the design adds a grace period and then an
  automatic pause.

## Statuses

| Status | Meaning |
|---|---|
| `trialing` | In the free trial |
| `active` | Paid up for the current period |
| `past_due` | A renewal was not paid in time |
| `paused` | Paused: no renewals are issued |
| `canceled` | Ended |

## Webhooks

`subscription.created`, `subscription.renewed`, `subscription.payment_failed`, `subscription.past_due` and
`subscription.canceled`. Grant or remove access from these events, after verifying each signature (see
[Webhooks](https://developers.terrawallet.us/docs/webhooks.md)).

## Auto-pay with a capped on-chain permission (later, gated)

The Terra Pay design also has an auto-pay mode for EVM chains first, then Tron: the customer signs one capped
permission, for example "Allow up to 10 USDC every 30 days to Merchant X until 23 Nov 2027". The Terra
Subscriptions contract would allow exactly one charge per period, never above the cap and only to the merchant's
registered wallet, and the customer could cancel at any time with immediate on-chain effect.

It is **not available**. Before it touches mainnet it needs an independent contract audit, fuzzing and formal
tests, a testnet beta and a legal review. Solana auto-pay needs its own audited program and comes after that.
Checkout always tells the customer which mode they are agreeing to before they agree.
