# Swap via Smart Router

**URL:** <https://forum.sun.io/t/swap-via-smart-router/794>\
**Category:** Swap\
**Tags:** smartcontract\
**Created:** [October 24, 2025, 6:47am UTC](https://forum.sun.io/t/swap-via-smart-router/794 "2025-10-24T06:47:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunio](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@sunio](https://forum.sun.io/u/sunio)\
**Post date:** [October 24, 2025, 6:47am UTC](https://forum.sun.io/t/swap-via-smart-router/794/1 "2025-10-24T06:47:01Z")

</div>

## **Step 1: Install Editor/IDE: Visual Studio Code (VS Code)**

- Official download: [https://code.visualstudio.com](https://code.visualstudio.com/)

- After downloading, simply extract and install

## **Step 2: Install Node: Use `nvm` to Install and Switch Node.js**

- Install NVM

```auto
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

```

- Load NVM:

```auto
source ~/.zshrc # If you use zsh
# Or:
source ~/.bash_profile

```

- Install the latest version of Node.js:

```auto
nvm install 22

```

- Switch to the newly installed version:

```auto
nvm use 22

```

- Set the new version as default:

```auto
nvm alias default 22

```

- Verify versions:

```auto
node -v
npm -v

```

## **Step 3: Clone Project Code Locally**

- GitHub address: [https://github.com/sun-protocol/SmartExchangeRouter](https://github.com/sun-protocol/SmartExchangeRouter)

## **Step 4: Install All Required Components**

- Open the project file with VS Code

- Enter the following in the bottom command line for automatic installation

```auto
npm install

```

## **Step 5: Configure Private Key**

```auto
export PRIVATE_KEY_NILE=Your private key

```

- After configuration, enter `env` to check if the configuration is successful

## **Step 6: Obtain Optimal Exchange Path Based on Smart Routing Calculation Service Interface**

- After obtaining the optimal 3 exchange paths, you can build transactions based on the optimal path

- For more details, refer to [Smart Routing Calculation Service](https://docs.sun.io/DEVELOPERS/Swap/SmartRouter/CalculationService)

## **Step 7: Build Transaction**

- Users can call the `swapExactInput` method of the smart contract to trigger transactions. The Smart Router will process the transaction data internally and complete the transaction.

- For more details, refer to [Smart Routing Swap Functions](https://docs.sun.io/DEVELOPERS/Swap/SmartRouter/SwapFunctions)

## **Step 8: Complete Code**

- For complete code, please refer to the `request.mjs` file in the project. You can replace the function parameters at the bottom according to your needs.

- You can also refer to [Smart Routing Swap Functions —\> Complete Code](https://docs.sun.io/DEVELOPERS/Swap/SmartRouter/SwapFunctions)

## **Step 9: Execute Transaction**

```auto
node scripts/request.mjs

```

- After the transaction is successfully executed, it returns the transaction hash. You can further view the detailed information of the transaction on [tronscan](https://tronscan.org/) using the hash.
