> ## 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.

# Set a customer's auto top-up

> Buy credits with the customer's saved card when their balance runs low.

Saves the customer's own [auto top-up](https://docs.integrateledger.com/pricing/top-ups.md), which replaces their plan's default whole. `enabled: false` turns the plan's default off; `autoTopUp: null` clears the customer's setting so the plan's applies again. Saving restarts a cooldown or suspension after failures.

It buys a credit pack (`addOnId`) or an amount (`amountCents`, within the catalog's `topUps`), one of the two: otherwise `400 invalid_auto_top_up`, `add_on_not_found`, `top_ups_not_configured` or `top_up_amount_out_of_range`.

The response is what [Get a customer's auto top-up](https://docs.integrateledger.com/api-reference/customers/get-auto-top-up.md) returns. Needs the **Customers** permission.


## OpenAPI

````yaml openapi.json POST /v1/customers/auto-top-up
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/customers/auto-top-up:
    post:
      tags:
        - Customers
      summary: Set or clear a customer's auto top-up
      description: >-
        Send autoTopUp: null to clear the customer's setting and follow the
        plan's default.
      operationId: setCustomerAutoTopUp
      parameters:
        - schema:
            type: string
            minLength: 1
            maxLength: 255
            description: Your ID for the customer, such as your user ID.
          required: true
          description: Your ID for the customer, such as your user ID.
          name: customerId
          in: query
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SetCustomerAutoTopUp'
      responses:
        '200':
          description: Saved; the auto top-up that now applies
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerAutoTopUp'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerError'
        '401':
          description: Missing or invalid Ledger API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerError'
        '403':
          $ref: '#/components/responses/InsufficientPermissions'
        '404':
          description: Customer not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '429':
          $ref: '#/components/responses/RateLimited'
      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_customer_auto_top_up(customer_id="user_123",
            auto_top_up={"enabled": True, "thresholdCredits": 100, "addOnId":
            "credits_500", "maxPerPeriod": 3})
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/customers/auto-top-up?customerId=user_123'
            \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"autoTopUp":{"enabled":true,"thresholdCredits":100,"addOnId":"credits_500","maxPerPeriod":3}}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.setCustomerAutoTopUp({ customerId: "user_123", body: {
            autoTopUp: { enabled: true, thresholdCredits: 100, addOnId:
            "credits_500", maxPerPeriod: 3 } } });
components:
  schemas:
    SetCustomerAutoTopUp:
      type: object
      properties:
        autoTopUp:
          $ref: '#/components/schemas/CustomerAutoTopUpInput'
      required:
        - autoTopUp
      additionalProperties: false
    CustomerAutoTopUp:
      type: object
      properties:
        customerId:
          type: string
        autoTopUp:
          type:
            - object
            - 'null'
          properties:
            source:
              type: string
              enum:
                - customer
                - plan
            enabled:
              type: boolean
            thresholdCredits:
              type: integer
            addOnId:
              type:
                - string
                - 'null'
            amountCents:
              type:
                - integer
                - 'null'
              description: 'What one top-up charges: the amount, or the pack''s price.'
            credits:
              type:
                - integer
                - 'null'
              description: >-
                Credits one top-up buys. Null when there is nothing to buy (the
                pack is archived, or top-ups by amount are off).
            maxPerPeriod:
              type: integer
            maxAmountCentsPerPeriod:
              type:
                - integer
                - 'null'
          required:
            - source
            - enabled
            - thresholdCredits
            - addOnId
            - amountCents
            - credits
            - maxPerPeriod
            - maxAmountCentsPerPeriod
        suspended:
          type: boolean
          description: >-
            Three failures in a row: no attempt until the setting is saved again
            or the default card changes.
        coolingDownUntil:
          type:
            - string
            - 'null'
          format: date-time
          description: After a failure, no attempt before this (unless the card changes).
        recent:
          type: array
          items:
            type: object
            properties:
              id:
                type: integer
              trigger:
                type: string
                enum:
                  - auto
                  - manual
              status:
                type: string
                enum:
                  - pending
                  - succeeded
                  - failed
              failureReason:
                type:
                  - string
                  - 'null'
              credits:
                type: integer
              amountCents:
                type: integer
              stripeInvoiceId:
                type:
                  - string
                  - 'null'
              createdAt:
                type: string
                format: date-time
              completedAt:
                type:
                  - string
                  - 'null'
                format: date-time
            required:
              - id
              - trigger
              - status
              - failureReason
              - credits
              - amountCents
              - stripeInvoiceId
              - createdAt
              - completedAt
          description: The last 10 top-ups, newest first.
      required:
        - customerId
        - autoTopUp
        - suspended
        - coolingDownUntil
        - recent
    CustomerError:
      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
    CustomerAutoTopUpInput:
      type:
        - object
        - 'null'
      properties:
        enabled:
          type: boolean
          description: >-
            False turns auto top-ups off for this customer, plan default
            included.
        thresholdCredits:
          type: integer
          exclusiveMinimum: 0
          description: Buy when available credits fall below this. Above 0.
        addOnId:
          type:
            - string
            - 'null'
          minLength: 1
          maxLength: 100
          description: The credit pack to buy. Send this or amountCents.
        amountCents:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 99999999
          description: Or an amount to buy, in cents, within the catalog's topUps bounds.
        maxPerPeriod:
          type: integer
          minimum: 1
          maximum: 31
          description: The most automatic top-ups per billing period, 1 to 31.
        maxAmountCentsPerPeriod:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 99999999
          description: The most they may charge per billing period, in cents.
      required:
        - enabled
        - thresholdCredits
        - maxPerPeriod
      additionalProperties: false
      description: The customer's own setting, or null to follow the plan's default.
    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.