# Bill a Customer Every Month

This tutorial takes you from an enabled account to a working monthly subscription: you create it, the payer pays the invoice Fluxis emails them, and your server learns about it by webhook. The concepts behind each step are in [Subscriptions](/docs/developers/subscriptions).

::::warning
Subscriptions are in **alpha**, and your account must be enabled by Fluxis first. Without that, `POST /v1/subscriptions` answers `403`.
::::

Once your account is enabled, **Subscriptions** (Alpha) also appears in the dashboard sidebar. There you can create an email-invoiced subscription, list them by status or point of sale, search by customer email, open one to see its billing history and copy the payment link of an open invoice, and cancel it. This tutorial uses the API; each step says where the dashboard can do the same.

## Before you start

You need:

- API credentials for the account, or for its organization. See [API Credentials](/docs/developers/api-credentials).
- A **point of sale** on that account whose `payment_options` include the token you want to bill in.
- A public **HTTPS endpoint** for webhooks. During development, [ngrok](https://ngrok.com) or similar works.

The examples use the staging API, `https://api.stgfluxis.us`. In production, use `https://api.fluxis.us`.

::::note
Staging watches the real networks, so paying an invoice there moves real funds. Test with small amounts, such as 1 USDT.
::::

## 1. Get a token

```bash
curl -s https://api.stgfluxis.us/v1/auth/token \
  -H 'Content-Type: application/json' \
  -d '{"api_key": "YOUR_API_KEY", "api_secret": "YOUR_API_SECRET"}'
```

The token is in `data.token`. Send it on every call below, together with your API key:

```bash
export FLUXIS_API_KEY=YOUR_API_KEY
export FLUXIS_TOKEN=TOKEN_FROM_ABOVE
```

## 2. Pick the point of sale and the token

List your points of sale and note the `id` of the one to bill from, and one of its `payment_options`:

```bash
curl -s 'https://api.stgfluxis.us/v1/pos?page=1&page_size=50' \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN"
```

The token you bill in is a [unique asset id](/docs/key-concepts/uai), such as `npolygon_t0xc2132D05D31c914a87C6611C10748AEb04B58e8F` for USDT on Polygon. The payer can still pay with any other token the point of sale accepts, converted at the current rate.

## 3. Make sure a webhook is registered

Fluxis tells you about paid invoices, cancellations and expiries through the enabled webhooks on the subscription's account whose event types include `subscription`. If the account already has one, skip this step.

Otherwise, create one in the dashboard under **Developers → Webhooks** with the **Subscriptions** event, or register it via the API:

```bash
curl -s https://api.stgfluxis.us/v1/account/YOUR_ACCOUNT_ID/webhook \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "url": "https://your-server.example/webhooks/fluxis",
    "event_types": ["subscription"],
    "description": "Subscription billing"
  }'
```

Store the `secret` from the response now. It is shown only once, and you need it to verify every delivery.

::::info
A webhook receives only the event types it lists. To get payment notices on the same endpoint too, list both: `["payment_request", "subscription"]`. See [Webhooks](/docs/developers/webhooks#event-types).
::::

## 4. Create the subscription

```bash
curl -s https://api.stgfluxis.us/v1/subscriptions \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "point_of_sale_id": "YOUR_POS_ID",
    "collection_method": "email_invoice",
    "payer_email": "payer@example.com",
    "amount": "9.99",
    "asset": "npolygon_t0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
    "interval": "month",
    "interval_count": 1,
    "anchor_day": 1
  }'
```

```json
{
  "data": {
    "id": "f2353889-21be-4c84-9852-17f8de63fb42",
    "collection_method": "email_invoice",
    "invoice_url": "https://checkout.stgfluxis.us/checkout/78ba11aa-4af7-4957-b3ea-138d19e535f8"
  }
}
```

Save `id` against your customer. The subscription is `pending` until the first invoice is paid.

The payer receives the first invoice by email within a few minutes. To skip the wait, send them to `invoice_url` from your own page. If `invoice_url` is missing, the first invoice could not be opened yet; Fluxis retries within a minute and emails it, so you do not need to do anything.

::::tip
Always send `"collection_method": "email_invoice"`. A request without it is read as a wallet subscription, which needs a partner wallet, and is refused if it carries a `payer_email`.
::::

In the dashboard, **Subscriptions → New subscription** creates the same email-invoiced subscription and sends the first invoice right away.

## 5. Handle the webhook

When the payer pays, your endpoint receives:

```json
{
  "data": {
    "notification_type": "subscription_invoice_settled",
    "subscription_id": "f2353889-21be-4c84-9852-17f8de63fb42",
    "point_of_sale_id": "dd3ef322-ed47-4c25-8db4-40b19176fd74",
    "account_id": "41150e54-2c5e-4259-8f79-8a1b61f5e217",
    "payment_request_id": "78ba11aa-4af7-4957-b3ea-138d19e535f8",
    "period_index": 0,
    "amount": "9.99",
    "asset": "npolygon_t0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
    "subscription_status": "active",
    "activated": true,
    "next_charge_at": 1730419200,
    "settled_at": 1728043200
  }
}
```

A minimal handler in Node.js:

```typescript
app.post("/webhooks/fluxis", async (req, res) => {
  if (!verifyWebhookSignature(req.body, req.headers, process.env.FLUXIS_WEBHOOK_SECRET)) {
    return res.status(401).send();
  }
  res.status(200).send(); // answer within 2 seconds, then do the work

  const event = req.body.data;
  switch (event.notification_type) {
    case "subscription_invoice_settled":
      // Delivery is at least once: the same invoice can arrive twice.
      if (await alreadyProcessed(event.payment_request_id)) return;
      if (event.activated) {
        await startService(event.subscription_id);
      }
      await extendServiceUntil(event.subscription_id, event.next_charge_at);
      await markProcessed(event.payment_request_id);
      break;
    case "subscription_cancelled":
    case "subscription_expired":
      // Sent once per subscription, but delivery is at least once.
      await stopService(event.subscription_id);
      break;
    default:
      // payment_request_status_update and anything new: ignore what you don't handle.
      break;
  }
});
```

- Verify the signature first. See [Webhook Signature Verification](/docs/developers/webhook-signature-verification) for `verifyWebhookSignature`.
- Deduplicate on `payment_request_id`. It identifies the invoice, and the same notice can be sent twice.
- Fluxis makes **one delivery attempt** and does not retry. If your endpoint was down, read the subscription (step 6) or check `GET /v1/account/{accountID}/webhook/logs`.
- When billing stops, you receive `subscription_cancelled` or `subscription_expired` instead. See [When a subscription ends](/docs/developers/subscriptions#when-a-subscription-ends).

## 6. Read and list

Read one subscription to check where it stands:

```bash
curl -s https://api.stgfluxis.us/v1/subscriptions/SUBSCRIPTION_ID \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN"
```

List them, optionally by status or point of sale:

```bash
curl -s 'https://api.stgfluxis.us/v1/subscriptions?status=active&page=1&page_size=25' \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN"
```

In the dashboard, **Subscriptions** lists them with the same filters and a search by customer email; open one to see its billing history.

A cancellation for non-payment arrives as `subscription_cancelled` (step 5). A periodic read catches a notice your endpoint missed.

## 7. Cancel

When the customer asks to stop:

```bash
curl -s -X POST https://api.stgfluxis.us/v1/subscriptions/SUBSCRIPTION_ID/cancel \
  -H "x-fluxis-api-key: $FLUXIS_API_KEY" \
  -H "Authorization: Bearer $FLUXIS_TOKEN"
```

The response is the subscription, now `cancelled` with `cancellation_reason: "merchant_cancelled"`. Any invoice still awaiting payment is closed, and the payer is emailed that you cancelled. A second cancel answers `409`.

In the dashboard, open the subscription and choose **Cancel subscription**.

## Testing the full cycle

To see a second invoice without waiting a month, create a test subscription with `"interval": "day"`. The second invoice opens one day after the first is paid.

## Troubleshooting

| Symptom | What to check |
| ------- | ------------- |
| `403` on create | The account is not enabled for subscriptions yet, or the credential does not reach the point of sale's account. |
| `400` "asset is not one this point of sale accepts" | Add the token to the point of sale's `payment_options`, or bill in one it already has. |
| `400` "payer_email is not accepted…" | `collection_method` is missing. Send `email_invoice`. |
| The payer did not get the email | Ask them to check spam, or send them `invoice_url` yourself. |
| The checkout says the payment expired | The payer chose a token and did not pay before the checkout closed. A fresh invoice is emailed automatically. |
| No webhook arrived | Check that the account has an enabled webhook whose event types include `subscription`, and your endpoint answered `200` in time: `GET /v1/account/{accountID}/webhook/logs`. |

If you are stuck, contact Fluxis with the subscription `id`.
