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

# Using Soroswap with TypeScript

> Swapping and managing liquidity from TypeScript with the official @soroswap/sdk, from quote to signed transaction.

The supported way to use Soroswap from TypeScript is the official SDK,
[`@soroswap/sdk`](https://www.npmjs.com/package/@soroswap/sdk). It wraps the Soroswap API,
so you get routing across every supported protocol without assembling Soroban invocations
by hand.

> [!NOTE]
>
> If you need to call the contracts directly from another **contract**, that is a different
> job: see [Smart Contract Integration](https://docs.soroswap.finance/amm/technical-reference/smart-contract-integration.md).

## Install

```bash theme={null}
pnpm add @soroswap/sdk
```

You need an API key. Sign up at
[console.soroswap.finance](https://console.soroswap.finance/register) and generate one under
API keys; keys start with `sk_`.

> [!WARNING]
>
> The API key is a secret. Keep it server side. Do not ship it in a browser bundle.

## Initialize

```typescript theme={null}
import {
  SoroswapSDK,
  SupportedNetworks,
  SupportedProtocols,
  TradeType,
} from '@soroswap/sdk';

const soroswap = new SoroswapSDK({
  apiKey: process.env.SOROSWAP_API_KEY!,
  defaultNetwork: SupportedNetworks.MAINNET,
});
```

| Option | Meaning |
| - | - |
| `apiKey` | Your key, starting with `sk_`. Required. |
| `baseUrl` | Override the API host. Defaults to `https://api.soroswap.finance`. |
| `defaultNetwork` | `SupportedNetworks.MAINNET` or `SupportedNetworks.TESTNET`. |
| `timeout` | Request timeout in ms. Defaults to 30000. |

Every method takes an optional trailing `network` argument that overrides
`defaultNetwork` for that one call.

## The swap flow

A swap is four steps: quote, build, sign, send. The SDK does three of them; signing is
yours, because the SDK never holds a key.

### 1. Quote

```typescript theme={null}
const quote = await soroswap.quote({
  assetIn: 'CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA',
  assetOut: 'CDTKPWPLOURQA2SGTKTUQOWRCBZEORB4BWBOMJ3D3ZTQQSGE5F6JBQLV',
  amount: 10000000n,
  tradeType: TradeType.EXACT_IN,
  protocols: [
    SupportedProtocols.SOROSWAP,
    SupportedProtocols.AQUA,
    SupportedProtocols.SDEX,
  ],
  slippageBps: 50,
});
```

`amount` is a **BigInt**, in the asset's smallest unit. Passing a `number` will not work.

`tradeType` is `TradeType.EXACT_IN` when you know what you are spending, or
`TradeType.EXACT_OUT` when you know what you want to receive.

Other fields on the request: `parts`, `maxHops`, `assetList`, `feeBps` and
`gaslessTrustline`. `slippageBps` and `feeBps` are basis points, so 50 is 0.5%.

To see which protocols a network currently supports:

```typescript theme={null}
const protocols = await soroswap.getProtocols(SupportedNetworks.MAINNET);
```

### 2. Build

```typescript theme={null}
const { xdr } = await soroswap.build({
  quote,
  from: userAddress,
  to: userAddress, // optional, defaults to `from`
});
```

If the quote carried a `feeBps`, `build` also requires `referralId`, the address the fee
is paid to.

### 3. Sign

The SDK returns an unsigned XDR and stops there. Sign it with whatever the surrounding app
already uses: a browser wallet through
[`stellar-wallets-kit`](https://github.com/Creit-Tech/Stellar-Wallets-Kit), or a keypair
server side.

```typescript theme={null}
const signedXdr = await yourSigner.sign(xdr);
```

### 4. Send

```typescript theme={null}
const result = await soroswap.send(signedXdr);
```

## Pools and prices

```typescript theme={null}
// Every pool on the given protocols
const pools = await soroswap.getPools(
  SupportedNetworks.MAINNET,
  [SupportedProtocols.SOROSWAP, SupportedProtocols.AQUA],
);

// One pair
const pool = await soroswap.getPoolByTokens(
  assetA,
  assetB,
  SupportedNetworks.MAINNET,
  [SupportedProtocols.SOROSWAP],
);

const price = await soroswap.getPrice([assetA, assetB]);
const balances = await soroswap.getBalances(userAddress);
```

## Liquidity

`addLiquidity` and `removeLiquidity` return an XDR the same way `build` does: you sign and
send it yourself.

```typescript theme={null}
const { xdr } = await soroswap.addLiquidity({
  assetA,
  assetB,
  amountA: 1000000n,
  amountB: 2000000n,
  to: userAddress,
  slippageBps: '50',
});
```

> [!WARNING]
>
> Fetch the pool first and derive `amountA` and `amountB` from its current ratio. Amounts
> that do not match the pool's proportions fail during simulation.

```typescript theme={null}
const positions = await soroswap.getUserPositions(userAddress);
```

## Finding the router address

If you need the deployed router address rather than the SDK:

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

The addresses are also listed on
[Deployed Addresses](https://docs.soroswap.finance/amm/technical-reference/deployed-addresses.md).

## Understanding the route

A swap walks the `path` array pair by pair, exchanging at each step
(`0 <-> 1`, `1 <-> 2`, and so on to `n <-> n+1`) until the route completes. Every extra hop
costs fees and slippage, which is why routing quality matters and why the API computes it
for you rather than leaving you to guess a path.

## Reference

* SDK source and full type definitions:
  [github.com/soroswap/sdk](https://github.com/soroswap/sdk)
* REST reference, if you would rather call the API directly:
  [API reference](https://docs.soroswap.finance/api-reference)
* Getting started with the API: [Soroswap Aggregator API](https://docs.soroswap.finance/index.md)


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