Guide

About Swap API

The Swap API connects you with Native's Credit Pool liquidity and competitive quotes, powered by Native Swap Engine.

Native V2 supports two types of orders:

Each order type will have their own corresponding Swap API endpoints.

Set up a Swap using FirmQuote APIs

1. Get an API key

To start testing, please fill out the form and our team will get back to you with the API key.

2. Get orderbook

The latest prices for each pair Native quotes on a specified chain are published in an orderbook.

Our orderbook is updated every second with the latest prices quoted by our partners

Here's how to retrieve the orderbook:

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://v2.api.native.org/swap-api-v2/v1/orderbook?chain=ethereum',
  'headers': {
    'apikey': 'your-api-key'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Refer to GET Orderbook for the full reference.

Sample response

[
    {
        "base_symbol": "WETH",
        "quote_symbol": "USDT",
        "base_address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "quote_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "minimum_in_base": 0,
        "side": "bid",
        "levels": [
            [
                0.0001,
                3210.12
            ],
            [
                42,
                3210.13
            ],
            [
                12.75786733219471,
                3213.12345
            ]
        ]
    }
}

3. Get a firm quote

Next, obtain a quote with a price that Native is committed to. If the quote is favorable, you can execute it by submitting the associated call data to the Native Router on-chain.

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://v2.api.native.org/swap-api-v2/v1/firm-quote?chain=ethereum&address=0x46b2deae6eff3011008ea27ea36b7c27255ddfa9&amount=1&token_in=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&token_out=0xdac17f958d2ee523a2206206994597c13d831ec7',
  'headers': {
    'apikey': 'your-api-key'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Sample response

{
    "router_version": "1",
    "success": true,
    "source": "p1.4",
    "functionCall": "tradeRFQT",
    "orders": [
        {
            "pool": "0x28cacd5e26a719f139e2105ca1efc3d9dc892826",
            "signer": "0xff8ba4d1fc3762f6154cc942ccf30049a2a0cec6",
            "recipient": "0x46b2deae6eff3011008ea27ea36b7c27255ddfa9",
            "sellerToken": "0x0000000000000000000000000000000000000000",
            "buyerToken": "0xdac17f958d2ee523a2206206994597c13d831ec7",
            "sellerTokenAmount": "1000000000000000000",
            "buyerTokenAmount": "2740580786",
            "deadlineTimestamp": 1724642218,
            "nonce": 3291440531122899,
            "quoteId": "0xb46b8131a5144a5bb2214fc35a3403db",
            "multiHop": false,
            "signature": "0x4512df300b8c8de9b3e58c29d7a32868c0e9f45d81c9414899849eaa646ead085e432eb9c7bc4c55f96f0f959ca76690f3be9932a0de7950a2152360094d00a91b",
            "externalSwapCalldata": "0x",
            "amountOutMinimum": "2740580786",
            "widgetFee": {
                "feeRate": "30",
                "feeRecipient": "0xfA7f2F2ce037fC87846fD118E944Db50C31875eb"
            },
            "widgetFeeSignature": "0xc760273902ddad1e1af436281f2887e1186a1ae319ad9ffae760d3db9ecd59ca76bf4d3b7761c68582ffdb8e91b4d3238494a3e36a5a8b5be7b016e8f014d8fd1b"
        }
    ],
    "calldata": "0x000000000000000000000000000000000000000000000000000bb18c37a82ad3ff8ba4d1fc3762f6154cc942ccf30049a2a0cec628cacd5e26a719f139e2105ca1efc3d9dc89282646b2deae6eff3011008ea27ea36b7c27255ddfa9dac17f958d2ee523a2206206994597c13d831ec7c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000a359f1b20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000066cbf3aa46b2deae6eff3011008ea27ea36b7c27255ddfa9b46b8131a5144a5bb2214fc35a3403db4512df300b8c8de9b3e58c29d7a32868c0e9f45d81c9414899849eaa646ead085e432eb9c7bc4c55f96f0f959ca76690f3be9932a0de7950a2152360094d00a91b",
    "amountIn": "1000000000000000000",
    "amountOut": "2740580786",
    "toWrap": false,
    "toUnwrap": false,
    "fallbackSwapDataArray": [],
    "txRequest": {
        "target": "0xEAd050515E10fDB3540ccD6f8236C46790508A76",
        "calldata": "0xe525b10b000000000000000000000000000000000000000000000000000000000000002000000000000000000000000028cacd5e26a719f139e2105ca1efc3d9dc892826000000000000000000000000ff8ba4d1fc3762f6154cc942ccf30049a2a0cec600000000000000000000000046b2deae6eff3011008ea27ea36b7c27255ddfa90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000a359f1b20000000000000000000000000000000000000000000000000000000066cbf3aa000000000000000000000000000000000000000000000000000bb18c37a82ad3b46b8131a5144a5bb2214fc35a3403db000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026000000000000000000000000067297ee4eb097e072b4ab6f1620268061ae8046400000000000000000000000060cba82ddbf4b5ddcd4398cdd05354c6a790c309000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e0000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000a359f1b200000000000000000000000000000000000000000000000000000000000000414512df300b8c8de9b3e58c29d7a32868c0e9f45d81c9414899849eaa646ead085e432eb9c7bc4c55f96f0f959ca76690f3be9932a0de7950a2152360094d00a91b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041c760273902ddad1e1af436281f2887e1186a1ae319ad9ffae760d3db9ecd59ca76bf4d3b7761c68582ffdb8e91b4d3238494a3e36a5a8b5be7b016e8f014d8fd1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "value": "1000000000000000000"
    },
    "amountInOffset": 196,
    "amountOutMinimumOffset": 0
}

4. Submit call data to Native Router

To execute a firm quote, submit a transaction on-chain to call the Native Router with the returned call data.

You can find all the deployed addresses with their ABIs here: Addresses

Here’s an example of how to submit call data to the NativeRouter:

import axios from 'axios';
import {ethers} from "ethers";
// import routerAbi from './nativeRouter.json'

const apiKey = 'your-api-key'
const baseUrl = 'https://v2.api.native.org/swap-api-v2/v1/';

const provider = 'https://eth.llamarpc.com';
const routerAddress = '0x5C0aBf0F651613696A5c57efafC6ab59A460B32d'; // Refer to Contract Address section -> NativeRouter (Proxy) for the address

const walletAddress = ''; // Your address
const privateKey = ''; // Your private key for the address above

// Swap input
const chain = 'ethereum'; // 1, 56
const tokenIn = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'; // ETH
const tokenOut = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; // USDT
const amount = 1; // in ether, not in wei

type FirmQuoteResult = {
  orders: any[],
  widgetFee: {
    signer: string,
    feeRecipient: string,
    feeRate: number
  },
  widgetFeeSignature: string,
  calldata: string,
  amountIn: string,
  amountOut: string,
  fallbackData: string
}

async function callFirmQuote(): Promise<FirmQuoteResult> {
  const endpoint = 'firm-quote?';
  const headers: any = {
    api_key: apiKey,
  };

  const response = await axios
    .get(
      `${baseUrl}${endpoint}chain=${chain}&token_in=${tokenIn}&token_out=${tokenOut}&amount=${amount}&address=${walletAddress}`,
      {
        headers,
      }
    )
  console.log('Firm quote result', response.data)
  return response.data;
}

async function callRouterToSwap(firmQuoteResult: FirmQuoteResult) {
  const jsonRpcProvider = new ethers.JsonRpcProvider(provider);
  const signer = new ethers.Wallet(privateKey, jsonRpcProvider);
  const tx = await signer.sendTransaction({to: routerAddress, data: firmQuoteResult.calldata});
  await tx.wait()
}

async function main() {
  const firmQuoteResult = await callFirmQuote();
  await callRouterToSwap(firmQuoteResult)
}

main();

Last updated