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

# Create or update an entity

> Add a user, seat or workspace to a customer, with its own allowance or a cap on the shared credits.

An entity is a user, seat, agent or workspace inside one customer. Send `customerId` and your own `entityId`, with either or both of:

* `allowanceCredits`: credits per billing period that only this entity can spend. It spends them before the customer's shared credits. They're granted while the customer has a plan (paid or free), for the plan's billing period, so a customer without a plan gives its entities no allowance.
* `capCredits`: the most this entity may draw from the customer's shared credits and overage per billing period. Its own allowance doesn't count against it. `0` lets it spend only its allowance.

Then pass `entityId` to [check](https://docs.integrateledger.com/api-reference/usage/check.md), [track](https://docs.integrateledger.com/api-reference/usage/track.md) and [reservations](https://docs.integrateledger.com/customers/reservations.md) to spend as that entity. A track that would pass the cap returns `409 entity_cap_reached` with `capRemaining`.

The same call updates an entity: omitted fields keep their values and `null` clears one. A changed allowance applies to the current period at once; credits already spent stay spent. Calling it for a [deleted](https://docs.integrateledger.com/api-reference/entities/delete.md) entity's ID restores it (`created: true`).

The response is the entity with its balance, as [Get an entity](https://docs.integrateledger.com/api-reference/entities/get.md) returns it. An unknown customer returns `404 customer_not_found`. Needs the **Customers** permission at Write.


## OpenAPI

````yaml openapi.json POST /v1/entities
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/entities:
    post:
      tags:
        - Entities
      summary: Create or update an entity
      operationId: upsertEntity
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpsertEntityInput'
      responses:
        '200':
          description: The entity, created, updated or restored
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UpsertEntityResponse'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntityError'
        '401':
          description: Invalid Ledger API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntityError'
        '403':
          $ref: '#/components/responses/InsufficientPermissions'
        '404':
          description: Customer not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntityError'
        '413':
          $ref: '#/components/responses/PayloadTooLarge'
        '429':
          $ref: '#/components/responses/RateLimited'
        '503':
          description: >-
            The customer is busy with another billing change. Nothing was
            changed; retry the same request.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EntityError'
      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.upsert_entity(customer_id="user_123", entity_id="alice",
            allowance_credits=500, cap_credits=2000)
        - lang: bash
          label: cURL
          source: |-
            curl --request POST 'https://api.integrateledger.com/v1/entities' \
              -H "Authorization: Bearer $LEDGER_API_KEY" \
              -H 'Content-Type: application/json' \
              --data '{"customerId":"user_123","entityId":"alice","allowanceCredits":500,"capCredits":2000}'
        - lang: typescript
          label: TypeScript SDK
          source: >-
            import ledger from "integrateledger";


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

            await client.upsertEntity({ customerId: "user_123", entityId:
            "alice", allowanceCredits: 500, capCredits: 2000 });
components:
  schemas:
    UpsertEntityInput:
      type: object
      properties:
        customerId:
          type: string
          minLength: 1
          maxLength: 255
          description: Your ID for the customer, such as your user ID.
        entityId:
          type: string
          minLength: 1
          maxLength: 255
          description: Your ID for the user, seat or workspace, unique within the customer.
        name:
          type:
            - string
            - 'null'
          minLength: 1
          maxLength: 255
          description: A label for the entity, such as the user's name. Null clears it.
        allowanceCredits:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
          maximum: 2147483647
          description: >-
            Credits per billing period only this entity can spend, before the
            shared pool. Granted while the customer has a plan. Null: none.
        capCredits:
          type:
            - integer
            - 'null'
          minimum: 0
          maximum: 2147483647
          description: >-
            The most this entity may draw from the customer's shared credits and
            overage per billing period. Null: no cap.
      required:
        - customerId
        - entityId
      additionalProperties: false
    UpsertEntityResponse:
      allOf:
        - $ref: '#/components/schemas/Entity'
        - type: object
          properties:
            created:
              type: boolean
              description: True when the entity is new, or a deleted one restored.
          required:
            - created
    EntityError:
      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.
      required:
        - error
    Entity:
      type: object
      properties:
        entityId:
          type: string
        customerId:
          type: string
        name:
          type:
            - string
            - 'null'
        allowanceCredits:
          type:
            - integer
            - 'null'
          exclusiveMinimum: 0
        capCredits:
          type:
            - integer
            - 'null'
          minimum: 0
        createdAt:
          type: string
          format: date-time
        availableCredits:
          type: integer
          minimum: 0
          description: >-
            What the entity can spend now: its allowance left, plus the shared
            credits it may still draw under its cap.
        allowanceRemaining:
          type: integer
          minimum: 0
          description: >-
            Its own credits left this period, after its locks' holds: its
            allowance and the credits of any license seat it holds. 0 without
            either.
        periodUsage:
          type: integer
          minimum: 0
          description: >-
            Shared credits and overage it drew this period. Its allowance
            doesn't count.
        capRemaining:
          type:
            - integer
            - 'null'
          minimum: 0
          description: >-
            Shared credits it may still draw this period, after its locks'
            holds. Null without a cap.
        heldCredits:
          type: integer
          minimum: 0
          description: Credits its active locks hold.
        periodStart:
          type: string
          format: date-time
        periodEnd:
          type: string
          format: date-time
        seats:
          type: array
          items:
            $ref: '#/components/schemas/EntitySeat'
          description: >-
            The license seats it holds, by seat plan (POST /v1/licenses/assign).
            Always sent.
        plans:
          type: array
          items:
            $ref: '#/components/schemas/EntityPlan'
          description: >-
            The plans attached to the entity (POST /v1/billing/attach with
            `entityId`), its own grants and lifecycle, billed on the customer's
            bill.
      required:
        - entityId
        - customerId
        - name
        - allowanceCredits
        - capCredits
        - createdAt
        - availableCredits
        - allowanceRemaining
        - periodUsage
        - capRemaining
        - heldCredits
        - periodStart
        - periodEnd
        - plans
    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
    EntitySeat:
      type: object
      properties:
        planId:
          type: string
          description: The seat plan.
        slot:
          type: integer
          exclusiveMinimum: 0
      required:
        - planId
        - slot
    EntityPlan:
      type: object
      properties:
        planId:
          type: string
        group:
          type: string
          description: 'The plan''s product line: an entity holds at most one plan per group.'
        status:
          type: string
          enum:
            - pending
            - active
            - canceling
          description: >-
            `pending` while its change's invoice is unpaid; `canceling` until it
            ends at `endsAt`.
        startsAt:
          type: string
          format: date-time
        endsAt:
          type:
            - string
            - 'null'
          format: date-time
      required:
        - planId
        - group
        - status
        - startsAt
        - endsAt
  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.