# Subscriptions

Subscriptions let you bill a customer the same amount on a schedule (every month, every two weeks, every year) without creating each payment yourself. Fluxis emails the payer an invoice when each period falls due, the payer pays it from the usual Fluxis checkout with any wallet, and you are told by webhook when the money arrives.

::::warning
Subscriptions are in **alpha**. Your account has to be enabled by Fluxis before it can create one; until then every subscription endpoint answers `403`. Contact Fluxis to join. Details may still change, and we will tell enabled merchants before they do.
::::

To build an integration step by step, follow the [subscriptions tutorial](/docs/developers/tutorials/subscriptions). This page is the reference.

## How it works

1. **You create the subscription** with the payer's email and the terms: amount, token, how often. It starts `pending`.
2. **Fluxis opens the first invoice** and emails the payer a link to it. The create response carries the same link as `invoice_url`, so you can also send the payer to it straight away.
3. **The first payment activates the subscription.** It becomes `active`, and the schedule starts from that moment.
4. **Each later period**, Fluxis opens a new invoice, emails it, and sends one reminder halfway to its deadline if it is still unpaid.
5. **A period left unpaid cancels the subscription** with the reason `invoice_unpaid`, and the payer is told by email.

You can cancel at any time, and the payer is emailed that you did. The payer has no cancel link of their own: they ask you, and you cancel it in the dashboard or through the API. You can also change the terms, pause and resume, and resend the open invoice: see [Managing a subscription](#managing-a-subscription).

## Terms

| Field | Type | Rules |
| ----- | ---- | ----- |
| `point_of_sale_id` | uuid | A point of sale of an account enabled for subscriptions. The subscription belongs to that account. |
| `amount` | decimal string | Greater than zero, for example `"9.99"`. |
| `asset` | [unique asset id](/docs/key-concepts/uai) | One token on one network, for example `npolygon_t0xc2132D05D31c914a87C6611C10748AEb04B58e8F` (USDT on Polygon). Must be one of the point of sale's `payment_options`. |
| `interval` | string | `day`, `week`, `month` or `year`. |
| `interval_count` | integer | At least `1`. `interval: "week"` with `interval_count: 2` bills every two weeks. |
| `anchor_day` | integer | `1` to `31`. The day of the month a `month` or `year` subscription bills on. Required, but unused for `day` and `week`. |
| `collection_method` | string | Send `email_invoice`. Omitting it selects `wallet_mandate`, which is reserved for partner wallets. |
| `payer_email` | string | Where invoices go. Required with `email_invoice`. |
| `expires_at` | unix seconds | Optional. How long the payer has to pay the **first** invoice. Defaults to 24 hours from now, and must be in the future. |

Every invalid field is reported in one `400` answer, so you can fix a request in one go.

### What the payer pays in

The invoice is priced in `asset`, converted to that token's reference currency: USDT and USDC are priced in US dollars. On the checkout, the payer can pay with **any token the point of sale accepts**, at the current rate, not only `asset` itself. If you want the payer to pay in exactly one token, give the point of sale only that one.

### When each period falls due

- **Period 0** is the first invoice. It falls due when the subscription is created, and paying it is what activates the subscription.
- **Period 1** falls a whole interval after activation. For `month` and `year` it lands on `anchor_day`: a payer who activates on 20 March with `anchor_day: 1` is next billed on 1 May, never twice inside one interval.
- **Later periods** each fall one interval after the previous one, always on `anchor_day`. In a short month, an `anchor_day` of 29, 30 or 31 falls on the last day.

`next_charge_at` on the subscription always says when the next invoice is due.

### How long the payer has

| Invoice | Deadline |
| ------- | -------- |
| First (period 0) | The subscription's `expires_at`. If it passes unpaid, the subscription is cancelled with the reason `expired`. |
| Every later one | The moment the following period falls due. If it passes unpaid, the subscription is cancelled with the reason `invoice_unpaid`. |

On the checkout, once the payer chooses a token they have until the invoice's deadline to pay, for at most **23 hours** from that choice. If the deadline is less than eight minutes away, they still get eight minutes. Changing the token does not restart the time. If it lapses, Fluxis releases the invoice and emails a fresh one, as long as at least fifteen minutes are left before the deadline.

## Statuses

| `status` | Means |
| -------- | ----- |
| `pending` | Created; the first invoice is not paid yet. |
| `active` | The first invoice was paid. Fluxis bills each period. |
| `paused` | You paused it. No invoices are sent until you resume it; an invoice already open can still be paid. `paused_at` says when. |
| `cancelled` | No more invoices. `cancellation_reason` says why, and `cancelled_at` says when. |

| `cancellation_reason` | Means |
| --------------------- | ----- |
| `merchant_cancelled` | You cancelled it, in the dashboard or through the API. |
| `invoice_unpaid` | A period ran out unpaid. |
| `expired` | The first invoice was never paid before `expires_at`. |

A cancelled subscription never becomes active again. To bill the same payer again, create a new subscription.

## Endpoints

All of them take your usual credentials: the `x-fluxis-api-key` header and a bearer token from `POST /v1/auth/token`. An organization credential can act on any account in the organization that is enabled for subscriptions.

| Action | Endpoint |
| ------ | -------- |
| Create | `POST /v1/subscriptions` |
| Read one | `GET /v1/subscriptions/{id}` |
| List | `GET /v1/subscriptions?status=active&point_of_sale_id={posID}&page=1&page_size=25` |
| Cancel | `POST /v1/subscriptions/{id}/cancel` |
| Change the terms | `PATCH /v1/subscriptions/{id}/terms` |
| Pause | `POST /v1/subscriptions/{id}/pause` |
| Resume | `POST /v1/subscriptions/{id}/resume` |
| Resend the open invoice | `POST /v1/subscriptions/{id}/invoice/resend` |
| Charges of one | `GET /v1/subscriptions/{id}/charges` |
| Accounts you can use | `GET /v1/subscriptions/accounts` |

Listing is newest first. Every query parameter is optional; `page_size` goes up to 100. Reading or cancelling a subscription you do not own answers `404`, the same as one that does not exist. Cancelling one that is already cancelled answers `409`.

Each row in the list carries `latest_charge`, the most recent invoice or charge. `/charges` returns every one of them, newest period first. An email invoice still waiting for payment includes its `payment_link`, so you can send it again yourself. `/accounts` returns the accounts your credential can run subscriptions for; a credential that reaches none gets `403`.

Full request and response schemas are in the [API Reference](/api).

### A subscription

```json
{
  "id": "f2353889-21be-4c84-9852-17f8de63fb42",
  "point_of_sale_id": "dd3ef322-ed47-4c25-8db4-40b19176fd74",
  "status": "active",
  "collection_method": "email_invoice",
  "terms": {
    "amount": "9.99",
    "asset": "npolygon_t0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
    "interval": "month",
    "interval_count": 1,
    "anchor_day": 1
  },
  "payer_email": "payer@example.com",
  "current_period_index": 1,
  "next_charge_at": 1730419200,
  "expires_at": 1727913600
}
```

`current_period_index` is the period the next invoice bills. Timestamps are unix seconds and are left out when not set. `scheduled_change` and `paused_at` appear only while a terms change is waiting or the subscription is paused.

## Managing a subscription

These endpoints work on `email_invoice` subscriptions. Each answers with the subscription as it now reads, except the resend, which answers with the invoice it sent. Each one also emails the payer, so you do not need to tell them yourself.

### Change the terms

`PATCH /v1/subscriptions/{id}/terms` with any of `amount`, `asset`, `interval`, `interval_count` and `anchor_day`. Fields you leave out keep their value. The rules are the same as at creation: `asset` must be one of the point of sale's `payment_options`, and `anchor_day` is `1` to `31`.

```json
{ "amount": "12.50" }
```

The change applies from the **first invoice the payer has not been sent that falls due at least 15 days later**, so the payer always has 15 days' notice of the new terms:

- If the next invoice is less than 15 days away, it keeps the current terms and the change starts with the one after.
- An invoice already open keeps its amount and token.
- The first invoice of the change keeps the date it was already due on. A new `interval` or `anchor_day` counts from that invoice onward.
- Until then the subscription carries `scheduled_change`, with the new `terms`, the `effective_period_index` and its `effective_at`. `terms` keeps showing what is billed now.
- A new change replaces a scheduled one. Sending the terms already in force withdraws it.

Only `active` and `paused` subscriptions can be changed. A `pending` one answers `409`: cancel it and create a new one. A `wallet_mandate` answers `409` too, because the payer approved its exact terms.

### Pause and resume

`POST /v1/subscriptions/{id}/pause` stops invoicing an `active` subscription. While it is `paused`:

- No new invoices and no reminders are sent.
- An invoice already open can still be paid until its deadline. If it is paid, the subscription stays paused. If it runs out unpaid, it is closed, and the subscription is **not** cancelled.
- `next_charge_at` is left out, and `paused_at` says when you paused it.
- You can still cancel it.

`POST /v1/subscriptions/{id}/resume` restarts billing on the **first date of the subscription's own schedule that is still ahead**. Periods that fell due while it was paused are never invoiced and have no charges. The payer is told when the next invoice will arrive.

Pausing a subscription that is not `active`, or resuming one that is not `paused`, answers `409`.

### Resend the invoice

`POST /v1/subscriptions/{id}/invoice/resend` emails the open invoice to the payer again, and answers with that invoice as `/charges` shows it.

- If its `payment_link` still works, the same invoice and link are sent again.
- If the link expired (the payer chose a token and did not pay in time) but the invoice is still within its deadline, Fluxis issues a new link for the same invoice and sends that. The answer is then a new charge, with a new `id` and `payment_link`.

In `/charges`, an open invoice carries `payment_link_expired`, so you can tell the payer will get a new link. `resent_at` says when the invoice was last resent.

You can resend once every **ten minutes** per subscription. Sooner answers `429`, with a `Retry-After` header and `details` such as `retry_after=420`, both in seconds. It answers `409` when there is no open invoice, the invoice is already paid, or the payer's address refuses mail because earlier emails bounced.

## Webhooks

Fluxis tells you about each subscription change through the webhooks on the subscription's account that receive the `subscription` event type:

| `notification_type` | Sent when |
| ------------------- | --------- |
| `subscription_invoice_settled` | A payment for an invoice is applied: `activated: true` on the first invoice, and the new `next_charge_at`. |
| `subscription_updated` | You change the terms, or withdraw a scheduled change. Carries `previous_terms`, `new_terms` and `effective_at`. |
| `subscription_paused` | You pause it. |
| `subscription_resumed` | You resume it. Carries the `next_charge_at` billing restarts on. |
| `subscription_cancelled` | Billing stops: you cancelled it, or a period ran out unpaid. Carries `cancellation_reason` and `cancelled_by`. |
| `subscription_expired` | The first invoice was never paid before `expires_at`. |

Each arrives within about a minute. Delivery is at least once, so deduplicate on `notice_id`. Payloads are in [Webhook Events](/docs/developers/webhook-events#subscription-events).

Because each invoice is a payment request, webhooks that receive `payment_request` also get the usual `payment_request_status_update` notices for it. Handle the subscription notice for billing, and ignore the payment ones you do not need.

## Alpha limitations

- **One way to collect.** Only `email_invoice` is open to merchants.
- **Only email invoices can be changed or paused.** A `wallet_mandate` keeps the terms the payer approved.
- **Notices may arrive out of order.** A pause and a resume made a minute apart can arrive resume first; order them by `paused_at` and `resumed_at`.
- **The settled notice reports the subscription's `asset`,** not the token the payer chose. The `payment_request_id` it carries leads to what was actually paid.
- **A transfer sent after the checkout closes** may not be credited to the invoice. Contact Fluxis with the transaction hash.
- **An underpaid invoice stays open** until it is covered or its deadline passes. Nothing is sent while it waits.
- **The invoice page is the standard checkout.** It does not yet say it belongs to a subscription.
