Skip to main content
In this section, you’ll learn how to execute a token swap using the transaction data generated from a previously obtained quote.
Prerequisites: Complete the Get a Quote guide first to obtain a valid quote.

Overview

After obtaining a quote, you can execute the swap by generating the necessary transaction data (bytecode) and submitting it to the blockchain. The Deframe API handles the complexity of preparing the transaction data for different providers and swap types.
Trustless Operation: You sign and submit all transactions yourself. Deframe never has custody of your funds.

Executing a Swap

Endpoint

POST /swap/bytecode

Request Body

ParameterTypeRequiredDescription
quoteIdstringThe quote ID from the /swap/quote endpoint
originAddressstringThe wallet address that will sign and send the transaction
destinationAddressstringThe destination wallet address to receive the swapped tokens
rawQuoteobjectThe rawQuote object from the quote response

Example Request

curl --request POST \
  --url https://api.deframe.io/swap/bytecode \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: YOUR_API_KEY' \
  --data '{
    "quoteId": "550e8400-e29b-41d4-a716-446655440000",
    "originAddress": "0x1234567890123456789012345678901234567890",
    "destinationAddress": "0x1234567890123456789012345678901234567890",
    "rawQuote": {
      // Provider-specific quote data from previous step
    }
  }'

Response Format

The API returns transaction data ready to be signed and executed:
{
  "chainId": 137,
  "transactionData": [
    {
      "to": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
      "data": "0x095ea7b3000000000000000000000000111111125421ca6dc452d289314280a0f8842a6500000000000000000000000000000000000000000000000000000000000f4240",
      "value": "0",
      "chainId": 137
    },
    {
      "to": "0x111111125421ca6dc452d289314280a0f8842a65",
      "data": "0x07ed2379...",
      "value": "0",
      "chainId": 137
    }
  ]
}

Response Fields

The chain ID where these transactions should be executed
Array of transactions to execute in order. May include:
  • Approval transaction: Grants permission to the swap contract
  • Swap transaction: Executes the actual token swap
  • Tracking transaction: Records the swap in Deframe’s system
The contract address to send the transaction to
Encoded transaction data (bytecode) for the contract call
Amount of native token to send (in wei). Usually “0” unless swapping native tokens.

Transaction Execution

You’ll need to sign and submit the transaction data to the blockchain. This happens on your end - we don’t touch your funds, custody remains yours.

Using ethers.js

import { ethers } from 'ethers'

// 1. Create a provider and signer
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()

// 2. Execute transactions in order
for (const tx of response.transactionData) {
  const transaction = await signer.sendTransaction({
    to: tx.to,
    data: tx.data,
    value: tx.value,
    chainId: tx.chainId
  })

  // Wait for confirmation
  await transaction.wait()
  console.log(`Transaction confirmed: ${transaction.hash}`)
}

Using wagmi/viem

import { useSendTransaction, useWaitForTransaction } from 'wagmi'

function SwapComponent() {
  const { sendTransaction } = useSendTransaction()

  const executeSwap = async (bytecode) => {
    for (const tx of bytecode.transactionData) {
      const { hash } = await sendTransaction({
        to: tx.to,
        data: tx.data,
        value: BigInt(tx.value),
        chainId: tx.chainId
      })

      console.log(`Transaction sent: ${hash}`)
    }
  }

  return <button onClick={() => executeSwap(response)}>Execute Swap</button>
}

Swap Types

The endpoint automatically handles different swap types:
Same-chain swaps (e.g., USDT to USDC on Ethereum):
  • Usually 2-3 transactions: approve + swap (+ tracking)
  • Executes immediately on the same blockchain
  • Lower fees, faster execution

Error Handling

The API returns appropriate error messages for:
  • Missing required parameters
  • Invalid or expired quote
  • Quote not found or already used
  • Provider-specific errors during transaction preparation
Example Error Response:
{
  "error": "Quote not found or expired",
  "code": "QUOTE_NOT_FOUND"
}
Always check that the quote hasn’t expired before executing. Quotes are typically valid for 5 minutes.

Complete Workflow

1

Get Quote

Use GET /swap/quote to get swap quote and pricing
2

Generate Bytecode

Use POST /swap/bytecode with the quote to generate transaction data
3

Execute Transactions

Sign and send the transactions using your wallet
4

Track Status

Transaction status tracking is available in an upcoming release. For now, monitor your transaction directly on-chain using the transaction hash returned after execution.
For cross-chain swaps, use GET /swap/status/{id} to monitor progress

Next Steps