How to monetize your app using 0x Swap API
This guide covers how you can monetize your app by using the 0x Swap API.
Introduction​
As you build your DeFi business, it’s likely that you are including swaps directly in-app to help your users conveniently trade at the best price. As your business grows, you may consider low-friction ways to monetize in order to generate revenue and build a sustainable Web3 business.
This guide covers two basic monetization options - collecting affiliate fees and collecting trade surplus. It also covers pricing considerations, along with code samples and a demo app to help you implement these options.
Regardless of which 0x pricing plan your team is using, both monetization options are available to all plans.
How to monetize on trades​
Out-of-the-box with 0x Swap API, you have two monetization options:
- Collect affiliate fees (aka trading fee or commission)
- Collect trade surplus (aka positive slippage)
Option 1: Collect affiliate fees​
As a 0x Swap API integrator, you have full flexibility to collect an affiliate fee on any trade made through your application.
Setup requires including the following two parameters when making a Swap API request:
feeRecipient
 - The ETH address that should receive affiliate feesbuyTokenPercentageFee
 - The percentage of thebuyAmount
(tokens received by the user) that should be attributed tofeeRecipient
(your wallet) as affiliate fees. Denoted as a decimal between 0 - 1.0 where 1.0 represents 100%.
Example API call​
https://api.0x.org/swap/v1/quote // Request a firm quote
?sellToken=0x6B175474E89094C44Da98b954EedeAC495271d0F // Sell DAI
&sellAmount=4000000000000000000000 // Sell amount: 4000 (18 decimal)
&buyToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE // Buy ETH
&takerAddress=$USER_TAKER_ADDRESS // Address that will make the trade
&feeRecipient=$INTEGRATOR_WALLET_ADDRESS // Wallet address that should receive the affiliate fees
&buyTokenPercentageFee=0.01 // Percentage of buyAmount that should be attributed as affiliate fees
--header '0x-api-key: [API_KEY]' // Replace with your own API key
When the transaction has gone through, the fee amount will be sent to the feeRecipient
 address you've set. The fee is received in the buyToken
 (the token that the user will receive).
If you would like to receive a specific type of token (e.g. USDC), you will need to convert those on your own.
Displaying fees in the UI​
The fee amount is incorporated as part of the quoted price. Two recommended methods of displaying the fees are:
- display the amount returned byÂ
grossBuyAmount * buyTokenPercentageFee
- display the
grossBuyAmount
and thebuyTokenPercentageFee
separately
The following are examples from the 0x demo app and production-level apps that show these two options.
Pricing considerations​
When deciding how much to set your fee amount, consider the following. We recommend setting your pricing in a way that strengthens your bottom line, aligning it with the value you provide to customers while considering any transaction costs. Note that the additional affiliate fee will impact the price for the end user, so find that sweet spot where your solution remains competitive and impactful.
Option 2: Collect trade surplus​
Trade surplus, also known as positive slippage, occurs when the user ends up receiving more tokens than their quoted amount. 0x Swap API can be easily configured so that you collect the trade surplus and send that to a specified address.
This can be done by setting the feeRecipientTradeSurplus
 parameter in a Swap API request.
feeRecipientTradeSurplus
 represents the wallet address you want to collect the fee in. When a transaction produces trade surplus, 100% of it will be collected in that wallet. The fee is received in the buyToken
 (the token that the user will receive). If you would like to receive a specific type of token (e.g. USDC), you will need to make that conversion on your own.
When feeRecipientTradeSurplus
 is not specified, the feature is effectively OFF and all trade surplus will be passed back to the user.
Note: Trade surplus is only sent to feeRecipientTradeSurplus
for SELLs (i.e. when the sellAmount is specified). It is a no-op for BUYs (i.e. when the buyAmount is specified), which means the user will always receive the trade surplus.
Example API call​
https://api.0x.org/swap/v1/quote // Request a firm quote
?sellToken=0x6B175474E89094C44Da98b954EedeAC495271d0F // Sell DAI
&sellAmount=4000000000000000000000 // Sell amount: 4000 (18 decimal)
&buyToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE // Buy ETH
&takerAddress=$USER_TAKER_ADDRESS // Address that will make the trade
&feeRecipientTradeSurplus=$INTEGRATOR_WALLET_ADDRESS // The recipient of any trade surplus fees
--header '0x-api-key: [API_KEY]' // Replace with your own API key
Demo app​
Want to see these monetization practices implemented in a live demo? Checkout this demo app to see how to implement collecting affiliate fees in practice:
👉 Live demo