# SDK

We provide a TypeScript SDK to interact with our API, making it easy to add token swaps to your app.

## Installation

You can install the [Rainbow Swap SDK](https://www.npmjs.com/package/rainbow-swap-sdk) using either npm or Yarn:

**Using npm:**

```
npm install rainbow-swap-sdk
```

**Using Yarn:**

```
yarn add rainbow-swap-sdk
```

## Basic Usage

The following example demonstrates a simple setup to execute a swap of 1.35 TON to USDT.

```typescript
import {getAssetsList, getBestRoute, toNano} from 'rainbow-swap-sdk';

// 1. Load the list of available tokens
const assetsList = await getAssetsList({
    userAssets: [] // Array of asset addresses the user holds; see AssetsListParams for more details.
});

// Retrieve specific assets by their address
const inputAsset = assetsList.find(asset => asset.address === 'ton');
const outputAsset = assetsList.find(asset => asset.address === 'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs');

// 2. Load the best swap route and swap messages
const bestRouteResponse = await getBestRoute({
    inputAssetAmount: toNano('1.35', inputAsset.decimals).toString(), // Convert 1.35 TON to nano format
    inputAssetAddress: inputAsset.address,
    outputAssetAddress: outputAsset.address,
    senderAddress: 'UQDGGjjuwhikx8ZPJsrLbKXGq7mx26D8pK_l8GqBejzB52Pa', // Optional user wallet address; if set, swap messages will be returned
    partnerId: 'demo-partner' // Optional unique identifier in our App Developer Partnership program
});

// 3. Sign and send messages to the blockchain to execute the swap.
// This example uses the React UI client. For other frameworks, refer to https://docs.ton.org/develop/dapps/ton-connect/overview
import {useTonConnectUI} from '@tonconnect/ui-react';

const [tonConnectUI] = useTonConnectUI();

const result = await tonConnectUI.sendTransaction({
    validUntil: Math.floor(Date.now() / 1000) + 60, // 60 seconds from now
    messages: bestRouteResponse.swapMessages
});
```

## Live Example

For a live example of using the SDK, visit the [Rainbow Swap Frontend Repository](https://github.com/0xblackbot/rainbow-swap).

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rainbow-ag.gitbook.io/docs/technical/sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
