> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compasslabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Buy exact amount

> This endpoint allows users to trade a variable amount of one token to receive an
exact amount of another token using the Uniswap protocol.

The transaction is executed on the specified blockchain network, and the user must
provide the necessary transaction details, including the token to buy, the token to
pay with, and the exact amount to receive. If the token being paid with is ETH and
needs to be wrapped, the appropriate amount will be wrapped automatically.
                    <Info>
                    **Required Allowances**

                        In order to make this transaction, token allowances need to be set for the following contracts.

                     - `UniswapV3Router`
                    </Info>
                



## OpenAPI

````yaml /v1/combined_spec.json post /v1/uniswap/swap/buy_exactly
openapi: 3.1.0
info:
  title: Compass API
  description: Compass Labs DeFi API
  version: 0.0.1
servers:
  - url: https://api.compasslabs.ai
    description: Production server
security:
  - ApiKeyAuth: []
paths:
  /v1/uniswap/swap/buy_exactly:
    post:
      tags:
        - Uniswap V3
      summary: Buy exact amount
      description: >-
        This endpoint allows users to trade a variable amount of one token to
        receive an

        exact amount of another token using the Uniswap protocol.


        The transaction is executed on the specified blockchain network, and the
        user must

        provide the necessary transaction details, including the token to buy,
        the token to

        pay with, and the exact amount to receive. If the token being paid with
        is ETH and

        needs to be wrapped, the appropriate amount will be wrapped
        automatically.
                            <Info>
                            **Required Allowances**

                                In order to make this transaction, token allowances need to be set for the following contracts.

                             - `UniswapV3Router`
                            </Info>
                        
      operationId: v1_uniswap_swap_buy_exactly
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UniswapBuyExactlyRequest'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UniswapBuyExactlyTransactionResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      x-codeSamples:
        - lang: python
          label: Python (SDK)
          source: |-
            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)
        - lang: typescript
          label: Typescript (SDK)
          source: |-
            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();
components:
  schemas:
    UniswapBuyExactlyRequest:
      properties:
        action_type:
          type: string
          const: UNISWAP_BUY_EXACTLY
          title: Action Type
          default: UNISWAP_BUY_EXACTLY
        token_in:
          type: string
          title: Token
          description: The symbol or address of the token to swap from..
          examples:
            - WETH
          default: USDC
        token_out:
          type: string
          title: Token
          description: The symbol or address of the token to swap to..
          examples:
            - WETH
          default: USDT
        fee:
          $ref: '#/components/schemas/FeeEnum'
          description: The swap fee of the pool
          examples:
            - '0.3'
          default: '0.01'
        amount_out:
          anyOf:
            - type: number
              exclusiveMinimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Amount Out
          description: The amount of 'token_out' to buy.
          examples:
            - 1.5
          default: 0.1
        max_slippage_percent:
          type: number
          maximum: 10
          minimum: 0
          title: Max Slippage Percent
          description: >-
            The maximum slippage allowed in percent. e.g. `1` means `1 %`
            slippage allowed.
          examples:
            - 0.5
          default: 0.5
        chain:
          type: string
          enum:
            - arbitrum
            - base
            - ethereum
          title: Chain
          default: base
          examples:
            - base
        sender:
          type: string
          title: Sender
          description: The address of the transaction sender.
          examples:
            - '0x29F20a192328eF1aD35e1564aBFf4Be9C5ce5f7B'
          default: '0x7a27794511C14F6afFDE618D3d8ef7B9C24058Ac'
        estimate_gas:
          type: boolean
          title: Estimate Gas
          description: >-
            Determines whether to estimate gas costs for transactions, also
            verifying that the transaction can be successfully executed.
          default: true
      type: object
      required:
        - token_in
        - token_out
        - fee
        - amount_out
        - max_slippage_percent
        - chain
        - sender
      title: UniswapBuyExactlyRequest
      description: Request model for buying an exact amount of tokens.
      default:
        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
      required_allowances:
        - UniswapV3Router
    UniswapBuyExactlyTransactionResponse:
      properties:
        transaction:
          anyOf:
            - $ref: '#/components/schemas/UnsignedTransaction'
            - $ref: '#/components/schemas/UserOperationResponse'
          title: Transaction
          description: >-
            The unsigned transaction data. User must sign and broadcast to
            network.
        amount_in_quote:
          type: string
          pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Amount In Quote
          description: >-
            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.
      type: object
      required:
        - transaction
        - amount_in_quote
      title: UniswapBuyExactlyTransactionResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    FeeEnum:
      type: string
      enum:
        - '0.01'
        - '0.05'
        - '0.3'
        - '1.0'
      title: FeeEnum
      description: |-
        The transaction fee of a Uniswap pool in bips.

        Uniswap supports 4 different fee levels.
    UnsignedTransaction:
      properties:
        chainId:
          type: string
          title: Chainid
          description: The chain id of the transaction
        data:
          type: string
          title: Data
          description: The data of the transaction
        from:
          type: string
          title: From
          description: The sender of the transaction
        gas:
          anyOf:
            - type: string
            - type: 'null'
          title: Gas
          description: The gas of the transaction
        to:
          type: string
          title: To
          description: The recipient of the transaction
        value:
          type: string
          title: Value
          description: The value of the transaction
        nonce:
          type: string
          title: Nonce
          description: The nonce of the address
        maxFeePerGas:
          type: string
          title: Maxfeepergas
          description: The max fee per gas of the transaction
        maxPriorityFeePerGas:
          type: string
          title: Maxpriorityfeepergas
          description: The max priority fee per gas of the transaction
      type: object
      required:
        - chainId
        - data
        - from
        - gas
        - to
        - value
        - nonce
        - maxFeePerGas
        - maxPriorityFeePerGas
      title: UnsignedTransaction
      example:
        chainId: '0x2105'
        data: >-
          0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d
        from: '0x4A83b4413CF41C3244027e1590E35a0F48403F0c'
        gas: '0x7a120'
        maxFeePerGas: '0x59682f00'
        maxPriorityFeePerGas: '0x3b9aca00'
        nonce: '0x5'
        to: '0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67'
        value: '0x0'
    UserOperationResponse:
      properties:
        to:
          type: string
          title: To
          description: The target contract address for the operation
        data:
          type: string
          title: Data
          description: The calldata for the operation
        value:
          type: string
          title: Value
          description: The ETH value to send with the operation
      type: object
      required:
        - to
        - data
        - value
      title: UserOperationResponse
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: >-
        Your Compass API Key. Get your key
        [here](https://www.compasslabs.ai/dashboard).

````