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

# Withdraw Collateral

> Release excess collateral from an open loan

Removes collateral from a loan while leaving it open. The contract reverts if the withdrawal would push the health factor below `1.0`, so a loan can only shed collateral it does not need.

Single transaction in both modes.

## 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="amount" type="string" required>
  Security tokens to release, as a decimal string. Parsed at the collateral token's decimals.
</ParamField>

<ParamField body="signedPrice" type="object">
  EIP-712 signed price from [Sign Price](/endpoints/lending/sign-price). Omit to price against the stored oracle value.

  <Expandable>
    <ParamField body="price" type="string">Price in base units.</ParamField>
    <ParamField body="timestamp" type="integer">Signing timestamp.</ParamField>
    <ParamField body="validUntil" type="integer">Signature expiry.</ParamField>
    <ParamField body="signature" type="string">EIP-712 signature.</ParamField>
  </Expandable>
</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="amount" type="string">Collateral released, 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 `withdrawCollateral` call. Returned when `txHash` is omitted.</ResponseField>
    <ResponseField name="functionName" type="string">`withdrawCollateral`. 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. The withdrawal 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>
  A withdrawal that would breach the health factor, exceed the posted collateral, or target an inactive loan is refused before a transaction is offered, with `WOULD_BREACH_HEALTH_FACTOR`, `INSUFFICIENT_COLLATERAL` or `LOAN_NOT_ACTIVE`. The price is only consulted while the loan still carries debt, so a debt-free withdrawal is never refused for a stale price.
</Warning>

<Warning>
  A `CUSTODY` withdrawal the collateral token refuses does not revert. The adapter escrows the balance instead, so the borrower signs a transaction that confirms and delivers nothing. When that is the case the response carries `collateralRelease` and `warning`. Surface them to the borrower before they sign.
</Warning>

<Note>
  Where the released tokens end up depends on the market mode. `FREEZE` markets unfreeze them in place, so the borrower's balance does not change but the tokens become transferable again. `CUSTODY` markets transfer them out of the adapter back to the borrower.
</Note>

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

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

  ```json Confirmed Response theme={null}
  {
    "success": true,
    "data": {
      "txHash": "0xabc...def",
      "loanId": 5,
      "amount": "100"
    }
  }
  ```
</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, or `amount` carries more decimal places than the collateral token supports                                                                   |
| `LOAN_NOT_ACTIVE`            | `400` | The loan is closed and can no longer be modified                                                                                                                                 |
| `INSUFFICIENT_COLLATERAL`    | `400` | `amount` exceeds the collateral the loan holds                                                                                                                                   |
| `WOULD_BREACH_HEALTH_FACTOR` | `400` | The withdrawal would leave the loan under-collateralised against its outstanding debt                                                                                            |
| `PRICE_STALE`                | `400` | The loan carries debt and the oracle price is outside the market's `maxPriceAge`. Push a NAV with [Sync Oracle Price](/endpoints/lending/sync-oracle), or supply a `signedPrice` |
| `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                                                                                                                                          |
| `MARKET_STATE_UNAVAILABLE`   | `503` | The market could not be read to check its price window. Retry shortly                                                                                                            |
| `LOAN_STATE_UNAVAILABLE`     | `503` | The loan or the resulting health factor could not be read. Retry shortly                                                                                                         |
