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

# Repay loan

> Partially or fully repay an active loan. Allows for the withdrawal of collateral for optional use in CPIs before repayment.



## OpenAPI

````yaml post /markets/creditbook/repay
openapi: 3.1.3
info:
  title: Loopscale API
  version: 1.0.0
servers:
  - url: https://tars.loopscale.com/v1
    description: Loopscale Production Server
security: []
paths:
  /markets/creditbook/repay:
    post:
      tags:
        - Markets
      summary: Repay loan
      description: >-
        Partially or fully repay an active loan. Allows for the withdrawal of
        collateral for optional use in CPIs before repayment.
      operationId: repayLoan
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RepayLoanRequest'
      responses:
        '200':
          description: Serialized transaction message and signatures
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LoanLockTransactionResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '500':
          $ref: '#/components/responses/GeneralError'
components:
  schemas:
    RepayLoanRequest:
      type: object
      required:
        - loan
        - repayParams
        - collateralWithdrawalParams
      properties:
        loan:
          type: string
          description: Loan address to repay
        collateralWithdrawalParams:
          type: array
          items:
            $ref: '#/components/schemas/CollateralWithdraw'
          description: List of collateral assets to withdraw before repayment
        repayParams:
          type: array
          items:
            $ref: '#/components/schemas/RepayLedgerParam'
          description: List of principal repayment instructions
        cpiIxs:
          type: array
          items:
            $ref: '#/components/schemas/CpiInstruction'
          description: Optional CPI instructions to run between withdrawal and repayment
        cpiLuts:
          type: array
          items:
            type: string
          description: Optional list of address lookup tables
        cpiSigners:
          type: array
          items:
            type: string
          description: Optional list of signer keypairs
        setupIxs:
          type: array
          items:
            $ref: '#/components/schemas/CpiInstruction'
          description: Optional instructions to execute first
        setupLuts:
          type: array
          items:
            type: string
          description: Optional Address Lookup Tables
        setupSigners:
          type: array
          items:
            type: string
          description: Optional signer keys, not including the user's own
        unifySetup:
          type: boolean
          description: >-
            If true, this flag will prepend the setupIxs to the repay
            transaction, if false (default), this endpoint will return two
            transactions (Setup + Repay Loan w/ CPI)
        closeIfPossible:
          type: boolean
          description: >-
            Defaults to true. If true, this flag will append a loan close
            instruction if the entire loan is empty, otherwise it will skip
            closing and need to be handled separately.
    LoanLockTransactionResponse:
      type: object
      properties:
        transactions:
          type: array
          items:
            type: object
            required:
              - message
              - signatures
            properties:
              message:
                type: string
                description: Base64-encoded versioned transaction message
              signatures:
                type: array
                items:
                  type: object
                  required:
                    - publicKey
                    - signature
                  properties:
                    publicKey:
                      type: string
                    signature:
                      type: string
        expectedLoanInfo:
          type: object
          description: Loan post transaction state used for verification
          properties:
            loan:
              type: object
              properties:
                address:
                  type: string
                  description: Onchain loan account
    CollateralWithdraw:
      type: object
      required:
        - amount
        - collateralMint
      properties:
        amount:
          type: number
          description: Collateral to withdraw (lamports)
        collateralMint:
          type: string
          description: Mint of collateral to withdraw
    RepayLedgerParam:
      type: object
      required:
        - amount
        - ledgerIndex
        - repayAll
      properties:
        amount:
          type: number
          description: Amount to repay in lamports
        ledgerIndex:
          type: integer
          description: Ledger index to apply repayment to (0–4)
        repayAll:
          type: boolean
          description: Set to true if intending to fully repay this ledger
    CpiInstruction:
      type: object
      required:
        - programId
        - accounts
        - data
      properties:
        programId:
          type: string
          description: Program ID to call
        accounts:
          type: array
          items:
            type: object
            required:
              - pubkey
              - isSigner
              - isWritable
            properties:
              pubkey:
                type: string
              isSigner:
                type: boolean
              isWritable:
                type: boolean
        data:
          type: string
          description: Base64 or hex-encoded instruction data
  responses:
    BadRequest:
      description: Bad Request
      content:
        text/plain:
          schema:
            type: string
            example: 'Something went wrong: You have an error in your SQL syntax.'
    GeneralError:
      description: General Server Error
      content:
        text/plain:
          schema:
            type: string
            example: >-
              Failed to deserialize the JSON body into the target type: missing
              field `mints` at line 1 column 1.

````