# Sell prepaid credits

> Let customers buy credits, spend them across their team, and buy more when they run out.

Say customers buy credits, and every member of a customer’s team spends from the same balance. That’s a balance limit on the team, with credits added when they buy.

## 1. Create the balance

The window `total` never resets. The amount is what every team starts with, here 100 free credits:

* TypeScript

  ```ts
  import { createClient } from "@limitry/sdk";

  // Uses the LIMITRY_API_KEY environment variable.
  const limitry = createClient();

  const limit = await limitry.limits.create({
    name: "Credits",
    subject: { kind: "team" },
    action: "*",
    amount: 100,
    unit: "credits",
    window: "total",
  });
  console.log(limit.id);
  ```

* Python

  ```python
  from limitry import Limitry

  # Uses the LIMITRY_API_KEY environment variable.
  limitry = Limitry()

  limit = limitry.limits.create(
      name="Credits",
      subject={"kind": "team"},
      action="*",
      amount=100,
      unit="credits",
      window="total",
  )
  print(limit.id)
  ```

* CLI

  ```bash
  limitry limits create --name Credits --subject '{"kind":"team"}' --action "*" \
    --amount 100 --unit credits --window total
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/limits \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Credits","subject":{"kind":"team"},"action":"*",
         "amount":100,"unit":"credits","window":"total"}'
  ```

## 2. Add credits when a customer pays

In your payment webhook, add the credits they bought. Use the payment’s id as the idempotency key, so a redelivered webhook can’t add credits twice:

* TypeScript

  ```ts
  import { createClient } from "@limitry/sdk";

  // Uses the LIMITRY_API_KEY environment variable.
  const limitry = createClient();

  const credit = await limitry.limits.credits.create(
    "lmt_3f0c…",
    {
      subject: { kind: "team", id: "team_42" },
      amount: 5000,
      note: "5,000 credits",
    },
    { idempotencyKey: "payment_8231" },
  );
  console.log(credit);
  ```

* Python

  ```python
  from limitry import Limitry

  # Uses the LIMITRY_API_KEY environment variable.
  limitry = Limitry()

  credit = limitry.limits.credits.create(
      "lmt_3f0c…",
      subject={"kind": "team", "id": "team_42"},
      amount=5000,
      note="5,000 credits",
      idempotency_key="payment_8231",
  )
  print(credit)
  ```

* CLI

  ```bash
  limitry limits credits create lmt_3f0c… --subject '{"kind":"team","id":"team_42"}' \
    --amount 5000 --note "5,000 credits" --idempotency-key payment_8231
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/limits/$LIMIT_ID/credits \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Idempotency-Key: payment_8231" \
    -d '{ "subject": { "kind": "team", "id": "team_42" }, "amount": 5000,
          "note": "5,000 credits" }'
  ```

Credits go to the balance in the key’s environment. Your payment handler in production needs a live key (`lim_live_…`), with `$LIMIT_ID` the live limit’s id. A test key adds test credits, which suits your payment provider’s test mode. See [Test and live](/docs/test-and-live/).

## 3. Spend from the team’s balance

Check as the user, and name the team as the shared subject. Any limits you set on the user still apply as well:

* TypeScript

  ```ts
  const report = await limitry.guard(
    {
      subject: { kind: "user", id: user.id },
      shared: [{ kind: "team", id: user.teamId }],
      action: "report",
      cost: 25,
    },
    () => buildReport(params),
  );
  ```

* Python

  ```python
  with limitry.guard(
      subject={"kind": "user", "id": user.id},
      shared=[{"kind": "team", "id": user.team_id}],
      action="report",
      cost=25,
  ):
      report = build_report(params)
  ```

However many teammates spend at the same moment, a check never takes the balance below zero: Limitry refuses a cost that doesn’t fit. Two things can still take it below zero: committing a reservation at more than it reserved, which Limitry charges in full (see [Reservations](/docs/reservations/)), and taking back credits the team already spent (below).

## 4. When the balance runs out

The check gets a no with `retryAfterSeconds: null`, because waiting won’t help. Use that to offer more credits rather than “try again later”.

To show the balance in your product, read `limits[].remaining` from any check’s response, or make a `preview` check, which counts nothing.

## 5. When you refund a payment

In your refund webhook, take the credits back with a negative amount, a note, and the refund’s id as the idempotency key:

* TypeScript

  ```ts
  import { createClient } from "@limitry/sdk";

  // Uses the LIMITRY_API_KEY environment variable.
  const limitry = createClient();

  const credit = await limitry.limits.credits.create(
    "lmt_3f0c…",
    {
      subject: { kind: "team", id: "team_42" },
      amount: -5000,
      note: "Refund of payment_8231",
    },
    { idempotencyKey: "refund_311" },
  );
  console.log(credit);
  ```

* Python

  ```python
  from limitry import Limitry

  # Uses the LIMITRY_API_KEY environment variable.
  limitry = Limitry()

  credit = limitry.limits.credits.create(
      "lmt_3f0c…",
      subject={"kind": "team", "id": "team_42"},
      amount=-5000,
      note="Refund of payment_8231",
      idempotency_key="refund_311",
  )
  print(credit)
  ```

* CLI

  ```bash
  limitry limits credits create lmt_3f0c… --subject '{"kind":"team","id":"team_42"}' \
    --amount -5000 --note "Refund of payment_8231" --idempotency-key refund_311
  ```

* curl

  ```bash
  curl -X POST https://api.limitry.com/v1/limits/$LIMIT_ID/credits \
    -H "Authorization: Bearer $LIMITRY_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Idempotency-Key: refund_311" \
    -d '{ "subject": { "kind": "team", "id": "team_42" }, "amount": -5000,
          "note": "Refund of payment_8231" }'
  ```

If the team already spent the credits, the balance goes below zero. Their checks then get a no until they buy more. A chargeback works the same way.

Related: [Pools, balances and budgets](/docs/pools-and-budgets/), [Idempotency](/docs/idempotency/).
