---
title: "Build loan"
method: POST
path: "/markets/creditbook/build"
tags: ["creditbook"]
---

# Build loan

`POST /markets/creditbook/build`

Builds or mutates a loan through a sequence of high-level actions. Supported step types currently include `repay`, `flashBorrow`, and `swapCollateral`.

## Headers

- `Idempotent-Key` string, nullable

## Request body

- LoanBuildParams
  - `loan` string, nullable
  - `steps` LoanBuildStep[], required — Ordered build steps.
    - union — One step of a composed loan build, adjacently-tagged by `type` with the step's own transaction params under `params`.
      - object
        - `params` TxnRepayPrincipalParams, required
          - `closeIfPossible` boolean
          - `collateralWithdrawalParams` WithdrawCollateralParams[]
            - `amount` integer, required
            - `closeIfEligible` boolean
            - `collateralMint` string, required
          - `cpiIxs` ParsedInstruction[] — Caller-supplied CPI instructions spliced into the built transaction.
            - `accounts` ParsedAccountMeta[], required — ordered account metas
              - …
            - `data` string, required — base58-encoded instruction data
            - `programId` string, required — program id (base58)
          - `cpiLuts` string[]
          - `cpiSigners` string[]
          - `loan` string, required
          - `repayParams` RepayPrincipalParamsSchema[], required
            - `amount` integer, required — Amount to repay, in the principal mint's native units (ignored if `repay_all`).
            - `ledgerIndex` integer, required — Index of the ledger being repaid.
            - `repayAll` boolean, required — Repay the ledger's full outstanding principal + interest.
          - `setupIxs` ParsedInstruction[] — Setup instructions run before the main instructions.
            - `accounts` ParsedAccountMeta[], required — ordered account metas
              - …
            - `data` string, required — base58-encoded instruction data
            - `programId` string, required — program id (base58)
          - `setupLuts` string[]
          - `setupSigners` string[]
          - `stakeInstructions` StakeLpQueryParams
            - `amount` integer, required
            - `stakeAccount` string, nullable
            - `vault` string, required
          - `unifySetup` boolean
          - `weightMatrixUpdates` WeightMatrixUpdateParams[]
            - `collateralIndex` integer, required
            - `weightMatrixUpdate` integer[], required
        - `type` 'repay', required
      - object
        - `params` IncreaseLeverageTransactionInput, required
          - `cpiIxs` ParsedInstruction[] — Caller-supplied CPI instructions spliced into the built transaction.
            - `accounts` ParsedAccountMeta[], required — ordered account metas
              - …
            - `data` string, required — base58-encoded instruction data
            - `programId` string, required — program id (base58)
          - `cpiLuts` string[]
          - `cpiSigners` string[]
          - `depositCollateral` DepositCollateralInput[], required — Collateral to deposit alongside the borrow.
            - `collateralAmount` integer, required
            - `collateralAssetData` union, required — Collateral asset identification, tagged by collateral kind (SPL token, staked SOL, or a CLMM LP position on Orca / Meteora / Raydium).
              - …
            - `loanCreationParams` union — Optional collateral-specific loan-creation params (e.g. opening an Orca CLMM position as collateral); `Default` for a plain loan.
              - …
            - `weightMatrixUpdate` integer[]
          - `isLoop` boolean, nullable
          - `loanAddress` string, nullable
          - `nonce` string, nullable
          - `principalRequested` BorrowPrincipalRequested[], required — Per-ledger principal borrow requests.
            - `durationIndex` integer, required
            - `expectedLoanValues` ExpectedLoanValuesSchema, required — Expected loan economics the borrower signs off on, checked on-chain against the freshly-priced values to bound slippage.
              - …
            - `ledgerIndex` integer, required
            - `principalAmount` integer, required
            - `principalMint` string, required
            - `strategy` string, required
          - `refinanceParams` RefinanceLedgerParamsSchema — Params for refinancing a ledger to a new strategy/duration (`refinance_ledger`).
            - `assetIndexGuidance` integer[], required — Per-collateral oracle asset-index guidance.
            - `durationIndex` integer, required — Index into the new strategy's offered durations.
            - `ledgerIndex` integer, required — Index of the ledger being refinanced.
          - `setupIxs` ParsedInstruction[] — Setup instructions run before the main instructions.
            - `accounts` ParsedAccountMeta[], required — ordered account metas
              - …
            - `data` string, required — base58-encoded instruction data
            - `programId` string, required — program id (base58)
          - `setupLuts` string[]
          - `setupSigners` string[]
          - `unifySetup` boolean
        - `type` 'flashBorrow', required
      - object
        - `params` TxnSwapCollateralParams, required
          - `cpiIxs` ParsedInstruction[] — Caller-supplied CPI instructions spliced into the built transaction.
            - `accounts` ParsedAccountMeta[], required — ordered account metas
              - …
            - `data` string, required — base58-encoded instruction data
            - `programId` string, required — program id (base58)
          - `cpiLuts` string[]
          - `cpiSigners` string[]
          - `depositCollateral` DepositCollateralInput[], required
            - `collateralAmount` integer, required
            - `collateralAssetData` union, required — Collateral asset identification, tagged by collateral kind (SPL token, staked SOL, or a CLMM LP position on Orca / Meteora / Raydium).
              - …
            - `loanCreationParams` union — Optional collateral-specific loan-creation params (e.g. opening an Orca CLMM position as collateral); `Default` for a plain loan.
              - …
            - `weightMatrixUpdate` integer[]
          - `loan` string, required
          - `withdrawCollateral` WithdrawCollateralParams[], required
            - `amount` integer, required
            - `closeIfEligible` boolean
            - `collateralMint` string, required
        - `type` 'swapCollateral', required

## Response `200`

Derived loan address and the transaction sequence produced by the requested build steps.

- LoanBuildResponse
  - `loan` string, required
  - `transactions` VersionedTransactionReturn[], required
    - `message` string, required — serialized message
    - `signatures` VersionedTransactionSignature[], required — serialized signatures
      - `publicKey` string, required — serialized message
      - `signature` string, required — serialized signatures

## Other responses

- `400` — Invalid request
- `401` — Unauthorized
- `409` — Idempotency-Key reused with a different request body
- `500` — Internal server error

---

[API](https://skmtc.dev/loopscale/apis/loopscale-api.md) · [All operations](https://skmtc.dev/loopscale/apis/loopscale-api/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/loopscale/loopscale-api/revisions/531f64dbe09c/schema)
