Skip to content

Quickstart

This page swaps 1 NEAR to USDT. It takes one HTTP request to get the route and a few signed transactions to execute it.

The examples use fetch and NEAR Connect (@hot-labs/near-connect). For signing on a server, see Executing a route.

  1. Request a route.

    All amounts are raw integers in the token’s smallest unit. 1 NEAR is 10^24 yoctoNEAR.

    const params = new URLSearchParams({
    token_in: 'near',
    token_out: 'usdt.tether-token.near',
    amount_in: '1000000000000000000000000', // 1 NEAR
    max_wait_ms: '2000',
    slippage_type: 'Fixed',
    slippage: '0.01', // 1%
    trader_account_id: 'alice.near',
    });
    const response = await fetch(`https://router.intear.tech/route?${params}`);
    if (!response.ok) throw new Error(await response.text());
    const quotes = await response.json();

    Always pass trader_account_id. Without it, the route can’t include the storage deposits and registrations the trader is missing, and the swap may fail or lose tokens.

  2. Pick a quote.

    The response is an array of quotes, one per DEX that found a route, sorted best first. It’s already ranked by what the trader gets after gas and storage costs, so in most cases you take the first one. An empty array means no DEX found a route.

    [
    {
    "dex_id": "RheaDcl",
    "estimated_amount": { "amount_out": "4880007" },
    "worst_case_amount": { "amount_out": "4831206" },
    "has_slippage": true,
    "token_output": "nep141:usdt.tether-token.near",
    "execution_instructions": [ ... ],
    "estimated_gas_usage": "8231794617168100000000",
    "storage_deposits": "1250000000000000000000",
    ...
    },
    ...
    ]

    Show the user estimated_amount as the expected result and worst_case_amount as the minimum they will receive.

  3. Sign and send the transactions in order.

    Each item in execution_instructions is a NEAR transaction. Send them one by one, in order, and wait for each one to succeed before sending the next.

    import { NearConnector } from '@hot-labs/near-connect';
    const connector = new NearConnector({ network: 'mainnet' });
    await connector.connect();
    const wallet = await connector.wallet();
    const quote = quotes[0];
    for (const { NearTransaction: tx } of quote.execution_instructions) {
    await wallet.signAndSendTransaction({
    receiverId: tx.receiver_id,
    actions: tx.actions.map(({ FunctionCall: call }) => ({
    type: 'FunctionCall',
    params: {
    methodName: call.method_name,
    args: JSON.parse(atob(call.args)), // args are base64-encoded JSON
    gas: String(call.gas),
    deposit: call.deposit,
    },
    })),
    });
    }

    See Executing a route for the instruction format and edge cases.

  4. Check token_output.

    If token_output is the same as your token_out, you’re done. If it’s different, the tokens landed in another form of the same token (for example wNEAR instead of NEAR) and need one more conversion step. See Tokens and locations.