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

# Create loan

> Creates a new loan with initial collateral and requested principal borrows, returning both the loan address and the serialized transaction needed to initialize it.



## OpenAPI

````yaml post /markets/creditbook/create
openapi: 3.1.0
info:
  title: Loopscale API
  version: 0.1.0
servers:
  - url: https://tars.loopscale.com/v1
    description: Loopscale Production Server
security: []
paths:
  /markets/creditbook/create:
    post:
      tags:
        - creditbook
      summary: Create loan
      description: >-
        Creates a new loan with initial collateral and requested principal
        borrows, returning both the loan address and the serialized transaction
        needed to initialize it.
      operationId: query_create_loan
      parameters:
        - name: Idempotent-Key
          in: header
          description: >-
            Optional idempotency key; a retried write with the same key replays
            the original response
          required: false
          schema:
            type:
              - string
              - 'null'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TxnCreateLoanParams'
        required: true
      responses:
        '200':
          description: Serialized transaction and derived loan address.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateLoanReturn'
        '400':
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '409':
          description: Idempotency-Key reused with a different request body
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    TxnCreateLoanParams:
      type: object
      required:
        - depositCollateral
        - borrower
        - principalRequested
      properties:
        assetIndexGuidance:
          type: array
          items:
            type: integer
            format: int32
            minimum: 0
          description: >-
            Per-collateral oracle asset-index guidance (one byte per collateral
            slot).
        borrower:
          type: string
        depositCollateral:
          type: array
          items:
            $ref: '#/components/schemas/DepositCollateralInput'
        isLoop:
          type:
            - boolean
            - 'null'
        loanNonce:
          type:
            - string
            - 'null'
        principalRequested:
          type: array
          items:
            $ref: '#/components/schemas/BorrowPrincipalRequested'
        unstakeInstructions:
          oneOf:
            - type: 'null'
            - $ref: '#/components/schemas/UnstakeLpQueryParams'
              description: >-
                Optional vault LP unstake leg to compose with this txn as a
                prefix.
    CreateLoanReturn:
      type: object
      required:
        - transaction
        - loanAddress
      properties:
        loanAddress:
          type: string
        transaction:
          $ref: '#/components/schemas/VersionedTransactionReturn'
    ErrorResponse:
      type: object
      description: |-
        Canonical error envelope for every endpoint. The `x-request-id` response
        header carries the correlation id for the failing request.
      required:
        - error
      properties:
        error:
          $ref: '#/components/schemas/ErrorDetails'
          description: error detail
    DepositCollateralInput:
      type: object
      required:
        - collateralAmount
        - collateralAssetData
      properties:
        collateralAmount:
          type: integer
          format: int64
          minimum: 0
        collateralAssetData:
          $ref: '#/components/schemas/AssetDataInfo'
        loanCreationParams:
          oneOf:
            - type: 'null'
            - $ref: '#/components/schemas/LoanCreationParams'
        weightMatrixUpdate:
          type: array
          items:
            type: integer
            format: int32
            minimum: 0
    BorrowPrincipalRequested:
      type: object
      required:
        - ledgerIndex
        - principalAmount
        - principalMint
        - strategy
        - durationIndex
        - expectedLoanValues
      properties:
        durationIndex:
          type: integer
          format: int32
          minimum: 0
        expectedLoanValues:
          $ref: '#/components/schemas/ExpectedLoanValuesSchema'
        ledgerIndex:
          type: integer
          format: int32
          minimum: 0
        principalAmount:
          type: integer
          format: int64
          minimum: 0
        principalMint:
          type: string
        strategy:
          type: string
    UnstakeLpQueryParams:
      type: object
      required:
        - vault
        - amount
      properties:
        amount:
          type: integer
          format: int64
          minimum: 0
        stakeAccount:
          type:
            - string
            - 'null'
        vault:
          type: string
    VersionedTransactionReturn:
      type: object
      description: versioned txn return
      required:
        - message
        - signatures
      properties:
        message:
          type: string
          description: serialized message
        signatures:
          type: array
          items:
            $ref: '#/components/schemas/VersionedTransactionSignature'
          description: serialized signatures
    ErrorDetails:
      type: object
      description: Inner detail of the [`ErrorResponse`] envelope.
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
          description: HTTP status code, mirrored in the response status line
          example: 400
          minimum: 0
        message:
          type: string
          description: human-readable, client-safe message (sensitive data redacted)
          example: Request could not be processed.
    AssetDataInfo:
      oneOf:
        - type: object
          required:
            - Spl
          properties:
            Spl:
              $ref: '#/components/schemas/SplData'
        - type: object
          required:
            - StakedSol
          properties:
            StakedSol:
              $ref: '#/components/schemas/StakedSolData'
        - type: object
          required:
            - Orca
          properties:
            Orca:
              $ref: '#/components/schemas/OrcaPositionData'
        - type: object
          required:
            - Meteora
          properties:
            Meteora:
              $ref: '#/components/schemas/MeteoraDlmmPositionData'
        - type: object
          required:
            - Raydium
          properties:
            Raydium:
              $ref: '#/components/schemas/RaydiumPositionData'
      description: |-
        Collateral asset identification, tagged by collateral kind (SPL token,
        staked SOL, or a CLMM LP position on Orca / Meteora / Raydium).
    LoanCreationParams:
      oneOf:
        - type: string
          enum:
            - Default
        - type: object
          required:
            - OrcaPositionCreate
          properties:
            OrcaPositionCreate:
              $ref: '#/components/schemas/OrcaPositionCreateParams'
      description: >-
        Optional collateral-specific loan-creation params (e.g. opening an Orca
        CLMM

        position as collateral); `Default` for a plain loan.
    ExpectedLoanValuesSchema:
      type: object
      description: >-
        Expected loan economics the borrower signs off on, checked on-chain
        against the

        freshly-priced values to bound slippage.
      required:
        - expectedApy
        - expectedLqt
      properties:
        expectedApy:
          type: integer
          format: int64
          description: Expected loan APY, in basis points of a hundredth (cbps).
          minimum: 0
        expectedLqt:
          type: array
          items:
            type: integer
            format: int32
            minimum: 0
          description: >-
            Expected per-ledger liquidation threshold, one entry per ledger
            slot.
    VersionedTransactionSignature:
      type: object
      description: versioned txn return
      required:
        - publicKey
        - signature
      properties:
        publicKey:
          type: string
          description: serialized message
        signature:
          type: string
          description: serialized signatures
    SplData:
      type: object
      required:
        - mint
      properties:
        mint:
          type: string
    StakedSolData:
      type: object
      required:
        - stakeAccount
        - stakePool
      properties:
        stakeAccount:
          type: string
        stakePool:
          type: string
    OrcaPositionData:
      type: object
      required:
        - positionMint
        - whirlpool
      properties:
        positionMint:
          type: string
        tokenProgram:
          type:
            - string
            - 'null'
        whirlpool:
          type: string
    MeteoraDlmmPositionData:
      type: object
      required:
        - positionAddress
        - lbPair
      properties:
        lbPair:
          type: string
        positionAddress:
          type: string
        tokenProgram:
          type:
            - string
            - 'null'
    RaydiumPositionData:
      type: object
      required:
        - mint
        - pool
      properties:
        mint:
          type: string
        pool:
          type: string
        tokenProgram:
          type:
            - string
            - 'null'
    OrcaPositionCreateParams:
      type: object
      required:
        - upperTick
        - lowerTick
        - tickSpacing
        - whirlpool
        - tokenMintA
        - tokenMintB
      properties:
        lowerTick:
          type: integer
          format: int32
        tickSpacing:
          type: integer
          format: int32
        tokenMintA:
          type: string
        tokenMintB:
          type: string
        upperTick:
          type: integer
          format: int32
        whirlpool:
          type: string

````