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

> ## Agent Instructions
> Soroswap Aggregator API: base URL https://api.soroswap.finance, pick the network with ?network=mainnet or ?network=testnet (omitted means mainnet).
> Auth: Authorization: Bearer sk_... on every call. Get a key at https://console.soroswap.finance/register. A missing or invalid key returns 401 with code and registerUrl (POST /send returns 403 instead); an expired JWT or a missing role returns 403. Free plan 1 request/s; see https://docs.soroswap.finance/api/plans.md.
> Flow: POST /quote (assetIn, assetOut as C... contract addresses, amount as an integer string in the smallest unit, tradeType, protocols) -> POST /quote/build with the unchanged quote and from -> the user signs the XDR -> POST /send. Build right after quoting: a Soroban XDR is valid for 180 seconds. Submit it as is, never re-simulate it.
> Partner id: every account has one (9 hex chars, permanent). Read partnerId from GET /partner, or in the console at https://console.soroswap.finance under Fees & payouts. The partner fee itself is switched on by Soroswap; see https://docs.soroswap.finance/api/fees.md.
> AI tools: docs MCP at https://docs.soroswap.finance/mcp (claude mcp add --transport http soroswap-docs https://docs.soroswap.finance/mcp), skill with npx skills add https://docs.soroswap.finance. Other clients: https://docs.soroswap.finance/api/build-with-ai.md.
> Units and Stellar pitfalls: https://docs.soroswap.finance/api/stellar-gotchas.md. Errors and retries: https://docs.soroswap.finance/api/errors.md.

# Smart Contract Integration

> Calling the SoroswapRouter from your own Soroban contract, with the client import and authorization it needs.

How do you integrate Soroswap liquidity pools into your own DeFi protocol from a smart
contract, rather than from a frontend or the API?

You call the [SoroswapRouter](https://docs.soroswap.finance/amm/technical-reference/contracts/soroswap-router.md) from
your contract using a generated client. This is exactly what the aggregator's Soroswap
adapter does, and the pattern below is taken from
[`contracts/adapters/soroswap/src/protocol_interface.rs`](https://github.com/soroswap/aggregator/blob/main/contracts/adapters/soroswap/src/protocol_interface.rs)
in the aggregator repository.

## Import the router client

Generate a typed client from the router's WASM with `contractimport!`:

```rust theme={null}
use soroban_sdk::{Address, Env, Vec};

soroban_sdk::contractimport!(
    file = "./soroswap_contracts/soroswap_router.wasm"
);
pub type SoroswapRouterClient<'a> = Client<'a>;
```

You need the router WASM in your build. Fetch it from
[soroswap/core](https://github.com/soroswap/core) or from the deployed contract.

## Call it

```rust theme={null}
pub fn swap_through_soroswap(
    e: &Env,
    router: &Address,
    amount_in: &i128,
    amount_out_min: &i128,
    path: &Vec<Address>,
    to: &Address,
    deadline: &u64,
) -> Vec<i128> {
    let router_client = SoroswapRouterClient::new(e, router);

    router_client.swap_exact_tokens_for_tokens(
        amount_in,
        amount_out_min,
        path,
        to,
        deadline,
    )
}
```

The call returns the amount at each hop of `path`, not just the final output amount.

## Authorization

The router calls `to.require_auth()` inside the swap. That means the address receiving the
output has to authorize the call, so your contract cannot swap on a user's behalf unless
that user authorized the invocation, or `to` is your own contract address and your
contract authorizes it. Design your flow around one of those two, not around holding user
keys.

## Getting the router address

Take it from [Deployed Addresses](https://docs.soroswap.finance/amm/technical-reference/deployed-addresses.md), or read it
at runtime from the API:

```bash theme={null}
curl https://api.soroswap.finance/api/mainnet/router
# {"address":"C..."}
```

Store it as a constant or in contract storage rather than hardcoding it in several places.

## The rest of the interface

`swap_tokens_for_exact_tokens`, `add_liquidity` and `remove_liquidity` are called the same
way through the same client. Their signatures are on the
[SoroswapRouter](https://docs.soroswap.finance/amm/technical-reference/contracts/soroswap-router.md) page. If you only
need a price and not an execution, `router_get_amounts_out` and `router_quote` are
read-only and cost you no state.

> [!TIP]
>
> If you are routing across more than just Soroswap, call
> [Aggregator v3](https://docs.soroswap.finance/aggregator/technical-reference/contracts/aggregator-v3.md) instead of the
> router. It runs typed routes across Soroswap, Aquarius, SushiSwap V3 and Comet in one call.
> See [Cross-Contract Integration](https://docs.soroswap.finance/aggregator/technical-reference/cross-contract-integration.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.