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

# Top up by amount

> Buy credits worth any amount within the catalog's top-up bounds.

Buys `amountCents × creditsPerUsd / 100` credits (rounded down) for the customer, within the catalog's [`topUps`](https://docs.integrateledger.com/pricing/top-ups) bounds.

* `paymentMethod: "checkout"` (the default) answers the pending top-up with a `checkoutUrl`. The credits are granted when the customer pays.
* `paymentMethod: "saved"` charges the default card now as a one-item invoice and answers the top-up `succeeded`, or `402 payment_failed` or `authentication_required` (send the customer through Checkout instead). A customer with no saved card gets `400 no_payment_method`.

An amount outside the bounds is `400 top_up_amount_out_of_range`; without `topUps` in the catalog, `400 top_ups_not_configured`. Needs the **Billing** permission.


## OpenAPI

````yaml openapi.json POST /v1/billing/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/billing/top-up:
    post:
      tags:
        - Billing
      summary: Top up a customer's credits by amount
      description: Leave paymentMethod out to get a Checkout URL instead.
      operationId: topUp
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TopUpInput'
      responses:
        '200':
          description: >-
            Saved card: the top-up, succeeded and granted. Checkout: the pending
            top-up with its checkoutUrl.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUp'
        '400':
          description: >-
            Invalid request, an amount out of range
            (`top_up_amount_out_of_range`), top-ups by amount not set up
            (`top_ups_not_configured`), or no saved card (`no_payment_method`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
        '401':
          description: Missing or invalid Ledger API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
        '402':
          description: >-
            The saved card was declined (`payment_failed`) or needs the customer
            to authenticate (`authentication_required`)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
        '403':
          $ref: '#/components/responses/InsufficientPermissions'
        '404':
          description: Customer not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '429':
          $ref: '#/components/responses/RateLimited'
        '502':
          description: Stripe isn't connected, or didn't answer
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
        '503':
          description: Live charges are off for this workspace
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopUpError'
      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.top_up(customer_id="user_123", amount_cents=2500,
            payment_method="saved")
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/billing/top-up' \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"customerId":"user_123","amountCents":2500,"paymentMethod":"saved"}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.topUp({ body: { customerId: "user_123", amountCents:
            2500, paymentMethod: "saved" } });
components:
  schemas:
    TopUpInput:
      type: object
      properties:
        customerId:
          type: string
          minLength: 1
          maxLength: 255
          description: Your ID for the customer, such as your user ID.
        amountCents:
          type: integer
          exclusiveMinimum: 0
          maximum: 99999999
          description: >-
            What to charge, in US cents, within the catalog's topUps.minCents
            and maxCents. Buys amountCents × creditsPerUsd / 100 credits,
            rounded down.
        paymentMethod:
          type: string
          enum:
            - checkout
            - saved
          description: >-
            `checkout` (the default) answers a Stripe Checkout URL; `saved`
            charges the customer's default card now.
        successUrl:
          type: string
          maxLength: 2048
          description: >-
            Checkout only: where Stripe sends the customer after paying.
            Overrides Settings › Checkout, with attach's rules.
        cancelUrl:
          type: string
          maxLength: 2048
          description: >-
            Checkout only: where Stripe sends the customer if they leave.
            Overrides Settings › Checkout.
      required:
        - customerId
        - amountCents
      additionalProperties: false
    TopUp:
      type: object
      properties:
        id:
          type: integer
        customerId:
          type: string
        status:
          type: string
          enum:
            - pending
            - succeeded
            - failed
        failureReason:
          type:
            - string
            - 'null'
        credits:
          type: integer
        amountCents:
          type: integer
        stripeInvoiceId:
          type:
            - string
            - 'null'
        checkoutUrl:
          type:
            - string
            - 'null'
          description: 'Checkout only: where to send the customer to pay.'
      required:
        - id
        - customerId
        - status
        - failureReason
        - credits
        - amountCents
        - stripeInvoiceId
        - checkoutUrl
    TopUpError:
      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
    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.