# Execute a swap

`POST /dex/swap` · Lithos client API · Lithos Dex · requires the `api_key` header

Source: https://lithosprotocol.org/docs/api/ld-swap

Sign and submit a swap that spends the pool directly. `minOutput` is required and rejects the trade if the pool has moved against you; send `expectedPoolBoxId` from the quote to get a 409 instead of a re-quote. `POST /dex/orders/swap` places the same swap as an order.

## Request body

`LDSwapExecuteRequest`, application/json.

- `amountIn` (string, required): Total handed to the pool, fees included. Decimal string.
- `ergIn` (boolean, required)
- `minOutput` (string, required): Floor below which the swap must be rejected. Required, and must not be negative — a negative floor is no floor. Decimal string. 
- `expectedPoolBoxId` (string): Pool box the quote was made against. Returns 409 instead of re-quoting if it moved. 

## Responses

### 200: Result of the swap transaction

`LDSwapResult`

- `outcome` (string, required, one of accepted, rejected, uncertain): Node submission outcome; uncertain retains the known transaction ID and input ownership.
- `amountOut` (string, required): Amount received. Decimal string.
- `txId` (string, required)

### 400: Malformed body, a non-positive `amountIn`, or a negative `minOutput`.

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

### 409: The pool moved. Re-read and quote again.

`LDStateChangedResponse`

- `error` (integer, required)
- `reason` (string, required)
- `detail` (string, required): What moved, and what it moved to
- `poolBoxId` (string): Current pool box id
- `vaultBoxId` (string): Current vault box id
- `provisionBoxId` (string): Current provision box id

### 422: The pool cannot fill this swap, or the wallet cannot cover it.

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

### 503: Node or wallet unavailable. Retry.

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

### default: Error

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

## Example request

```bash
curl -X POST "http://localhost:9000/dex/swap" \
  -H "api_key: $LITHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "amountIn": "1000000000", "ergIn": true, "minOutput": "29974258", "expectedPoolBoxId": "a1f4c2e8b7d93052416fbc8e07d21935a8c4f60b2e91d7385c06a4b1f2e83d97" }'
```

## Example response

```json
{
  "outcome": "accepted",
  "amountOut": "30124883",
  "txId": "40b7d045a858d537cdbf072e46ffbb86c0c9f6b0e93c369c1ffcba76797636d7"
}
```
