import { setOnchainKitConfig } from '@coinbase/onchainkit';
import { buildSwapTransaction } from '@coinbase/onchainkit/api';
import type { Token } from '@coinbase/onchainkit/token';
setOnchainKitConfig({ apiKey: 'YOUR_API_KEY' });
const fromToken: Token = {
name: 'ETH',
address: '',
symbol: 'ETH',
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
chainId: 8453,
};
const toToken: Token = {
name: 'USDC',
address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
symbol: 'USDC',
decimals: 6,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2',
chainId: 8453,
};
const response = await buildSwapTransaction({
fromAddress: '0x...',
from: fromToken,
to: toToken,
amount: '0.1',
useAggregator: false,
});{
"approveTransaction": {
"chainId": 8453,
"data": "",
"gas": 0,
"to": "",
"value": 0
},
"fee": {
"baseAsset": {
"name": "USDC",
"address": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"currencyCode": "USDC",
"decimals": 6,
"imageURL": "https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2",
"blockchain": "eth",
"aggregators": [],
"swappable": true,
"unverified": false,
"chainId": 8453
},
"percentage": "1",
"amount": "3517825"
},
"quote": {
"from": {
"address": "",
"chainId": 8453,
"decimals": 18,
"image": "https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png",
"name": "ETH",
"symbol": "ETH"
},
"to": {
"address": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"chainId": 8453,
"decimals": 6,
"image": "https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2",
"name": "USDC",
"symbol": "USDC"
},
"fromAmount": "100000000000000000",
"toAmount": "348264739",
"amountReference": "from",
"priceImpact": "",
"chainId": 8453,
"highPriceImpact": false,
"slippage": "3",
"warning": {
"type": "warning",
"message": "This transaction has a very high likelihood of failing if submitted",
"description": "failed with 500000000 gas: insufficient funds for gas * price + value: address 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed have 0 want 100000000000000000"
}
},
"transaction": {
"chainId": 8453,
"data": "0x...",
"gas": 419661,
"to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
"value": 100000000000000000
},
"warning": {
"type": "warning",
"message": "This transaction has a very high likelihood of failing if submitted",
"description": "failed with 500000000 gas: insufficient funds for gas * price + value: address 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed have 0 want 100000000000000000"
}
}import { setOnchainKitConfig } from '@coinbase/onchainkit';
import { buildSwapTransaction } from '@coinbase/onchainkit/api';
import type { Token } from '@coinbase/onchainkit/token';
setOnchainKitConfig({ apiKey: 'YOUR_API_KEY' });
const fromToken: Token = {
name: 'ETH',
address: '',
symbol: 'ETH',
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
chainId: 8453,
};
const toToken: Token = {
name: 'USDC',
address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
symbol: 'USDC',
decimals: 6,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2',
chainId: 8453,
};
const response = await buildSwapTransaction({
fromAddress: '0x...',
from: fromToken,
to: toToken,
amount: '0.1',
useAggregator: false,
});{
"approveTransaction": {
"chainId": 8453,
"data": "",
"gas": 0,
"to": "",
"value": 0
},
"fee": {
"baseAsset": {
"name": "USDC",
"address": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"currencyCode": "USDC",
"decimals": 6,
"imageURL": "https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2",
"blockchain": "eth",
"aggregators": [],
"swappable": true,
"unverified": false,
"chainId": 8453
},
"percentage": "1",
"amount": "3517825"
},
"quote": {
"from": {
"address": "",
"chainId": 8453,
"decimals": 18,
"image": "https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png",
"name": "ETH",
"symbol": "ETH"
},
"to": {
"address": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"chainId": 8453,
"decimals": 6,
"image": "https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2",
"name": "USDC",
"symbol": "USDC"
},
"fromAmount": "100000000000000000",
"toAmount": "348264739",
"amountReference": "from",
"priceImpact": "",
"chainId": 8453,
"highPriceImpact": false,
"slippage": "3",
"warning": {
"type": "warning",
"message": "This transaction has a very high likelihood of failing if submitted",
"description": "failed with 500000000 gas: insufficient funds for gas * price + value: address 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed have 0 want 100000000000000000"
}
},
"transaction": {
"chainId": 8453,
"data": "0x...",
"gas": 419661,
"to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
"value": 100000000000000000
},
"warning": {
"type": "warning",
"message": "This transaction has a very high likelihood of failing if submitted",
"description": "failed with 500000000 gas: insufficient funds for gas * price + value: address 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed have 0 want 100000000000000000"
}
}buildSwapTransaction function is used to get an unsigned transaction for a swap between two Tokens.
Before using this function, make sure to obtain a Client API Key from Coinbase Developer Platform.
Promise<BuildSwapTransactionResponse>
BuildSwapTransactionParams
Wallet address initiating the swap
Show child attributes
Token name (e.g., "ETH")
Contract address of the token
Token symbol (e.g., "ETH")
Number of decimals for the token
URL to the token’s image
Chain ID where the token resides
Show child attributes
Token name (e.g., "ETH")
Contract address of the token
Token symbol (e.g., "ETH")
Number of decimals for the token
URL to the token’s image
Chain ID where the token resides
Amount of the “from” token to swap (in smallest unit or decimal string)
Whether to use aggregator routing
Successful response
Show child attributes
Chain ID for the approval transaction
ABI‐encoded calldata for the approval
Gas limit for the approval transaction
Contract address to which the approval is sent
Amount of native currency to send (in wei)
Show child attributes
Show child attributes
Name of the fee asset
Contract address of the fee asset
Symbol of the fee asset
Number of decimals for the fee asset
URL for the fee asset’s image
Underlying blockchain identifier (e.g., “eth”)
List of aggregator identifiers or objects
Whether this asset can be swapped
Indicates if the asset is unverified
Chain ID where the fee asset resides
Fee percentage (as a string, e.g., “1” for 1%)
Fee amount in native currency (in smallest unit)
Show child attributes
Show child attributes
Contract address of the “from” token
Chain ID for the “from” token
Decimal precision of the “from” token
Image URL of the “from” token
Name of the “from” token
Symbol of the “from” token
Show child attributes
Contract address of the “to” token
Chain ID for the “to” token
Decimal precision of the “to” token
Image URL of the “to” token
Name of the “to” token
Symbol of the “to” token
Amount of “from” token (in smallest unit)
Amount of “to” token (in smallest unit)
Indicates which amount is the reference (e.g., “from”)
Estimated price impact (as a string)
Chain ID for this quote
Whether the price impact is considered high
Allowed slippage percentage (as a string)
Show child attributes
Chain ID for the swap transaction
ABI‐encoded calldata for the swap
Gas limit for the swap transaction
Contract address to which the swap is sent
Amount of native currency to send (in wei)
Was this page helpful?