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

# Flash borrow

> Atomically creates a new loan and executes optional CPIs before depositing collateral. Supports deferred collateralization. Returns a transaction message and resulting loan address.



## OpenAPI

````yaml post /markets/creditbook/flash_borrow
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/flash_borrow:
    post:
      tags:
        - Markets
      summary: Flash borrow
      description: >-
        Atomically creates a new loan and executes optional CPIs before
        depositing collateral. Supports deferred collateralization. Returns a
        transaction message and resulting loan address.
      operationId: flashBorrow
      parameters:
        - name: user-wallet
          in: header
          required: true
          schema:
            type: string
          description: Wallet address of the borrowing user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FlashBorrowRequest'
      responses:
        '200':
          description: Serialized transaction and resulting loan address
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LoanLockTransactionResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '500':
          $ref: '#/components/responses/GeneralError'
components:
  schemas:
    FlashBorrowRequest:
      type: object
      required:
        - principalRequested
        - depositCollateral
      properties:
        principalRequested:
          type: array
          minItems: 0
          default: []
          items:
            $ref: '#/components/schemas/PrincipalBorrowRequest'
        depositCollateral:
          type: array
          minItems: 0
          default: []
          items:
            $ref: '#/components/schemas/CollateralDeposit'
        cpiIxs:
          type: array
          items:
            $ref: '#/components/schemas/CpiInstruction'
          description: Instructions to execute between borrow and deposit steps
        cpiLuts:
          type: array
          items:
            type: string
          description: Optional Address Lookup Tables
        cpiSigners:
          type: array
          items:
            type: string
          description: Optional signer keys, not including the user's own
        loanAddress:
          type: string
          description: If leveraging an existing loan, provide its address
        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 flash loan
            transaction, if false (default), this endpoint will return two
            transactions (Setup + Flash Loan w/ CPI)
    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
    PrincipalBorrowRequest:
      type: object
      required:
        - ledgerIndex
        - principalAmount
        - principalMint
        - strategy
        - durationIndex
        - expectedLoanValues
      properties:
        ledgerIndex:
          type: integer
          description: Which ledger to borrow into (0–4). Use 0 for most cases.
        principalAmount:
          type: number
          description: Amount of principal to borrow in lamports
        principalMint:
          type: string
          description: Mint address of the principal token
        strategy:
          type: string
          description: Strategy address returned from quote
        durationIndex:
          type: integer
          description: Duration preset (0 = 1d, 1 = 1w, 2 = 1mo, 3 = 3mo, 4 = 5min)
        expectedLoanValues:
          type: object
          description: >-
            Values used to ensure the lender doesn't change order details before
            transaction completes
          allOf:
            - $ref: '#/components/schemas/ExpectedLoanValues'
    CollateralDeposit:
      type: object
      required:
        - collateralAmount
        - collateralAssetData
      properties:
        collateralAmount:
          type: number
          description: Collateral to deposit (lamports)
        collateralAssetData:
          $ref: '#/components/schemas/AssetDataInfo'
    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
    ExpectedLoanValues:
      type: object
      required:
        - expectedApy
        - expectedLqt
      properties:
        expectedApy:
          type: number
          description: Expected APY in cBPS
        expectedLqt:
          type: array
          items:
            type: number
          description: Expected liquidation thresholds per ledger (cBPS)
    AssetDataInfo:
      oneOf:
        - type: object
          required:
            - Spl
          properties:
            Spl:
              $ref: '#/components/schemas/SplAssetData'
        - type: object
          required:
            - StakedSol
          properties:
            StakedSol:
              $ref: '#/components/schemas/StakedSolAssetData'
        - type: object
          required:
            - Orca
          properties:
            Orca:
              $ref: '#/components/schemas/OrcaPositionAssetData'
        - type: object
          required:
            - Meteora
          properties:
            Meteora:
              $ref: '#/components/schemas/MeteoraDlmmPositionAssetData'
        - type: object
          required:
            - Raydium
          properties:
            Raydium:
              $ref: '#/components/schemas/RaydiumPositionAssetData'
      description: >-
        Tagged asset descriptor used for collateral inputs. LP position variants
        can optionally specify `tokenProgram` to override the token program used
        when deriving token accounts.
    SplAssetData:
      type: object
      required:
        - mint
      properties:
        mint:
          type: string
          description: Collateral mint pubkey
    StakedSolAssetData:
      type: object
      required:
        - stakeAccount
        - stakePool
      properties:
        stakeAccount:
          type: string
          description: Stake account pubkey
        stakePool:
          type: string
          description: Stake pool pubkey
    OrcaPositionAssetData:
      type: object
      required:
        - positionMint
        - whirlpool
      properties:
        positionMint:
          type: string
          description: Position mint pubkey
        whirlpool:
          type: string
          description: Whirlpool pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
    MeteoraDlmmPositionAssetData:
      type: object
      required:
        - positionAddress
        - lbPair
      properties:
        positionAddress:
          type: string
          description: DLMM position address
        lbPair:
          type: string
          description: Meteora DLMM pair pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
    RaydiumPositionAssetData:
      type: object
      required:
        - mint
        - pool
      properties:
        mint:
          type: string
          description: Raydium position mint pubkey
        pool:
          type: string
          description: Raydium pool pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
  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.

````