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

# Change a prepaid quantity

> Raise or lower how much of a prepaid feature the customer buys each period.

Sets a new period total for one [prepaid](https://docs.integrateledger.com/pricing/prepaid-quantity.md) feature of the customer's plan (`"credits"` for its credit pool), the included amount counted. Send an `Idempotency-Key` that identifies this one change, and reuse it on every retry.

* **A raise** is a prorated change on the customer's Stripe bill: one invoice with Stripe's credit for the old packs and charge for the new ones, to the period end, charged to the saved card. Once it's paid, the customer holds the new quantity and is granted the extra units prorated by the time left in the period, rounded down. The next renewal bills and grants the new quantity in full.
* **The same quantity** changes nothing: `{ itemChangeId: null, status: "unchanged" }`.
* **A decrease** waits for the renewal: `{ status: "scheduled" }`, with the new total in `pendingQuantity`. Nothing is refunded and this period keeps its packs and grant; the renewal bills the lower packs and grants the lower quantity. A later change replaces it: another decrease, or the held quantity again to drop it. A raise while one waits is `409 subscription_change_pending`.

The quantity is checked as attach checks it: at least the included amount, whole packs above it (`400 invalid_request` at `quantities.N.quantity`), and at most `maxQuantity` (`400 max_purchase_exceeded`). One feature changes per request. The customer must hold the plan on an active paid bill (`409 plan_not_held`), with no other change pending on it (`409 subscription_change_pending`). An entity's own plan sells no prepaid units, so `entityId` is `400 invalid_request`.

| `status` | Meaning |
| - | - |
| `applied` | Paid and granted; `quantities` holds the new total. |
| `pending_payment` | Send the customer to `paymentUrl`, Stripe's hosted invoice page. |
| `processing` | Stripe hasn't settled it. Retry with the same key. |
| `closed` | Stripe voided the invoice. The old quantity stays. |
| `scheduled` | A decrease, applied at the renewal. |
| `unchanged` | The quantities asked for are the ones held. |

Needs the **Billing** permission.

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/quantities \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Idempotency-Key: raise-user_123-1" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "planId": "pro", "quantities": [{"featureId": "credits", "quantity": 10000}]}'
```


## OpenAPI

````yaml openapi.json POST /v1/billing/quantities
openapi: 3.1.0
info:
  title: Ledger API
  version: 0.1.0
servers:
  - url: https://api.integrateledger.com
    description: Hosted Ledger API
security: []
paths:
  /v1/billing/quantities:
    post:
      tags:
        - Billing
      summary: Change a prepaid quantity
      operationId: setQuantities
      parameters:
        - schema:
            type: string
            minLength: 1
            maxLength: 255
            description: >-
              Required: a raise changes what the customer pays now. Identifies
              this one intended change: reuse it on every retry, and use a new
              one for the next. A key lasts 24 hours.
          required: false
          description: >-
            Required: a raise changes what the customer pays now. Identifies
            this one intended change: reuse it on every retry, and use a new one
            for the next. A key lasts 24 hours.
          name: idempotency-key
          in: header
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QuantitiesInput'
      responses:
        '200':
          description: >-
            The change and the quantities the customer holds. A raise is charged
            now, prorated (Stripe's credit for the old packs and charge for the
            new), and grants the extra units prorated by the time left in the
            period; a decrease is `scheduled` for the renewal, with no refund
            and this period untouched; the same quantity changes nothing
            (`unchanged`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/QuantitiesResult'
        '400':
          description: >-
            Invalid request (a feature the plan doesn't sell prepaid, a quantity
            below the included units or not whole packs, more than one feature
            changed, or entityId), no Idempotency-Key (idempotency_key_required)
            or a malformed one (invalid_idempotency_key), above the entry's
            maxQuantity (max_purchase_exceeded), or a bill Stripe can't charge
            now (ineligible_stripe_adjustment, ineligible_stripe_subscription,
            stripe_plan_mismatch)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '401':
          description: Invalid Ledger API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '402':
          description: >-
            The saved card was declined (payment_failed). Nothing changed; retry
            with the same Idempotency-Key once the card works.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '403':
          $ref: '#/components/responses/InsufficientPermissions'
        '404':
          description: Customer not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '409':
          description: >-
            The customer holds no active paid plan with this planId
            (plan_not_held), another change is pending on the bill, or a raise
            while a decrease waits on its schedule
            (subscription_change_pending), a downgrade (downgrade_scheduled) or
            cancellation (cancellation_scheduled) is scheduled, the plan isn't
            synced to Stripe (plan_not_synced), or the Idempotency-Key was used
            for a different change (idempotency_key_mismatch)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          description: >-
            Unexpected error in Ledger. It has been logged with the request ID;
            retry, or quote request_id when reporting it.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InternalError'
        '502':
          description: >-
            Stripe unavailable or refused the change (stripe_item_change_failed:
            retry with the same Idempotency-Key), its connection incomplete
            (stripe_not_configured), or the signed webhook endpoint unhealthy
            (stripe_webhook_unhealthy)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
        '503':
          description: >-
            Live billing is not enabled, or the change needs another pass with
            Stripe (item_change_reconciliation_required, with itemChangeId):
            retry with the same Idempotency-Key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AttachError'
      security:
        - ApiKey: []
      x-codeSamples:
        - lang: python
          label: Python SDK
          source: >-
            import os

            from integrateledger import ledger


            client = ledger(api_key=os.environ["LEDGER_API_KEY"])

            client.set_quantities(customer_id="user_123", plan_id="pro",
            quantities=[{"feature_id": "credits", "quantity": 10000}],
            idempotency_key="quantity_123")
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/billing/quantities' \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Idempotency-Key: quantity_123' \
              -H 'Content-Type: application/json' \
              --data '{"customerId":"user_123","planId":"pro","quantities":[{"featureId":"credits","quantity":10000}]}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


            const client = new ledger({ apiKey: process.env.LEDGER_API_KEY ?? ""
            });

            await client.setQuantities({ idempotencyKey: "quantity_123", body: {
            customerId: "user_123", planId: "pro", quantities: [{ featureId:
            "credits", quantity: 10000 }] } });
components:
  schemas:
    QuantitiesInput:
      type: object
      properties:
        customerId:
          type: string
          minLength: 1
          maxLength: 255
          description: Your ID for the customer, such as your user ID.
          example: user_123
        planId:
          type: string
          minLength: 1
          maxLength: 255
          description: The customer's plan whose prepaid quantities change.
          example: pro
        entityId:
          type: string
          minLength: 1
          maxLength: 255
          description: >-
            An entity's own plan sells no prepaid units, so sending one is `400
            invalid_request`.
        quantities:
          type: array
          items:
            $ref: '#/components/schemas/PrepaidQuantityInput'
          minItems: 1
          maxItems: 50
          description: >-
            The new period totals, one entry per prepaid feature; send only the
            one that changes. A raise is charged now, a decrease waits for the
            renewal.
      required:
        - customerId
        - planId
        - quantities
      additionalProperties: false
    QuantitiesResult:
      type: object
      properties:
        itemChangeId:
          type:
            - integer
            - 'null'
          description: The change on the customer's bill, or null when nothing changed.
        status:
          type: string
          enum:
            - applied
            - pending_payment
            - processing
            - closed
            - scheduled
            - unchanged
          description: >-
            `applied`: paid and granted. `pending_payment`: the card needs the
            customer (open paymentUrl). `processing`: Stripe hasn't settled it;
            retry with the same key. `closed`: Stripe voided it. `scheduled`: a
            decrease, billed and granted from the renewal (pendingQuantity until
            then). `unchanged`: the quantities asked for are the ones held.
        paymentUrl:
          type:
            - string
            - 'null'
        quantities:
          type: array
          items:
            $ref: '#/components/schemas/PrepaidQuantity'
          description: >-
            The quantities held now: the new ones once applied, the old ones
            before.
      required:
        - itemChangeId
        - status
        - paymentUrl
        - quantities
    AttachError:
      type: object
      properties:
        error:
          type: string
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
        issues:
          type: array
          items:
            type: object
            properties:
              path:
                type: string
              message:
                type: string
            required:
              - path
              - message
          description: >-
            With `invalid_request`: each field that failed validation and why.
            `path` is empty for the body itself, such as an unknown field.
      required:
        - error
    InternalError:
      type: object
      properties:
        error:
          type: string
          example: internal_error
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
        request_id:
          type:
            - string
            - 'null'
      required:
        - error
        - request_id
    PrepaidQuantityInput:
      type: object
      properties:
        featureId:
          type: string
          minLength: 1
          maxLength: 255
          description: >-
            The prepaid feature: a metered feature's ID, or "credits" for the
            plan's credit pool.
          example: credits
        quantity:
          type: integer
          minimum: 0
          maximum: 1000000000
          description: >-
            The period's total, the included units counted: at least the
            included units, whole packs of billingUnits above them, and at most
            maxQuantity. Only the packs above the included units are billed.
          example: 5000
      required:
        - featureId
        - quantity
      additionalProperties: false
    PrepaidQuantity:
      type: object
      properties:
        featureId:
          type: string
          example: credits
        quantity:
          type: integer
          description: The period's total, the included units counted.
          example: 10000
        pendingQuantity:
          type:
            - integer
            - 'null'
          description: A decrease waiting for the renewal, or null when none is waiting.
          example: null
      required:
        - featureId
        - quantity
        - pendingQuantity
    InsufficientPermissionsError:
      type: object
      properties:
        error:
          type: string
          example: insufficient_permissions
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
        requiredScope:
          type: string
          example: customers:read
      required:
        - error
        - requiredScope
    PayloadTooLargeError:
      type: object
      properties:
        error:
          type: string
          example: payload_too_large
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
      required:
        - error
    RateLimitError:
      type: object
      properties:
        error:
          type: string
          example: rate_limited
        message:
          type: string
          description: >-
            What went wrong and what to do about it, for a person or an agent to
            read. Every Ledger error has one. The wording can change: branch on
            `error`, not on this text.
          example: >-
            No customer "user_123" exists in this environment. Create it with
            POST /v1/customers, or check that the API key is for the right
            environment.
      required:
        - error
  responses:
    InsufficientPermissions:
      description: >-
        The API key works but lacks the permission this endpoint needs, named in
        requiredScope. See the API keys guide.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/InsufficientPermissionsError'
    PayloadTooLarge:
      description: 'The request body is over the limit: 1 MiB, or 5 MiB for a webhook.'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/PayloadTooLargeError'
    RateLimited:
      description: >-
        Too many requests. Wait Retry-After seconds, then retry. See the rate
        limits guide.
      headers:
        Retry-After:
          description: Seconds until a request is allowed again
          schema:
            type: integer
        RateLimit-Limit:
          description: 'The burst: requests allowed at once'
          schema:
            type: integer
        RateLimit-Remaining:
          description: Requests left right now
          schema:
            type: integer
        RateLimit-Reset:
          description: Seconds until the full burst is back
          schema:
            type: integer
        RateLimit-Policy:
          description: >-
            The sustained rate per second and the burst, such as
            200;w=1;burst=1000
          schema:
            type: string
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/RateLimitError'
  securitySchemes:
    ApiKey:
      type: http
      scheme: bearer

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.