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

# Close Loan

> Close a fully repaid loan and release the collateral

Closes a loan whose debt is zero and releases the pledged collateral. In `FREEZE` markets the tokens are unfrozen in the borrower's wallet. In `CUSTODY` markets they are transferred back from the adapter.

The endpoint validates loan state on-chain before doing anything, so a loan with any outstanding balance is rejected with a clear reason rather than an opaque revert.

## Path Parameters

<ParamField path="marketId" type="string" required>Market ID.</ParamField>

## Body Parameters

<ParamField body="loanId" type="integer" required>
  On-chain loan ID. A positive integer. Accepted as a JSON number or as a numeric string, so the value [Get Loan](/endpoints/lending/get-loan) returns passes straight back.
</ParamField>

<ParamField body="txHash" type="string">
  Hash of the transaction you broadcast for this operation. Send it to confirm the transaction and record the result. Omit it to receive the calldata.
</ParamField>

## Response Fields

<ResponseField name="data" type="object">
  <Expandable>
    <ResponseField name="txHash" type="string">Transaction hash. Returned when confirming with `txHash`.</ResponseField>
    <ResponseField name="loanId" type="integer">Loan ID, echoed back. Returned when confirming with `txHash`.</ResponseField>
    <ResponseField name="action" type="string">`SIGN_TRANSACTION` when `txHash` is omitted.</ResponseField>
    <ResponseField name="transaction" type="object">Unsigned transaction, `{ to, data, value, chainId }`, for the `closeLoan` call. Returned when `txHash` is omitted.</ResponseField>
    <ResponseField name="functionName" type="string">`closeLoan`. Returned when `txHash` is omitted.</ResponseField>
    <ResponseField name="collateralRelease" type="object">Present on a `CUSTODY` market when the collateral token would refuse to return the collateral to the borrower. Closing still succeeds; the adapter escrows the balance instead of reverting, and the borrower reclaims it through [Claim Escrowed Collateral](/endpoints/lending/claim-escrowed-collateral).</ResponseField>
    <ResponseField name="warning" type="string">Human-readable form of `collateralRelease`, present alongside it.</ResponseField>
  </Expandable>
</ResponseField>

<Warning>
  Interest accrues until the moment of repayment, so a loan repaid with a stale quote almost always carries a small residual balance. `DEBT_OUTSTANDING` reports the exact remainder. Repay that amount, then close.
</Warning>

<Note>
  A successful close on confirm also marks the Trusset position `REPAID` with a `closedAt` timestamp, unless the position was already `LIQUIDATED`. When `txHash` is omitted the position is reconciled on the next sync rather than at broadcast.
</Note>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.trusset.org/lending-external-securities/api/positions/{marketId}/close-loan" \
    -H "X-API-Key: trusset_your_key_here" \
    -H "Content-Type: application/json" \
    -d '{"loanId": 5}'
  ```
</RequestExample>

<ResponseExample>
  ```json Calldata Response theme={null}
  {
    "success": true,
    "data": {
      "action": "SIGN_TRANSACTION",
      "transaction": { "to": "0x70A0...", "data": "0x..." },
      "functionName": "closeLoan"
    }
  }
  ```

  ```json Confirmed Response theme={null}
  {
    "success": true,
    "data": {
      "txHash": "0xabc...def",
      "loanId": 5
    }
  }
  ```

  ```json Error - Debt Outstanding theme={null}
  {
    "success": false,
    "error": {
      "code": "DEBT_OUTSTANDING",
      "message": "Loan has 0.184220 USDC outstanding. Repay in full before closing."
    }
  }
  ```

  ```json Error - Already Closed theme={null}
  {
    "success": false,
    "error": {
      "code": "LOAN_ALREADY_CLOSED",
      "message": "Loan is already closed"
    }
  }
  ```
</ResponseExample>

## Error Codes

| Code                     | HTTP  | Cause                                                                                      |
| ------------------------ | ----- | ------------------------------------------------------------------------------------------ |
| `NO_MARKET_ADDRESS`      | `400` | The market has no on-chain address recorded                                                |
| `VALIDATION_ERROR`       | `400` | `loanId` is not a positive integer                                                         |
| `DEBT_OUTSTANDING`       | `400` | Principal or accrued interest remains. The message states the exact amount                 |
| `LOAN_ALREADY_CLOSED`    | `400` | The loan is no longer active                                                               |
| `LOAN_NOT_FOUND`         | `404` | No loan with that ID exists on this market                                                 |
| `MARKET_NOT_FOUND`       | `404` | No market with this ID on your instance                                                    |
| `LOAN_STATE_UNAVAILABLE` | `503` | The loan could not be read from the network, so no transaction was prepared. Retry shortly |
