π Monad Meta Aggregator Swap API Guide
This guide shows how to fetch a swap quote using the monad meta aggregator API with viem in typescript.
π endpointβ
GET https://alpha.clober.io/api/quote
π₯ query parametersβ
name | type | description |
---|---|---|
inputTokenAddress | string | address of the input token (e.g. native ETH is 0x0000000000000000000000000000000000000000 ) |
outputTokenAddress | string | address of the token you want to receive |
amountIn | string | amount of input token in wei |
slippageLimitPercent | string | slippage limit in percent (e.g. "1" for 1%) |
userAddress | string (optinal) | user's wallet address (if userAddress is not provided, the API will not build a transaction) |
π¦ example requestβ
GET https://alpha.clober.io/api/quote?inputTokenAddress=0x0000000000000000000000000000000000000000&outputTokenAddress=0xf817257fed379853cDe0fa4F97AB987181B1E5Ea&amountIn=1000000000000000000&slippageLimitPercent=1&userAddress=0x5F79EE8f8fA862E98201120d83c4eC39D9468D49
π§ͺ using viem + typescriptβ
import axios from 'axios'
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { monadTestnet } from 'viem/chains'
const account = privateKeyToAccount('0xYOUR_PRIVATE_KEY')
const client = createWalletClient({
account,
chain: monadTestnet,
transport: http(),
})
async function fetchAndExecuteQuote() {
const inputToken = '0x0000000000000000000000000000000000000000' // native ETH
const outputToken = '0xf817257fed379853cDe0fa4f97AB987181B1E5Ea'
const amountIn = '1000000000000000000' // 1 ETH in wei
const slippage = '1'
const userAddress = account.address
try {
const { data } = await axios.get('https://alpha.clober.io/api/quote', {
params: {
inputTokenAddress: inputToken,
outputTokenAddress: outputToken,
amountIn,
slippageLimitPercent: slippage,
userAddress,
},
}) as {
data: {
bestQuote: {
aggregator: string
amountOut: string
transaction: {
to: `0x${string}`
data: `0x${string}`
value: string
gas: string
gasPrice: string
}
}
}
}
const best = data.bestQuote
console.log('β
best aggregator:', best.aggregator)
console.log('π expected amountOut:', best.amountOut)
const tx = best.transaction
const hash = await client.sendTransaction({
to: tx.to as `0x${string}`,
data: tx.data as `0x${string}`,
value: BigInt(tx.value),
gas: BigInt(tx.gas),
gasPrice: BigInt(tx.gasPrice),
})
console.log('π sent tx hash:', hash)
} catch (err) {
console.error('β error while quoting or sending tx:', err)
}
}
fetchAndExecuteQuote()
π sample responseβ
this is a sample response from the monad meta aggregator quote api. it includes the best available quote and a list of all quotes from supported aggregators.
{
"bestQuote": {
"amountOut": "1528866",
"aggregator": "Monorail",
"transaction": {
"from": "0x5F79EE8f8fA862E98201120d83c4eC39D9468D49",
"to": "0xfD845859628946B317A78A9250DA251114FbD846",
"data": "0xb69cbf9f0000...", // encoded calldata
"value": "1000000000000000000",
"gas": "940662",
"gasPrice": "52000000000"
},
"executionMilliseconds": 330.06097
},
"allQuotes": [
{
"amountOut": "1528866",
"aggregator": "Monorail",
"transaction": {
"from": "0x5F79EE8f8fA862E98201120d83c4eC39D9468D49",
"to": "0xfD845859628946B317A78A9250DA251114FbD846",
"data": "0xb69cbf9f0000...",
"value": "1000000000000000000",
"gas": "940662",
"gasPrice": "52000000000"
},
"executionMilliseconds": 330.06097
},
{
"amountOut": "1522248",
"aggregator": "OpenOcean",
"transaction": {
"from": "...",
"to": "...",
"data": "...",
"value": "1000000000000000000",
"gas": "872528",
"gasPrice": "52000000000"
},
"executionMilliseconds": 347.45342
},
{
"amountOut": "1521628",
"aggregator": "Clober",
"transaction": {
"from": "...",
"to": "...",
"data": "...",
"value": "1000000000000000000",
"gas": "693416",
"gasPrice": "52000000000"
},
"executionMilliseconds": 274.84042
}
]
}
amountOut
: expected output token amount after the swap (in smallest unit, e.g. wei)aggregator
: name of the aggregator/router that provides the quotetransaction
: full transaction object that can be submitted viaviem.sendTransaction()
from
: address that will initiate the transaction (usually userβs wallet address)to
: router contract address to be calleddata
: encoded calldata for executing the swapvalue
: amount of ETH to send along with the transaction (if input is native ETH)gas
: suggested gas limit for executing the swapgasPrice
: suggested gas price in wei
executionMilliseconds
: time taken to compute this quote (used for diagnostics)