I was wondering if there's a PancakeSwap API, that allows me to swap BNB for a token, and if it isn't possible, can I do it with Binance Smart Chain's API?
-
The documentation is at https://docs.pancakeswap.finance/code/smart-contracts/pancakeswap-exchange/factory-v2, and basically just says "this is a uniswap v2 clone" - so you'll want to read the uniswap docs as well. – James Moore Nov 27 '21 at 01:10
2 Answers
pancakeswap currently doesn't have APIs or sdk as Uniswap sdk. Best solution is connect to binance smart chain and connect to the pancakeswap router contract through web3.
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
)
This is the function for swap tokens. You can refer the uniswap doc to get more information.

- 166
- 1
- 6
-
Documentation is listed [here](https://docs.pancakeswap.finance/code/smart-contracts/pancakeswap-exchange/v2/router-v2) and source code is listed [here](https://github.com/pancakeswap/pancake-smart-contracts/blob/d8f55093a43a7e8913f7730cfff3589a46f5c014/projects/exchange-protocol/contracts/PancakeRouter.sol#L262) – Alvov1 Jan 22 '23 at 20:53
There's an API which allows you to find the best DEX on different blockchains, including BSC, and allows you to specify the DEX you want by excluding all of the other ones.
Here's a link to the API specification: https://0x.org/docs/api
An example you could type into the browser is: https://bsc.api.0x.org/swap/v1/quote?buyToken=BUSD&sellToken=BNB&sellAmount=1000000000000000000&excludedSources=BakerySwap,Belt,DODO,DODO_V2,Ellipsis,Mooniswap,MultiHop,Nerve,SushiSwap,Smoothy,ApeSwap,CafeSwap,CheeseSwap,JulSwap,LiquidityProvider
This URL converts 1 BNB into BUSD on only Pancakeswap V1 and Pancakeswap V2.
You can also integrate this into a smart contract.
Hope this helps :D

- 74
- 2
- 4
-
3I don't understand how it works. Where is the private key of my wallet to do the swap? – user2983041 May 17 '21 at 08:46
-
3You don't need your private key to do a swap. If you give your key to anyone, that person can take your coins. DO NOT GIVE YOUR PRIVATE KEYS TO ANYONE OR ANY APPLICATION. Where your private key is depends on the wallet you are using. What wallet are you using? – Thierryonre May 17 '21 at 15:19
-
I don't use wallet software. In my project I directly swap using my private key and web3.js. If that api does not use my private key, I do not understand how it knows what my wallet is. I have read the doc and it is not clear to me. Does this api allow to do a token exchange without using a software wallet such as metamask? Does that api need to confirm transactions by a person one by one in the wallet? Be cause "swapExactTokensForTokens" need your private key and you can swap tokens with no wallet confirmation. – user2983041 May 17 '21 at 15:58
-
To be honest, I'm still working through this API. I believe it can generate a request which you can send through web3.js for the swap to occur. Where you specify your wallet would most likely be in the web3.js code where you send the request. – Thierryonre May 19 '21 at 13:34
-
How could you integrate that in a smart contract ? I thought the only way to access information from outside a smart contract was by using an Oracle... – KotlinIsland Jul 22 '21 at 10:59
-