Skip to main content
POST
/
v1
/
uniswap
/
swap
/
buy_exactly
Python (SDK)
from compass_api_sdk import CompassAPI, models


with CompassAPI(
    api_key_auth="<YOUR_API_KEY_HERE>",
) as compass_api:

    res = compass_api.uniswap_v3.uniswap_swap_buy_exactly(token_in="USDC", token_out="USDT", fee=models.FeeEnum.ZERO_DOT_01, amount_out=0.1, max_slippage_percent=0.5, chain=models.UniswapBuyExactlyRequestChain.BASE, sender="0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac", estimate_gas=True)

    # Handle response
    print(res)
import { CompassApiSDK } from "@compass-labs/api-sdk";

const compassApiSDK = new CompassApiSDK({
  apiKeyAuth: "<YOUR_API_KEY_HERE>",
});

async function run() {
  const result = await compassApiSDK.uniswapV3.uniswapSwapBuyExactly({
    tokenIn: "USDC",
    tokenOut: "USDT",
    fee: "0.01",
    amountOut: 0.1,
    maxSlippagePercent: 0.5,
    chain: "base",
    sender: "0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac",
  });

  console.log(result);
}

run();
curl --request POST \
  --url https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '
{
  "chain": "base",
  "sender": "0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac",
  "token_in": "USDC",
  "token_out": "USDT",
  "fee": "0.01",
  "amount_out": 0.1,
  "max_slippage_percent": 0.5,
  "wrap_eth": false
}
'
const options = {
  method: 'POST',
  headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
  body: JSON.stringify({
    chain: 'base',
    sender: '0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac',
    token_in: 'USDC',
    token_out: 'USDT',
    fee: '0.01',
    amount_out: 0.1,
    max_slippage_percent: 0.5,
    wrap_eth: false
  })
};

fetch('https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'chain' => 'base',
    'sender' => '0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac',
    'token_in' => 'USDC',
    'token_out' => 'USDT',
    'fee' => '0.01',
    'amount_out' => 0.1,
    'max_slippage_percent' => 0.5,
    'wrap_eth' => false
  ]),
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/json",
    "x-api-key: <api-key>"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly"

	payload := strings.NewReader("{\n  \"chain\": \"base\",\n  \"sender\": \"0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac\",\n  \"token_in\": \"USDC\",\n  \"token_out\": \"USDT\",\n  \"fee\": \"0.01\",\n  \"amount_out\": 0.1,\n  \"max_slippage_percent\": 0.5,\n  \"wrap_eth\": false\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("x-api-key", "<api-key>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly")
  .header("x-api-key", "<api-key>")
  .header("Content-Type", "application/json")
  .body("{\n  \"chain\": \"base\",\n  \"sender\": \"0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac\",\n  \"token_in\": \"USDC\",\n  \"token_out\": \"USDT\",\n  \"fee\": \"0.01\",\n  \"amount_out\": 0.1,\n  \"max_slippage_percent\": 0.5,\n  \"wrap_eth\": false\n}")
  .asString();
require 'uri'
require 'net/http'

url = URI("https://api.compasslabs.ai/v1/uniswap/swap/buy_exactly")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"chain\": \"base\",\n  \"sender\": \"0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac\",\n  \"token_in\": \"USDC\",\n  \"token_out\": \"USDT\",\n  \"fee\": \"0.01\",\n  \"amount_out\": 0.1,\n  \"max_slippage_percent\": 0.5,\n  \"wrap_eth\": false\n}"

response = http.request(request)
puts response.read_body
{
  "transaction": {
    "chainId": "0x2105",
    "data": "0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d",
    "from": "0x4A83b4413CF41C3244027e1590E35a0F48403F0c",
    "gas": "0x7a120",
    "maxFeePerGas": "0x59682f00",
    "maxPriorityFeePerGas": "0x3b9aca00",
    "nonce": "0x5",
    "to": "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
    "value": "0x0"
  },
  "amount_in_quote": "<string>"
}
{
  "detail": [
    {
      "loc": [
        "<string>"
      ],
      "msg": "<string>",
      "type": "<string>"
    }
  ]
}

Authorizations

x-api-key
string
header
required

Your Compass API Key. Get your key here.

Body

application/json

Request model for buying an exact amount of tokens.

token_in
string
default:USDC
required

The symbol or address of the token to swap from..

Example:

"WETH"

token_out
string
default:USDT
required

The symbol or address of the token to swap to..

Example:

"WETH"

fee
enum<string>
default:0.01
required

The swap fee of the pool

Available options:
0.01,
0.05,
0.3,
1.0
Example:

"0.3"

amount_out
default:0.1
required

The amount of 'token_out' to buy.

Required range: x > 0
Example:

1.5

max_slippage_percent
number
default:0.5
required

The maximum slippage allowed in percent. e.g. 1 means 1 % slippage allowed.

Required range: 0 <= x <= 10
Example:

0.5

chain
enum<string>
default:base
required
Available options:
arbitrum,
base,
ethereum
Example:

"base"

sender
string
default:0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac
required

The address of the transaction sender.

Example:

"0x29F20a192328eF1aD35e1564aBFf4Be9C5ce5f7B"

action_type
string
default:UNISWAP_BUY_EXACTLY
Allowed value: "UNISWAP_BUY_EXACTLY"
estimate_gas
boolean
default:true

Determines whether to estimate gas costs for transactions, also verifying that the transaction can be successfully executed.

Response

Successful Response

transaction
UnsignedTransaction · object
required

The unsigned transaction data. User must sign and broadcast to network.

Example:
{
  "chainId": "0x2105",
  "data": "0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d",
  "from": "0x4A83b4413CF41C3244027e1590E35a0F48403F0c",
  "gas": "0x7a120",
  "maxFeePerGas": "0x59682f00",
  "maxPriorityFeePerGas": "0x3b9aca00",
  "nonce": "0x5",
  "to": "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
  "value": "0x0"
}
amount_in_quote
string
required

The estimated amount in for the transaction. The actual input amount for this transaction is guaranteed be within the acceptable threshold, defined by the max_slippage_percent, relative to this quote.

Pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$