Skip to main content

Introduction

Fibrous Router SDK is a TypeScript library that provides a simple and efficient way to integrate optimal token swaps into your decentralized applications. Built on top of the Fibrous Router API, it handles all the complexity of finding the best routes and executing swaps across multiple chains.

Why Use the SDK?

Type Safety

Full TypeScript support with comprehensive type definitions

Multi-Chain

Single interface for Base, HyperEVM, Scroll, Monad, and Starknet

Easy Integration

Simple API that abstracts away complex routing logic

Optimized Routes

Automatically finds the best swap paths across all DEXs

Supported Chains

The SDK supports the following blockchain networks:
ChainChain ID
StarknetSN_MAIN
Base8453
Scroll534352
HyperEVM998
Monad143

Quick Start

1

Install the SDK

npm install fibrous-router-sdk
2

Initialize the Router

import { Router as FibrousRouter } from "fibrous-router-sdk";

const fibrous = new FibrousRouter({
  apiVersion: "v2", // optional, v2 is the latest version
});
3

Get the Best Route

const chains = await fibrous.refreshSupportedChains();
const chainId = chains.find(chain => chain.chain_name == "base")?.chain_id;

const route = await fibrous.getBestRoute({
  amount: inputAmount,
  tokenInAddress,
  tokenOutAddress,
  chainId,
});
4

Execute the Swap

const { route, calldata } = await fibrous.buildRouteAndCalldata({
  inputAmount,
  tokenInAddress,
  tokenOutAddress,
  slippage: 0.5, // 0.5% slippage
  destination: userAddress,
  chainId,
});

Key Features

πŸ” Smart Routing

The SDK automatically finds the optimal route by:
  • Aggregating liquidity from multiple DEX protocols
  • Splitting large trades across multiple paths
  • Minimizing price impact and slippage
  • Considering gas costs in route optimization

Batch Operations

Execute multiple swaps in a single transaction (Starknet):
const routes = await fibrous.getBestRouteBatch(
  amounts,
  tokenInAddresses,
  tokenOutAddresses,
  "starknet"
);

Token Management

Easy access to supported tokens:
const tokens = await fibrous.supportedTokens(chainId);
const usdc = tokens.get("usdc");

Protocol Flexibility

Exclude specific protocols or use only certain ones:
const chains = await fibrous.refreshSupportedChains();
const chainId = chains.find(chain => chain.chain_name == "base")?.chain_id;

const route = await fibrous.getBestRoute({
  amount,
  tokenInAddress: tokenIn,
  tokenOutAddress: tokenOut,
  chainId,
  options: {
    excludeProtocols: [1, 2], // Protocol IDs, not names
    direct: false
  }
});

Complete Example

Here’s a complete example of swapping tokens on Base:
import { Router as FibrousRouter, getBestRouteParams, buildRouteAndCalldataParams } from "fibrous-router-sdk";
import { parseUnits } from "ethers";

async function swapTokens() {
  // Initialize router with v2 API
  const fibrous = new FibrousRouter({
    apiVersion: "v2",
  });
  
  // Refresh and get supported chains
  const chains = await fibrous.refreshSupportedChains();
  const chainId = chains.find(chain => chain.chain_name == "base")?.chain_id;
  if (!chainId) {
    throw new Error("Chain not supported");
  }
  
  // Get supported tokens
  const tokens = await fibrous.supportedTokens(chainId);
  const eth = tokens.get("eth");
  const usdc = tokens.get("usdc");
  
  if (!eth || !usdc) {
    throw new Error("Tokens not found");
  }
  
  // Prepare swap parameters
  const inputAmount = BigInt(parseUnits("1", Number(eth.decimals))); // 1 ETH
  const slippage = 0.5; // 0.5%
  const userAddress = "0xYourAddress";
  
  // Get best route
  const getBestRouteParams: getBestRouteParams = {
    amount: inputAmount,
    tokenInAddress: eth.address,
    tokenOutAddress: usdc.address,
    chainId,
  };
  const route = await fibrous.getBestRoute(getBestRouteParams);
  
  console.log(`Input: ${route.inputAmount} ${eth.symbol}`);
  console.log(`Output: ${route.outputAmount} ${usdc.symbol}`);
  
  // Build route and calldata
  const buildRouteAndCalldataParams: buildRouteAndCalldataParams = {
    inputAmount,
    tokenInAddress: eth.address,
    tokenOutAddress: usdc.address,
    slippage,
    destination: userAddress,
    chainId,
  };
  const { route: routeData, calldata } = await fibrous.buildRouteAndCalldata(buildRouteAndCalldataParams);
  
  // Execute with your wallet
  // const receipt = await wallet.sendTransaction(tx);
  
  return { route: routeData, calldata };
}

Chain-Specific Examples

const fibrous = new FibrousRouter({ apiVersion: "v2" });
const chains = await fibrous.refreshSupportedChains();
const chainId = chains.find(chain => chain.chain_name == "base")?.chain_id;

const route = await fibrous.getBestRoute({
  amount: BigInt(parseUnits("1", 18)),
  tokenInAddress: "0x0000000000000000000000000000000000000000", // ETH
  tokenOutAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC
  chainId,
});

SDK vs API

Use the SDK when:
  • Building a TypeScript/JavaScript application
  • You want type safety and autocomplete
  • You need simplified integration
  • Working with multiple chains
  • You want built-in error handling

Requirements

  • Node.js: Version 18.x or higher
  • Web3 Provider: ethers.js v6+ or starknet.js v5+
  • TypeScript: Version 4.5+ (recommended)

Installation

npm install fibrous-router-sdk

Next Steps

Resources

Support

Need help? We’re here for you: