Skip to main content
Your application may need to allow users to swap between two assets. You want your users to get the best price available across the ecosystem. Aggregators check many liquidity sources and find the best price. Integrating an aggregator API can thus be a convenient and efficient means of enabling swapping in your app. In this guide, we will examine integrating one such aggregator, Kuru Flow. You’ll learn how to integrate Kuru Flow into a frontend application—from authentication to fetching quotes and executing swaps.

Requirements

Before you begin, ensure you have the following:
  • A frontend framework with wallet connection (React, Next.js, etc.)
  • wagmi and viem for wallet interactions
  • User wallet connected to Monad mainnet (chain ID 143)

1. Configure Authentication

We first need to get credentials to access the Kuru Flow API. Kuru Flow uses JWT tokens for API authentication.
lib/kuru-flow.ts

2. Get a Swap Quote

The /api/quote endpoint calculates the best swap path and returns transaction data. The following parameters are supported: *Either autoSlippage: true OR slippageTolerance must be provided. We can write a small function to interact with the quote API:
lib/kuru-flow.ts

Response Structure

lib/kuru-flow.ts
The calldata field does NOT include the 0x prefix. You must add it before sending the transaction.

3. Check Token Allowance (ERC20 Only)

Before executing a swap with ERC20 tokens, we should verify that the router has permission to spend the user’s tokens.
Native MON does not require approval. Skip this step when swapping native MON.
components/SwapCard.tsx

4. Approve Token Spending (ERC20 Only)

If allowance is insufficient for an ERC20 token, we should request approval before swapping.
components/SwapCard.tsx
Use viem’s encodeFunctionData instead of manual hex string manipulation for reliable ABI encoding.

5. Execute the Swap

To submit the swap, we should use the transaction data from the quote response:
components/SwapCard.tsx

Token Addresses and Decimals (Mainnet)

Be careful with token decimals when formatting output. USDC uses 6 decimals, not 18.

Referral Fee System

Kuru features a referral system. As the referrer, we can earn fees on every swap by including your referrer details in quote requests.
lib/kuru-flow.ts
Example fee calculations at 50 bps (0.5%):
Replace 0xYOUR_WALLET_ADDRESS_HERE with your actual wallet address before deploying. The referrerAddress must be a valid Ethereum address or transactions will fail.

Error Handling

Along the way, we might run into errors. Here is how to interpret them:

Common Mistakes

  1. Missing 0x prefix - Add 0x prefix to calldata before sending
  2. Invalid referrer address - Must be a valid Ethereum address
  3. Skipping approval for ERC20 - ERC20 tokens require approval before swap (native MON does not)
  4. Wrong decimals - USDC has 6 decimals, MON has 18
  5. Zero value for native MON swaps - When swapping native MON, the transaction value must be set to the swap amount

Next Steps