// Introduction

Quickstart

From install to a settled swap. This walkthrough uses the TypeScript SDK. The same steps are GET /v1/:chain/quote and GET /v1/:chain/swap. To quote on your own RPC, GET /v1/:chain/quotePlan and POST the offer to /v1/:chain/swap.

1. Install#

Add the SDK to any TypeScript or JavaScript project.

bash
npm install @routerx.exchange/sdk

Quoting works without a wallet. The SDK loads a quote plan from the RouterX API, then runs those calls over HTTPS JSON-RPC — the built-in public endpoints, or rpcUrls when you pass them. A wallet provider is required to approve and send swaps.

2. Request a quote#

Create a client for the chain you operate on and ask for the best gas-adjusted route. Amounts are decimal strings in the token's smallest unit.

quote.ts
import { RouterX } from '@routerx.exchange/sdk'

const rx = new RouterX({ chainId: 8453 })

const quote = await rx.swap.quote({
  tokenIn: '0x4200000000000000000000000000000000000006', // WETH
  tokenOut: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
  amountIn: '1000000000000000000', // 1 WETH
})

console.log(quote.amountOut)
console.log(quote.offer.path)
console.log(quote.router)

The quote is the plan

quote.offer holds the adapter path and amounts. Pass that quote into rx.swap.swap or buildSwapTx so the signed route is the one just shown. It is reused while quotedAt is within 30 seconds.

3. Settle the swap#

Attach a wallet when it is time to sign. The quote from the previous step is passed through, so the swap does not read a second price.

swap.ts
rx.setProvider(window.ethereum)

const { hash, minAmountOut, tx } = await rx.swap.swap({
  tokenIn: quote.tokenIn,
  tokenOut: quote.tokenOut,
  amountIn: quote.amountIn,
  to: account,
  slippage: 0.5,
  quote,
})

console.log('settled:', hash, minAmountOut)

Simulate before you fire

For liquidation and arbitrage flows, call rx.swap.buildSwapTx(…) first and eth_call the resulting tx against the current block before broadcasting.

Next steps#