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

> Usage limits, usage alerts and overage controls for one customer or entity.

Saves the customer's own [controls](https://docs.integrateledger.com/pricing/usage-limits.md), or one entity's with `entityId`. Each kind you send (`usageLimits`, `usageAlerts`, `overage`) replaces that kind's entries at this level; a kind you leave out is untouched; `null` clears it back to the plan's.

A customer's entry replaces the plan's entry with the same key whole: a usage limit by its target, interval and filter; an alert by its target and name; an overage entry by its target. Every level's usage limit applies at once: an entity's own limit counts only that entity, inside the customer's.

Refusals: `400 invalid_usage_limit` (a limit on a held or boolean feature, a duplicate), `invalid_alert` (a percentage alert without an allowance, an alert on a boolean feature), `invalid_overage_control` (an entry on a credits, held or boolean feature, or `allowed: true` without an overage price or `freeUnits`), each with `issues`; `404 customer_not_found`, `entity_not_found`.

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


## OpenAPI

````yaml openapi.json POST /v1/customers/controls
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/controls:
    post:
      tags:
        - Customers
      summary: >-
        Set or clear a customer's usage limits, usage alerts and overage
        controls
      description: >-
        Each kind you send replaces that kind's entries at this level; null
        clears it back to the plan's.
      operationId: setCustomerControls
      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
        - schema:
            type: string
            minLength: 1
            maxLength: 255
            description: >-
              One of the customer's entities: its own entries, beside the
              customer's.
          required: false
          description: >-
            One of the customer's entities: its own entries, beside the
            customer's.
          name: entityId
          in: query
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SetCustomerControls'
      responses:
        '200':
          description: Saved; the controls now in force
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerControls'
        '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 or entity 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_controls(customer_id="user_123",
            controls={"usageLimits": [{"featureId": "chat", "limit": 200,
            "interval": "day"}]})
        - lang: bash
          label: cURL
          source: >-
            curl --request POST
            'https://api.integrateledger.com/v1/customers/controls?customerId=user_123'
            \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"usageLimits":[{"featureId":"chat","limit":200,"interval":"day"}]}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.setCustomerControls({ customerId: "user_123", body: {
            usageLimits: [{ featureId: "chat", limit: 200, interval: "day" }] }
            });
components:
  schemas:
    SetCustomerControls:
      type: object
      properties:
        usageLimits:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/UsageLimit'
          maxItems: 50
          description: >-
            Replaces this level's usage limits; null clears them back to the
            plan's. Left out: unchanged.
        usageAlerts:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/UsageAlert'
          maxItems: 50
          description: >-
            Replaces this level's usage alerts; null clears them back to the
            plan's. Left out: unchanged.
        overage:
          type:
            - array
            - 'null'
          items:
            $ref: '#/components/schemas/OverageControl'
          maxItems: 50
          description: >-
            Replaces this level's overage entries; null clears them back to the
            plan's. Left out: unchanged.
      additionalProperties: false
    CustomerControls:
      type: object
      properties:
        customerId:
          type: string
        entityId:
          type:
            - string
            - 'null'
        usageLimits:
          type: array
          items:
            type: object
            properties:
              featureId:
                type: string
              creditPool:
                type: boolean
                const: true
              interval:
                type: string
              anchor:
                type: string
              filter:
                type:
                  - object
                  - 'null'
                additionalProperties: {}
              limit:
                type: integer
              scope:
                type: string
                enum:
                  - customer
                  - entity
              source:
                type: string
                enum:
                  - entity
                  - customer
                  - plan
              used:
                type: integer
              remaining:
                type: integer
              windowStart:
                type: string
                format: date-time
              resetsAt:
                type: string
                format: date-time
            required:
              - interval
              - filter
              - limit
              - scope
              - source
              - used
              - remaining
              - windowStart
              - resetsAt
        usageAlerts:
          type: array
          items:
            type: object
            properties:
              featureId:
                type: string
              creditPool:
                type: boolean
                const: true
              name:
                type: string
              type:
                type: string
              threshold:
                type: number
              usageLimit:
                type: object
                properties:
                  interval:
                    type: string
                  filter:
                    type: object
                    additionalProperties: {}
                required:
                  - interval
              scope:
                type: string
                enum:
                  - customer
                  - entity
              source:
                type: string
                enum:
                  - entity
                  - customer
                  - plan
              firedAt:
                type:
                  - string
                  - 'null'
                format: date-time
                description: When it last fired in its current window; null if not yet.
            required:
              - name
              - type
              - threshold
              - scope
              - source
              - firedAt
        overage:
          type: array
          items:
            type: object
            properties:
              featureId:
                type: string
              creditPool:
                type: boolean
                const: true
              allowed:
                type: boolean
              freeUnits:
                type: integer
              billOverage:
                type: boolean
              source:
                type: string
                enum:
                  - entity
                  - customer
                  - plan
              billed:
                type: integer
                description: >-
                  Overage this period that invoices bill, in units (credits for
                  the pool).
              unbilled:
                type: integer
                description: >-
                  Overage this period no invoice bills: the free buffer and
                  record-only overage.
              freeLeft:
                type: integer
            required:
              - allowed
              - freeUnits
              - billOverage
              - source
              - billed
              - unbilled
              - freeLeft
      required:
        - customerId
        - entityId
        - usageLimits
        - usageAlerts
        - overage
    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
    UsageLimit:
      type: object
      properties:
        featureId:
          type: string
          minLength: 1
          maxLength: 100
          description: >-
            A credits feature (counts uses: the track's quantity) or a metered
            feature (counts units). Send this or creditPool.
        creditPool:
          type: boolean
          description: >-
            Limit the shared credit pool: counts credits drawn by every credits
            feature, from grants and overage.
          const: true
        limit:
          type: integer
          exclusiveMinimum: 0
          maximum: 2147483647
          description: >-
            The most each window may use, above 0. A use that would pass it is
            refused whole with usage_limit_reached; one that lands on it is
            allowed.
        interval:
          type: string
          enum:
            - day
            - week
            - month
            - billing_period
          description: >-
            The window: a day, a week (from Monday in UTC), a month, or the
            billing period.
        anchor:
          type: string
          enum:
            - utc
            - billing_cycle
          description: >-
            Where windows start: `utc` calendar boundaries, or `billing_cycle`
            (from the subscription's billing anchor). Defaults to `utc` for day
            and week, `billing_cycle` for month. Ignored for billing_period.
        filter:
          type: object
          additionalProperties:
            anyOf:
              - type: string
                maxLength: 255
              - type: number
              - type: boolean
          description: >-
            Count only uses whose properties hold every pair. `true` and
            `"true"` differ; numbers compare as numbers.
      required:
        - limit
        - interval
      additionalProperties: false
    UsageAlert:
      type: object
      properties:
        featureId:
          type: string
          minLength: 1
          maxLength: 100
        creditPool:
          type: boolean
          const: true
        name:
          type: string
          minLength: 1
          maxLength: 100
          description: >-
            Unique per target on one level. A customer's alert replaces the
            plan's of the same name.
        type:
          type: string
          enum:
            - usage
            - usage_percentage
            - remaining
            - remaining_percentage
          description: >-
            usage: used this period crosses threshold upward. remaining: what's
            left crosses it downward. The _percentage forms are of the period's
            allowance (or of the usage limit).
        threshold:
          type: number
          exclusiveMinimum: 0
          maximum: 2147483647
        usageLimit:
          type: object
          properties:
            interval:
              type: string
              enum:
                - day
                - week
                - month
                - billing_period
            filter:
              type: object
              additionalProperties:
                anyOf:
                  - type: string
                    maxLength: 255
                  - type: number
                  - type: boolean
              description: >-
                Properties of this use, up to 20 keys (1 to 64 characters) with
                string, number or boolean values. Only matched against usage
                limit filters; not stored.
          required:
            - interval
          additionalProperties: false
          description: >-
            Alert on the window of this target's usage limit (same interval and
            filter) instead of the balance.
      required:
        - name
        - type
        - threshold
      additionalProperties: false
    OverageControl:
      type: object
      properties:
        featureId:
          type: string
          minLength: 1
          maxLength: 100
        creditPool:
          type: boolean
          const: true
        allowed:
          type: boolean
          description: >-
            false: no overage past the balance (a hard stop). Default: overage
            when the plan or item has an overage price. true without a price
            needs freeUnits.
        freeUnits:
          type: integer
          minimum: 0
          maximum: 2147483647
          description: >-
            Units (credits for the pool) past the balance each period that are
            recorded and never billed, before any priced overage.
        billOverage:
          type: boolean
          description: >-
            false: priced overage is recorded under the cap and never invoiced.
            Default true.
      additionalProperties: false
    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.