---
description: swap tokens
---

# swap

## /bsc/pcs/swap

<mark style="color:green;">`POST`</mark> `https://cryptotalk-public-gateway-byeii62k.de.gateway.dev/bsc/pcs/swap`

Support all swap requests : `token to token, token to BNB, BNB to token.`\
Sample code is given below.\
\
Request body\
**1. addr :** Wallet address\
**2. frm\_tkn\_addr :** Token to be swapped from\
**3. to\_tkn\_addr:** Token to be swapped to\
**4. amt\_float:** input amount as floating point (keep max decimals to 7)\
**5. slippage:** desired slippage (0.01 for 1%, 0.03 for 3% and so on)\
**6. path:**  Path that should be taken to swap tokens (ex: BNB > BUSD > USDC)\
**7. min\_output\_amt:** Minimum acceptable returned amount\
**8. isInputFixed:** send true if input amount is fixed, send false is output amount is fixed

#### Query Parameters

| Name | Type   | Description |
| ---- | ------ | ----------- |
| key  | string | API Key     |

#### Request Body

| Name   | Type   | Description                                                                                                                                                                                                                                                                                                                                                                           |
| ------ | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| params | object | <p><code>{</code> <br><code>"to_tkn_addr" "0xbb4cdb9..c095c",</code><br><code>"frm_tkn_addr" : "0xeeee..eeeeeeee", "amt_float" : 0.1,</code><br><code>"min_output_amt" : 0.1,</code><br><code>"gas" : "10",</code><br><code>"isInputFixed" : true,</code><br><code>"slippage" : 0.01,</code><br><code>"addr" : "0xaaaa..4",</code> <br><code>"path" : []</code><br><code>}</code></p> |

{% tabs %}
{% tab title="200 " %}
```
{
  isError: false,
  errorMsg: 'NA',
  methodName: 'swapExactTokensForTokens',
  rawTx: {
    from: '0x5585d5365f37671eecc0fdcac5a06bd1d176408e',
    value: '0x0',
    to: '0x05ff2b0db69458a0750badebc4f9e13add608c7f',
    data: '0x38ed173900000000000000000000000000000000000000000000000000b03006698f2000000000000000000000000000000000000000000000000000c21f642a306e780000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000005585d5365f37671eecc0fdcac5a06bd1d176408e00000000000000000000000000000000000000000000000000000000605059200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000a184088a740c695e156f91f5cc086a06bb78b827000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d560000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82',
    nonce: '0xf24',
    gasLimit: '0x7a120',
    gasPrice: '0x2540be400'
  }
}
```
{% endtab %}
{% endtabs %}

## Sample code to swap 0.05 AUTO token to BUSD through pancake swap

```javascript
## Simple Javascript example on how to swap tokens via pancake swap ##
async function swap(){
  const fetch         = require('node-fetch');
  const _             = require('lodash');
  const amount        = 0.05;
  const TOKEN_LIST    = "https://cryptotalk-public-gateway-byeii62k.de.gateway.dev/bsc/tokens-list";
  const QUOTE_URL     = "https://cryptotalk-public-gateway-byeii62k.de.gateway.dev/bsc/pcs/quote";
  const SWAP_URL      = "https://cryptotalk-public-gateway-byeii62k.de.gateway.dev/bsc/pcs/swap";
  const BROADCAST_URL = "https://cryptotalk-public-gateway-byeii62k.de.gateway.dev/bsc/broadcast";
  const API_KEY       = "AIzaSyCngkz41JSdBMlr4iKqAMLiTMHD9TfQvuc";

    async function localStylePost(URL, body){
        let txn_response = await fetch(URL, { method : 'post', body : JSON.stringify({ params : body}), headers: { 'Content-Type': 'application/json'}});
        return txn_response.json();    
    }

    async function localStyleGet(URL, query){
        let txn_response = await fetch(URL + query);
        return txn_response.json();
    }
    
  console.log("step - 1 : Your wallet address\n");
  const myAddr  = "0x5585d5365f37671eecc0fdcac5a06bd1d176408e"; //account.address;

  console.log("step - 2 : Fetching BEP20 token list address\n");
  let token_list_object = await localStyleGet(TOKEN_LIST + "?key=" + API_KEY, "");

  console.log("step - 3 : Get Quote\n");
  let quote_req_body = {
            frm_tkn_addr  : _.find(token_list_object.data, ['symbol', 'AUTO']).address,
            to_tkn_addr   : _.find(token_list_object.data, ['symbol', 'BUSD']).address,
            amt_float     : amount
        }
  let quote_response = await localStylePost(QUOTE_URL + "?key=" + API_KEY, quote_req_body);
      quote_response = quote_response.data.bestRate;
  console.log(quote_response);

  console.log("step - 4 : Swap request body preparation\n");
  let swap_req_body = {
      "frm_tkn_addr"      : _.find(token_list_object.data, ['symbol', 'AUTO']).address,
      "to_tkn_addr"       : _.find(token_list_object.data, ['symbol', 'BUSD']).address,
      "amt_float"         : amount,
      "min_output_amt"    : quote_response.quote,
      "gas" 		          : "10",
      "isInputFixed" 	    : true,
      "slippage"		      : 0.01, 
      "addr"              : myAddr,
      "path"			        : quote_response.route_addr
  }
  
  console.log("step - 4: creating a swap request\n");
  let swap_req  = await localStylePost(SWAP_URL + "?key=" + API_KEY, swap_req_body);
  console.log(swap_req);
}
```
