# API

## Swagger <a href="#swagger" id="swagger"></a>

{% openapi src="/files/tN2c0XH9icoXTz2KjluX" path="/v1/quote" method="get" %}
[mosaic-api.yaml](https://2052832733-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FV8Xc6SWICSNHJsYKexJR%2Fuploads%2Fgit-blob-ec8d66f05c7703d3f56c6d028a3841287c62271b%2Fmosaic-api.yaml?alt=media)
{% endopenapi %}

{% openapi src="/files/tN2c0XH9icoXTz2KjluX" path="/v1/tokens" method="get" %}
[mosaic-api.yaml](https://2052832733-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FV8Xc6SWICSNHJsYKexJR%2Fuploads%2Fgit-blob-ec8d66f05c7703d3f56c6d028a3841287c62271b%2Fmosaic-api.yaml?alt=media)
{% endopenapi %}

## Setup Authentication

{% hint style="info" %}
**All requests to Mosaic’s API must include an `X-API-Key` header for authentication. The API key is used to track your usage and ensure secure access. To obtain an API key, please contact the Mosaic team via** [***Discord***](https://discord.gg/mosaicagg) ***or*** [***Telegram***](http://t.me/mosaicaggchat)***.***
{% endhint %}

## Get quote

To retrieve the best swap rate, send the following HTTP request to get a quote:

```bash
curl 'https://api.mosaic.ag/v1/quote?srcAsset=0x1%3A%3Aaptos_coin%3A%3AAptosCoin&dstAsset=0x275f508689de8756169d1ee02d889c777de1cebda3a7bbcce63ba8a27c563c6f%3A%3Atokens%3A%3AUSDC&amount=1000000000&sender=0x0000000000000000000000000000000000000000000000000000000000000000&slippage=10' \
--header 'x-api-key: xxx'
```

This will return the best available quote for swapping the source asset (AptosCoin) into the destination asset (USDC) based on current market conditions.

Here is a detailed guide using Typescript:

### 1. Setup Environment

Import the required libraries and initialize your connection to the Aptos testnet using the `AptosConfig` and `Aptos` objects from the `@aptos-labs/ts-sdk`. This sets up your environment for making requests to the Mosaic Aggregator API and interacting with Aptos.

```javascript
import {
  Account,
  Aptos,
  APTOS_COIN,
  AptosConfig,
  Ed25519PrivateKey,
} from "@aptos-labs/ts-sdk";
import axios from "axios";

// Initialize Aptos config to connect to the testnet
const aptos = new Aptos(
  new AptosConfig({
    fullnode: "https://aptos.testnet.suzuka.movementlabs.xyz/v1",
  })
);
```

### 2. Setup User Account

Here, you will create an Aptos account using the user's private key. You need to fill in your own private key when initializing the account.

```javascript
// Create user account from private key
const user = Account.fromPrivateKey({
  privateKey: new Ed25519PrivateKey(), // TODO: Fill your private key here.
});
```

### 3. Define Assets and Amount

Specify the assets to swap and the amount (in APTOS decimals):

```javascript
const srcAsset = APTOS_COIN; // Source: APTOS Coin
const dstAsset =
  "0x275f508689de8756169d1ee02d889c777de1cebda3a7bbcce63ba8a27c563c6f::tokens::USDC"; // Destination: USDC
const amount = 1_00000000; // Amount: 1 APT (APTOS uses 8 decimals)
```

### 4. Get a Quote from Mosaic Aggregator API

Retrieve the best swap rate by making a GET request to the Mosaic API.

```javascript
// Get a quote from Mosaic Aggregator API
const mosaicResponse = await axios({
  method: "GET",
  url: "https://api.mosaic.ag/v1/quote",
  params: {
    srcAsset,
    dstAsset,
    amount,
    sender: user.accountAddress.toString(),
    slippage: 100, // 100 = 1%
  },
  headers: {
    "X-API-KEY": "xxx", // TODO: Fill the API key in here.
  },
});
```

### 5. Build the transaction

Once you have the quote, you use the returned data (function, type arguments, and function arguments) to build a transaction that will perform the swap.

```javascript
// Build the transaction based on the Mosaic response
const transaction = await aptos.transaction.build.simple({
  sender: user.accountAddress,
  data: {
    function: mosaicResponse.data.data.tx.function,
    typeArguments: mosaicResponse.data.data.tx.typeArguments,
    functionArguments: mosaicResponse.data.data.tx.functionArguments,
  },
});
```

### 6. Sign and submit the transaction

After building the transaction, you sign it using the user's private key and submit the transaction to the Aptos blockchain. The transaction hash will be logged, which can be used to check the transaction status on the explorer.

```javascript
// Sign and submit the transaction
const pendingTransactionResponse =
  await aptos.transaction.signAndSubmitTransaction({
    signer: user,
    transaction: transaction,
  });

// Output transaction URL to the console
console.log(
  `Tx = https://explorer.movementnetwork.xyz/txn/${pendingTransactionResponse.hash}?network=testnet`
);
```

*By following this guide, you will be able to integrate the Mosaic Aggregator API for performing token swaps on the Aptos network. For further assistance or inquiries, feel free to reach out to the Mosaic support team on* [*Discord*](https://discord.gg/mosaicagg) *or* [*Telegram*](http://t.me/mosaicaggchat)*.*\
\\


---

# 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://docs.mosaic.ag/swap-integration/api.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.
