> ## 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. Must be a positive integer sent as a JSON number.
</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">`{ to, data }` for the `closeLoan` call. Returned when `txHash` is omitted.</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>

## Errors

| Code                  | HTTP  | Cause                                                                                                                       |
| --------------------- | ----- | --------------------------------------------------------------------------------------------------------------------------- |
| `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                                                                                  |
| `RPC_UNAVAILABLE`     | `500` | Loan state could not be verified. Retry. Only returned on confirm; the calldata response returns the transaction regardless |
| `NO_MARKET_ADDRESS`   | `400` | The market has no on-chain address recorded                                                                                 |
