> For the complete documentation index, see [llms.txt](https://dirolprotocols-organization.gitbook.io/dirol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dirolprotocols-organization.gitbook.io/dirol/api/get-swap.md).

# Get swap

### What `/swap` returns

`GET /swap` returns the best route and a ready-to-sign transaction object.

It includes the same routing fields as `/quote` plus `tx`.

### Request parameters

* `tokenIn` — input token address.
* `tokenOut` — output token address.
* `amount` — input amount in raw units.
* `recipient` — address that receives the output tokens.
* `slippageBps` — optional slippage in basis points. Default: `50`.
* `excludeSources` — optional comma-separated source IDs.

### Example request

```bash
curl "https://api.dirol.io/api/v1/swap?tokenIn=0x3bd359C1119dA7Da1D913D1C4D2B7c461115433A&tokenOut=0x754704Bc059F8C67012fEd69BC8A327a5aafb603&amount=1000000000000000000&recipient=0x9603e52146d02Aa2eD5B6A71E43933ac07940B12"
```

### Example response

```json
{
  "tokenIn": "0x3bd359c1119da7da1d913d1c4d2b7c461115433a",
  "tokenOut": "0x754704bc059f8c67012fed69bc8a327a5aafb603",
  "amountIn": "1000000000000000000",
  "amountOut": "385274",
  "routes": [...],
  "priceImpactBps": 12,
  "amountInUsd": "0.39",
  "amountOutUsd": "0.39",
  "tx": {
    "to": "0x646462f4d0168A94fE1884c8ae82148a3618A18d",
    "data": "0xd53e7c84...",
    "value": "0",
    "estimatedGas": "350000"
  }
}
```

### Approval flow

{% stepper %}
{% step %}

### Get the swap transaction

Call `/swap` with your token pair, amount, and recipient.

Read the `tx` object from the response.
{% endstep %}

{% step %}

### Approve ERC-20 input tokens

If the input token is an ERC-20, approve `tx.to` to spend `amountIn`.

Skip this step for native `MON`.
{% endstep %}

{% step %}

### Send the transaction

Send the returned `tx.data` to `tx.to`.

Use `tx.value` for ERC-20 swaps. For native `MON`, send `value = amountIn`.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
For native `MON` swaps, use the `WMON` address as `tokenIn` when requesting `/swap`.
{% endhint %}

### ethers.js example

```typescript
import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider(process.env.MONAD_RPC_URL!);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);

const ERC20_ABI = [
  "function approve(address spender, uint256 amount) external returns (bool)"
];

const tokenIn = "0x754704Bc059F8C67012fEd69BC8A327a5aafb603";
const tokenOut = "0x3bd359C1119dA7Da1D913D1C4D2B7c461115433A";
const amount = "1000000";
const recipient = await signer.getAddress();
const isNativeIn = false;

const params = new URLSearchParams({
  tokenIn,
  tokenOut,
  amount,
  recipient
});

const res = await fetch(`https://api.dirol.io/api/v1/swap?${params}`);
const swap = await res.json();

if (!isNativeIn) {
  const token = new ethers.Contract(tokenIn, ERC20_ABI, signer);
  const approveTx = await token.approve(swap.tx.to, amount);
  await approveTx.wait();
}

const sentTx = await signer.sendTransaction({
  to: swap.tx.to,
  data: swap.tx.data,
  value: isNativeIn ? BigInt(amount) : BigInt(swap.tx.value),
  gasLimit: BigInt(swap.tx.estimatedGas)
});

console.log(sentTx.hash);
```

### viem example

```typescript
import { createWalletClient, custom, parseAbi } from "viem";

const [account] = await window.ethereum.request({
  method: "eth_requestAccounts"
});

const monadMainnet = {
  id: 143,
  name: "Monad Mainnet",
  nativeCurrency: { name: "Monad", symbol: "MON", decimals: 18 },
  rpcUrls: {
    default: {
      http: ["https://your-monad-rpc"]
    }
  }
};

const client = createWalletClient({
  account,
  chain: monadMainnet,
  transport: custom(window.ethereum)
});

const erc20Abi = parseAbi([
  "function approve(address spender, uint256 amount) returns (bool)"
]);

const tokenIn = "0x754704Bc059F8C67012fEd69BC8A327a5aafb603";
const tokenOut = "0x3bd359C1119dA7Da1D913D1C4D2B7c461115433A";
const amount = "1000000";
const recipient = account;
const isNativeIn = false;

const params = new URLSearchParams({
  tokenIn,
  tokenOut,
  amount,
  recipient
});

const res = await fetch(`https://api.dirol.io/api/v1/swap?${params}`);
const swap = await res.json();

if (!isNativeIn) {
  await client.writeContract({
    address: tokenIn,
    abi: erc20Abi,
    functionName: "approve",
    args: [swap.tx.to, BigInt(amount)]
  });
}

const hash = await client.sendTransaction({
  account,
  to: swap.tx.to,
  data: swap.tx.data,
  value: isNativeIn ? BigInt(amount) : BigInt(swap.tx.value),
  gas: BigInt(swap.tx.estimatedGas)
});

console.log(hash);
```

### Transaction fields

* `tx.to` — Dirol aggregator contract.
* `tx.data` — encoded calldata.
* `tx.value` — native value to send.
* `tx.estimatedGas` — estimated gas limit.

### Notes

* `/swap` is the endpoint to use for execution.
* `recipient` must be a valid Monad address.
* For ERC-20 inputs, approval must happen before the swap transaction.
* For native `MON`, skip approval and send `value = amountIn`.
