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

# Configure the ledger counterparty of one provider in one ledger of a tenant

> Sets the ledger account used as the counterparty of the postings of the operations of one provider of the tenant of the credentials you authenticate with, in the ledger named in the path, for one asset. A ledger with no configured counterparty uses `@external/<asset>`. The write and its history line are recorded in one transaction, with the actor, the instant, the previous and the new account, the reason and the trace. To go back to the default, write `@external/<asset>`.

VALIDATION. `accountAlias` must match its pattern, `assetCode` must be uppercase letters, and an alias in the default form must be exactly `@external/` followed by `assetCode`; otherwise 400 PBP-0001, before any write. The ledger of the path must be a canonical UUID, in any case, and is kept in lower case; otherwise 400 PBP-0026. A provider not mounted on this installation is refused with 422 PBP-0702 reason PROVIDER_NOT_INSTALLED, and one not connected to the tenant with reason PROVIDER_NOT_CONNECTED. The account of any alias other than the default is then looked up in Midaz, in the ledger of the path, under the organization in `X-Organization-Id`, with the check every posting against it applies: absent there, 422 PBP-0702 reason ACCOUNT_NOT_FOUND; not of type `external`, compared exactly, ACCOUNT_NOT_EXTERNAL; not of the asset `assetCode`, ACCOUNT_ASSET_MISMATCH. Nothing is written on any refusal. Midaz unavailable answers 503 PBP-0008 and nothing is written, as does a connection that cannot be read. Midaz applies no balance floor to an `external` account, so postings run against the provider's position; an ordinary account would have a customer's funds debited instead. An account that stops passing that check after the write makes the posting fail with 503 PBP-0706, never sent to `@external/<asset>`. A write that repeats the account in effect (the default, with no configured counterparty) is refused with 422 PBP-0702 reason NO_CHANGE. A write made while a payment of that provider in that ledger waits for the ledger operation of its pending debit, or a settled boleto of theirs for that of its settlement credit, is refused with 422 PBP-0702 reason POSTINGS_IN_FLIGHT, nothing written; resend it under the same Idempotency-Key once the posting settles.

ONE COUNTERPARTY PER ORGANIZATION. Midaz accounting routes belong to the organization: the external leg of `BOLETO_IN_DEBIT`, `PAYMENT_OUT_CREDIT` and `DARF_OUT_CREDIT` names one account for every ledger of it, so an organization has one effective counterparty. A write of an account other than `@external/<asset>` lists the ledgers of the organization in `X-Organization-Id` in Midaz (Midaz unavailable answers 503 PBP-0008 and nothing is written) and is refused with 422 PBP-0702 reason LEDGER_COUNTERPARTY_CONFLICT, nothing written, when a row of another provider, in any ledger of the organization, or of the same provider, in another ledger, names an account other than `@external/<asset>` and other than the one written. A provider or a ledger with no row, or with a row of `@external/<asset>`, never conflicts: its postings are refused until the same account is written there too, so the writes of several providers and ledgers may go in any order. A write of `@external/<asset>` is never refused for a conflict; that provider's postings are then refused until the organization's accounting routes name the account in effect again.

Writes in the same organization and asset are serialized; one that cannot take its turn in time answers 503 PBP-0008, nothing written, and can be resent under the same Idempotency-Key.

OPTIMISTIC CONCURRENCY. `version` must be the current version of the (ledger, provider, asset) counterparty (0 when never configured), read with getLedgerCounterparty. A stale version is refused with 409 PBP-0705.

EFFECT. A posting of an operation uses the counterparty of the ledger and of the provider recorded on it, never the one of the provider currently on the route. A posting reads the counterparty each time it is presented, so one presented again after a change would carry the new account: while a posting of that provider in that ledger may still be presented again, the write is refused with 422 PBP-0702 reason POSTINGS_IN_FLIGHT. The commit or cancel of a reserve follows the original pending transaction in the ledger.

IDEMPOTENT under Idempotency-Key: a retry with the same key replays the stored 200. A missing or malformed key is refused with 400 PBP-0012.

WHO MAY CALL IT: `provider-routes:configure`; otherwise 403 PBP-0004.



## OpenAPI

````yaml /en/openapi/v3-current/payments.yaml post /v1/admin/ledgers/{ledger_id}/providers/{provider_type}/ledger-counterparty
openapi: 3.1.0
info:
  description: >-
    API for the Lerian Payments interface via BTG. It covers boleto issuance,
    cancellation, and queries; bill payments (bankslip, utilities, and DARF) and
    queries; aggregated boleto and payment dashboards; provider connection and
    outbound webhook configuration; and the provider webhook receiver for
    settlement events.


    ERROR BODIES. A failed request arrives as an RFC 9457 problem document on
    application/problem+json. Branch on the code member. A license refusal
    carries an LCS code, listed in the License Manager error list.


    NOTICES. The webhooks section describes each notice Payments posts to the
    tenant's callback URL. Delivery does not guarantee order: order a boleto's
    notices by the occurredAt member of the body. Notices of one boleto recorded
    together never tie and follow the order of the facts; notices recorded at
    different moments can tie, and in a tie the boleto query tells the current
    state. The same notice can arrive more than once: after verifying its
    signature and its X-Webhook-Timestamp, discard repetitions by the eventId
    member of the body, equal to the X-Webhook-Id header and the same on every
    attempt.
  title: Payments — via BTG API
  version: 1.0.0
servers:
  - url: https://api.example.com
    description: >-
      Replace the example host with the URL provided during environment
      onboarding.
security: []
tags:
  - description: >-
      Tenant-level setup: connecting the banking provider and configuring where
      status notifications are delivered.
    name: Admin
  - description: Issuing, querying and cancelling boletos, and retrieving the rendered PDF.
    name: Boletos
  - description: >-
      Aggregated counts, amounts and breakdowns over a period, for boletos and
      for payments.
    name: Dashboards
  - description: >-
      The notices Payments posts to the tenant's callback URL, one per type
      enabled in enabledEvents, each signed with the configuration's secret.
      Each item describes the body, the delivery headers, when the notice is
      sent and which providers send it.
    name: Notices
  - description: >-
      Paying bankslips, utility bills and DARF tax slips, and querying those
      payments.
    name: Payments
  - description: >-
      The webhook the payment provider posts settlement events to. Authenticated
      with a credential agreed with the provider, not with the platform identity
      used by the rest of this API.
    name: Settlement
paths:
  /v1/admin/ledgers/{ledger_id}/providers/{provider_type}/ledger-counterparty:
    post:
      tags:
        - Admin
      summary: >-
        Configure the ledger counterparty of one provider in one ledger of a
        tenant
      description: >-
        Sets the ledger account used as the counterparty of the postings of the
        operations of one provider of the tenant of the credentials you
        authenticate with, in the ledger named in the path, for one asset. A
        ledger with no configured counterparty uses `@external/<asset>`. The
        write and its history line are recorded in one transaction, with the
        actor, the instant, the previous and the new account, the reason and the
        trace. To go back to the default, write `@external/<asset>`.


        VALIDATION. `accountAlias` must match its pattern, `assetCode` must be
        uppercase letters, and an alias in the default form must be exactly
        `@external/` followed by `assetCode`; otherwise 400 PBP-0001, before any
        write. The ledger of the path must be a canonical UUID, in any case, and
        is kept in lower case; otherwise 400 PBP-0026. A provider not mounted on
        this installation is refused with 422 PBP-0702 reason
        PROVIDER_NOT_INSTALLED, and one not connected to the tenant with reason
        PROVIDER_NOT_CONNECTED. The account of any alias other than the default
        is then looked up in Midaz, in the ledger of the path, under the
        organization in `X-Organization-Id`, with the check every posting
        against it applies: absent there, 422 PBP-0702 reason ACCOUNT_NOT_FOUND;
        not of type `external`, compared exactly, ACCOUNT_NOT_EXTERNAL; not of
        the asset `assetCode`, ACCOUNT_ASSET_MISMATCH. Nothing is written on any
        refusal. Midaz unavailable answers 503 PBP-0008 and nothing is written,
        as does a connection that cannot be read. Midaz applies no balance floor
        to an `external` account, so postings run against the provider's
        position; an ordinary account would have a customer's funds debited
        instead. An account that stops passing that check after the write makes
        the posting fail with 503 PBP-0706, never sent to `@external/<asset>`. A
        write that repeats the account in effect (the default, with no
        configured counterparty) is refused with 422 PBP-0702 reason NO_CHANGE.
        A write made while a payment of that provider in that ledger waits for
        the ledger operation of its pending debit, or a settled boleto of theirs
        for that of its settlement credit, is refused with 422 PBP-0702 reason
        POSTINGS_IN_FLIGHT, nothing written; resend it under the same
        Idempotency-Key once the posting settles.


        ONE COUNTERPARTY PER ORGANIZATION. Midaz accounting routes belong to the
        organization: the external leg of `BOLETO_IN_DEBIT`,
        `PAYMENT_OUT_CREDIT` and `DARF_OUT_CREDIT` names one account for every
        ledger of it, so an organization has one effective counterparty. A write
        of an account other than `@external/<asset>` lists the ledgers of the
        organization in `X-Organization-Id` in Midaz (Midaz unavailable answers
        503 PBP-0008 and nothing is written) and is refused with 422 PBP-0702
        reason LEDGER_COUNTERPARTY_CONFLICT, nothing written, when a row of
        another provider, in any ledger of the organization, or of the same
        provider, in another ledger, names an account other than
        `@external/<asset>` and other than the one written. A provider or a
        ledger with no row, or with a row of `@external/<asset>`, never
        conflicts: its postings are refused until the same account is written
        there too, so the writes of several providers and ledgers may go in any
        order. A write of `@external/<asset>` is never refused for a conflict;
        that provider's postings are then refused until the organization's
        accounting routes name the account in effect again.


        Writes in the same organization and asset are serialized; one that
        cannot take its turn in time answers 503 PBP-0008, nothing written, and
        can be resent under the same Idempotency-Key.


        OPTIMISTIC CONCURRENCY. `version` must be the current version of the
        (ledger, provider, asset) counterparty (0 when never configured), read
        with getLedgerCounterparty. A stale version is refused with 409
        PBP-0705.


        EFFECT. A posting of an operation uses the counterparty of the ledger
        and of the provider recorded on it, never the one of the provider
        currently on the route. A posting reads the counterparty each time it is
        presented, so one presented again after a change would carry the new
        account: while a posting of that provider in that ledger may still be
        presented again, the write is refused with 422 PBP-0702 reason
        POSTINGS_IN_FLIGHT. The commit or cancel of a reserve follows the
        original pending transaction in the ledger.


        IDEMPOTENT under Idempotency-Key: a retry with the same key replays the
        stored 200. A missing or malformed key is refused with 400 PBP-0012.


        WHO MAY CALL IT: `provider-routes:configure`; otherwise 403 PBP-0004.
      operationId: configureLedgerCounterparty
      parameters:
        - description: >-
            Midaz ledger whose counterparty is read or written, the same
            identifier the boleto and the payment record as their ledger. Each
            ledger has its own counterparty per provider and asset. Must be a
            canonical UUID (any case); otherwise 400 PBP-0026.
          in: path
          name: ledger_id
          required: true
          schema:
            description: >-
              Midaz ledger whose counterparty is read or written, the same
              identifier the boleto and the payment record as their ledger. Each
              ledger has its own counterparty per provider and asset. Must be a
              canonical UUID (any case); otherwise 400 PBP-0026.
            examples:
              - 0199a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a6c
            maxLength: 100
            minLength: 1
            type: string
        - description: >-
            Provider whose ledger counterparty is read or written, as the
            capabilities read names it. Matched case-insensitively.
          in: path
          name: provider_type
          required: true
          schema:
            description: >-
              Provider whose ledger counterparty is read or written, as the
              capabilities read names it. Matched case-insensitively.
            examples:
              - TESTPROV_A
            maxLength: 20
            minLength: 1
            pattern: ^[A-Za-z][A-Za-z0-9_]{0,19}$
            type: string
        - description: >-
            Midaz organization that owns the ledger in the path. Required: a
            request without it, or with a value that is not a canonical UUID, is
            refused with 400 PBP-0025. Used only to look the account of
            accountAlias up in that ledger; it does not select the tenant, which
            comes from the validated identity.
          in: header
          name: X-Organization-Id
          schema:
            description: >-
              Midaz organization that owns the ledger in the path. Required: a
              request without it, or with a value that is not a canonical UUID,
              is refused with 400 PBP-0025. Used only to look the account of
              accountAlias up in that ledger; it does not select the tenant,
              which comes from the validated identity.
            type: string
        - description: >-
            Client-supplied idempotency key, a UUID. Required: a request without
            it, with a value that is not a UUID, or with the nil UUID
            (00000000-0000-0000-0000-000000000000) is refused with 400 PBP-0012.
            A retry with the same key replays the stored 2xx answer.
          in: header
          name: Idempotency-Key
          required: true
          schema:
            description: >-
              Client-supplied idempotency key, a UUID. Required: a request
              without it, with a value that is not a UUID, or with the nil UUID
              (00000000-0000-0000-0000-000000000000) is refused with 400
              PBP-0012. A retry with the same key replays the stored 2xx answer.
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ConfigureLedgerCounterpartyRequest'
              description: The asset, the account, the reason and the version read.
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConfiguredLedgerCounterpartyResponse'
          description: OK
        '400':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: Bad Request
        '401':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: >-
            Authentication failed: the request carries no valid credential, or
            the credential does not identify a tenant. The error body is an RFC
            9457 problem document (`application/problem+json`) with error code
            `PBP-0003`. Obtain a valid bearer token and send the request again.
        '403':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: >-
            The authenticated principal is not allowed to perform this request.
            The error body is an RFC 9457 problem document
            (`application/problem+json`) with error code `PBP-0004`: the
            principal lacks permission for this operation, and the permission
            has to be granted before the request can succeed.
        '409':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: >-
            Conflict. PBP-0705: `version` is stale — another write changed this
            counterparty since it was read; read again and resend with the
            current version and a NEW Idempotency-Key. While an earlier request
            under the same `Idempotency-Key` is still being processed, a request
            under that key is answered PBP-0007 and is not run. Send it again
            under the SAME key after a short wait: the key answers this code for
            at most five minutes after the earlier request arrived. Afterwards
            the same key answers with the earlier request's stored answer, with
            500 PBP-0002 when that answer cannot be replayed, or, when no answer
            was kept, by running the request. Do not send it under a new key to
            get past this answer: the earlier request may still take effect.
        '413':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: Request Entity Too Large
        '415':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: 'Unsupported Media Type. PBP-0022: the body is not JSON.'
        '422':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/ProviderRouteRejectedProblem'
          description: >-
            Unprocessable Entity. PBP-0702: the counterparty write was refused;
            `reason` says why (NO_CHANGE, PROVIDER_NOT_INSTALLED,
            PROVIDER_NOT_CONNECTED, ACCOUNT_NOT_FOUND, ACCOUNT_NOT_EXTERNAL,
            ACCOUNT_ASSET_MISMATCH, POSTINGS_IN_FLIGHT,
            LEDGER_COUNTERPARTY_CONFLICT). PBP-0013: the Idempotency-Key was
            already used with a different request.
        '500':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: Internal Server Error
        '503':
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/Detail'
          description: >-
            Service Unavailable. PBP-0008 means a dependency the request needs,
            authentication or the tenant's storage, is temporarily unavailable.
            The request was not processed; wait and send the same request again
            with the same Idempotency-Key.
      security:
        - BearerAuth: []
components:
  schemas:
    ConfigureLedgerCounterpartyRequest:
      additionalProperties: false
      properties:
        accountAlias:
          description: >-
            Alias of the ledger account, with the leading @. @external/<asset>
            returns to the default and must name the assetCode of the request;
            any other value is the alias of an account created through the Midaz
            API, of a-zA-Z0-9@:_- only. Otherwise 400 PBP-0001.
          examples:
            - '@siloc-a-liquidar'
          maxLength: 100
          minLength: 2
          pattern: ^(@external/[A-Z]{3,10}|@[A-Za-z0-9@:_-]{1,99})$
          type: string
        assetCode:
          description: Asset code in the ledger.
          examples:
            - BRL
          maxLength: 10
          minLength: 3
          pattern: ^[A-Z]{3,10}$
          type: string
        reason:
          description: >-
            Why the counterparty is changed. Recorded in the history. Blank is
            refused with 400 PBP-0001.
          maxLength: 500
          minLength: 1
          pattern: \S
          type: string
        version:
          description: >-
            The counterparty's current version, as read; 0 when never
            configured.
          examples:
            - 0
          format: int64
          minimum: 0
          type: integer
      required:
        - assetCode
        - accountAlias
        - reason
        - version
      type: object
    ConfiguredLedgerCounterpartyResponse:
      additionalProperties: false
      properties:
        changeId:
          description: Identifier of the history line this write recorded.
          examples:
            - 019a5c1e-2b3d-7f4a-8c5d-6e7f8a9b0c1d
          format: uuid
          type: string
        counterparty:
          $ref: '#/components/schemas/LedgerCounterparty'
          description: The counterparty as written, with its new version.
      required:
        - counterparty
        - changeId
      type: object
    Detail:
      additionalProperties: true
      properties:
        code:
          description: >-
            Stable, machine-readable domain error code scoped to the emitting
            service (format: <SERVICE>-NNNN).
          type: string
        detail:
          description: >-
            A human-readable explanation specific to this occurrence of the
            problem.
          examples:
            - Property foo is required but is missing.
          type: string
        errors:
          description: Optional list of individual error details
          items:
            $ref: '#/components/schemas/ErrorDetail'
          type:
            - array
            - 'null'
        instance:
          description: >-
            A URI reference that identifies the specific occurrence of the
            problem.
          examples:
            - https://example.com/error-log/abc123
          format: uri
          type: string
        status:
          description: HTTP status code
          examples:
            - 400
          format: int64
          type: integer
        title:
          description: >-
            A short, human-readable summary of the problem type. This value
            should not change between occurrences of the error.
          examples:
            - Bad Request
          type: string
        type:
          default: about:blank
          description: A URI reference to human-readable documentation for the error.
          examples:
            - https://example.com/errors/example
          format: uri
          type: string
        upstream:
          $ref: '#/components/schemas/Upstream'
          description: >-
            RFC 9457 extension member: the error a proxied third-party provider
            reported. Absent unless the emitting service explicitly surfaced
            one.
      type: object
    ProviderRouteRejectedProblem:
      allOf:
        - $ref: '#/components/schemas/Detail'
        - properties:
            maxDays:
              description: >-
                The provider's maximum payment deadline, in calendar days after
                the due date. Present only with reason
                PAYMENT_DEADLINE_ABOVE_MAXIMUM.
              minimum: 0
              type: integer
            reason:
              description: >-
                Why the route, counterparty or payment deadline write was
                refused (PBP-0702 only). ACCOUNT_NOT_FOUND,
                ACCOUNT_NOT_EXTERNAL, ACCOUNT_ASSET_MISMATCH, POSTINGS_IN_FLIGHT
                and LEDGER_COUNTERPARTY_CONFLICT only on the counterparty write;
                PAYMENT_DEADLINE_ABOVE_MAXIMUM only on the payment deadline
                write.
              enum:
                - PROVIDER_NOT_CONNECTED
                - CAPABILITY_MISSING
                - PROVIDER_NOT_INSTALLED
                - UNKNOWN_OPERATION
                - NO_CHANGE
                - PROVIDER_SWITCH_UNAVAILABLE
                - ACCOUNT_NOT_FOUND
                - ACCOUNT_NOT_EXTERNAL
                - ACCOUNT_ASSET_MISMATCH
                - POSTINGS_IN_FLIGHT
                - LEDGER_COUNTERPARTY_CONFLICT
                - PAYMENT_DEADLINE_ABOVE_MAXIMUM
              type: string
          type: object
      description: >-
        Problem document of the configure 422 (route, ledger counterparty and
        payment deadline). `reason` is present only when code is PBP-0702; the
        counterparty write answers only NO_CHANGE, PROVIDER_NOT_INSTALLED,
        PROVIDER_NOT_CONNECTED, ACCOUNT_NOT_FOUND, ACCOUNT_NOT_EXTERNAL,
        ACCOUNT_ASSET_MISMATCH, POSTINGS_IN_FLIGHT or
        LEDGER_COUNTERPARTY_CONFLICT; the payment deadline write only NO_CHANGE,
        PROVIDER_NOT_INSTALLED, CAPABILITY_MISSING,
        PAYMENT_DEADLINE_ABOVE_MAXIMUM or PROVIDER_NOT_CONNECTED.
    LedgerCounterparty:
      additionalProperties: false
      properties:
        accountAlias:
          description: >-
            Alias of the ledger account, with the leading @ of the Midaz alias;
            @external/<asset> is the default counterparty.
          examples:
            - '@external/BRL'
          maxLength: 100
          minLength: 2
          type: string
        assetCode:
          description: Asset code in the ledger.
          examples:
            - BRL
          maxLength: 10
          minLength: 3
          pattern: ^[A-Z]{3,10}$
          type: string
        isDefault:
          description: >-
            True when the postings of the (ledger, provider, asset) use
            @external/<asset>: no counterparty is configured, or the configured
            one is @external/<asset>.
          examples:
            - false
          type: boolean
        ledgerId:
          description: >-
            Midaz ledger of the counterparty. Null only on the default item of
            the tenant list, which applies to every ledger with no configured
            counterparty.
          examples:
            - 0199a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a6c
          maxLength: 100
          type:
            - string
            - 'null'
        providerType:
          description: Provider whose operations post against the counterparty.
          examples:
            - BTG
          maxLength: 20
          type: string
        updatedAt:
          description: >-
            When the counterparty was last written, in UTC. Null when never
            configured.
          examples:
            - '2026-11-07T14:05:09Z'
          format: date-time
          type:
            - string
            - 'null'
        version:
          description: >-
            Version of this (tenant, ledger, provider, asset) counterparty. 0
            when never configured. Incremented by every accepted write. A write
            sends it back; a stale one is refused with 409 PBP-0705.
          examples:
            - 1
          format: int64
          minimum: 0
          type: integer
      required:
        - ledgerId
        - providerType
        - assetCode
        - accountAlias
        - isDefault
        - version
      type: object
    ErrorDetail:
      additionalProperties: false
      properties:
        location:
          description: >-
            Where the error occurred, e.g. 'body.items[3].tags' or
            'path.thing-id'
          type: string
        message:
          description: Error message text
          type: string
        value:
          description: The value at the given location
      type: object
    Upstream:
      additionalProperties: false
      properties:
        code:
          description: The upstream provider's own error code, verbatim.
          examples:
            - E4001
          type: string
        message:
          description: >-
            The upstream provider's own error message, verbatim (bounded, never
            its raw response body).
          examples:
            - account not found at provider
          type: string
      type: object
  securitySchemes:
    BearerAuth:
      bearerFormat: JWT
      description: JWT bearer token issued by the identity provider.
      scheme: bearer
      type: http

````

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