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

# Plan Swap

> Create a plan to swap one asset for another

# Plan Swap

Creates a plan to swap one asset for another via DEX aggregation. Legend fetches the best available quote and returns a plan with the swap details. Returns a plan ID and an EIP-712 digest that must be signed to authorize execution.

## Request

```
POST /accounts/:account_id/plan/swap
```

### Path parameters

| Parameter    | Type   | Description        |
| ------------ | ------ | ------------------ |
| `account_id` | string | The sub-account ID |

### Body parameters

| Parameter     | Type   | Required | Description                                                                                    |
| ------------- | ------ | -------- | ---------------------------------------------------------------------------------------------- |
| `sell_asset`  | string | Yes      | Asset to sell (e.g., `"USDC"`)                                                                 |
| `buy_asset`   | string | Yes      | Asset to buy (e.g., `"WETH"`)                                                                  |
| `network`     | string | Yes      | Target network (e.g., `"base"`, `"mainnet"`). Funds on other chains are bridged automatically. |
| `sell_amount` | string | One of   | Amount to sell in the asset's smallest unit                                                    |
| `buy_amount`  | string | One of   | Amount to buy in the asset's smallest unit                                                     |

Exactly one of `sell_amount` or `buy_amount` must be provided.

## Examples

### Sell exact amount

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/plan/swap \
    -H "Authorization: Bearer $LEGEND_QUERY_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "sell_asset": "USDC",
      "buy_asset": "WETH",
      "network": "base",
      "sell_amount": "10000000"
    }'
  ```

  ```typescript TypeScript theme={null}
  const plan = await client.plan.swap("acc_2o0yiljtp378", {
    sellAsset: "USDC",
    buyAsset: "WETH",
    network: "base",
    sellAmount: "10000000",
  });
  ```

  ```python Python theme={null}
  plan = await client.plan.swap(
      "acc_2o0yiljtp378",
      sell_asset="USDC",
      buy_asset="WETH",
      network="base",
      sell_amount="10000000",
  )
  ```

  ```rust Rust theme={null}
  use legend_client::SwapParams;

  let plan = client.plan.swap("acc_2o0yiljtp378", &SwapParams {
      sell_asset: "USDC".into(),
      buy_asset: "WETH".into(),
      network: "base".into(),
      sell_amount: Some("10000000".into()),
      buy_amount: None,
  }).await?;
  ```
</CodeGroup>

### Buy exact amount

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/plan/swap \
    -H "Authorization: Bearer $LEGEND_QUERY_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "sell_asset": "USDC",
      "buy_asset": "WETH",
      "network": "base",
      "buy_amount": "1000000000000000000"
    }'
  ```

  ```typescript TypeScript theme={null}
  const plan = await client.plan.swap("acc_2o0yiljtp378", {
    sellAsset: "USDC",
    buyAsset: "WETH",
    network: "base",
    buyAmount: "1000000000000000000",
  });
  ```

  ```python Python theme={null}
  plan = await client.plan.swap(
      "acc_2o0yiljtp378",
      sell_asset="USDC",
      buy_asset="WETH",
      network="base",
      buy_amount="1000000000000000000",
  )
  ```

  ```rust Rust theme={null}
  use legend_client::SwapParams;

  let plan = client.plan.swap("acc_2o0yiljtp378", &SwapParams {
      sell_asset: "USDC".into(),
      buy_asset: "WETH".into(),
      network: "base".into(),
      sell_amount: None,
      buy_amount: Some("1000000000000000000".into()),
  }).await?;
  ```
</CodeGroup>

```json Response (201 Created) theme={null}
{
  "plan_id": "pln_71mcui082png",
  "details": {
    "quark_operation_actions": [...],
    "eip712_data": {
      "digest": "0x8a3f...b7c2",
      "domain_separator": "0x...",
      "hash_struct": "0x..."
    }
  },
  "expires_at": "2025-06-15T10:32:00Z"
}
```

### Response fields

| Field                        | Type   | Description                                                 |
| ---------------------------- | ------ | ----------------------------------------------------------- |
| `plan_id`                    | string | Unique identifier for this plan (`pln_` prefix)             |
| `details`                    | object | Transaction details including EIP-712 signing data          |
| `details.eip712_data.digest` | string | The hash to sign with the account's signer key              |
| `expires_at`                 | string | ISO 8601 timestamp — plan must be executed before this time |

## Errors

| Status | Code                | Description                                                               |
| ------ | ------------------- | ------------------------------------------------------------------------- |
| 400    | `invalid_params`    | Missing required parameters or both sell\_amount and buy\_amount provided |
| 400    | `plan_failed`       | Could not generate a valid plan (e.g., insufficient balance)              |
| 400    | `no_wallet`         | Account has no wallets                                                    |
| 408    | `timeout`           | Folio computation timed out                                               |
| 422    | `swap_quote_failed` | Could not fetch a swap quote from the DEX aggregator                      |

## Notes

* Plans expire after 2 minutes. Sign and execute via [Execute Plan](/api-reference/plan-execute) before expiry.
* Provide exactly one of `sell_amount` or `buy_amount`. If `sell_amount` is given, the API finds the best quote for selling that exact amount. If `buy_amount` is given, it finds a quote to buy that exact amount.
* A 0.15% Legend fee is applied to the buy side of the quote.
