1inch swap API with Metamask


I’m trying to build one basic swap, directly using 1inch API. Language I’m using is javascript.
I want to prompt the Metamask approval for the user, so that they can give the required permission to it for swap to happen.

How can this be achieved? I understand we have web3.eth.sendTransaction(data) , but since I’m using Moralis across my application, wanted to know is there any method exists for this?

URL: https://api.1inch.io/v4.0/56/swap?fromTokenAddress=0x55d398326f99059ff775485246999027b3197955&toTokenAddress=0xaf3889ba617ac973b358513d9031778d2bc783df&amount=1000000000000000000&fromAddress=0x06125e1457f833E29D5aebAaCdF8eED8A6Febaee&slippage=8


    "fromToken": {
        "symbol": "USDT",
        "name": "Tether USD",
        "decimals": 18,
        "address": "0x55d398326f99059ff775485246999027b3197955",
        "logoURI": "https://tokens.1inch.io/0xdac17f958d2ee523a2206206994597c13d831ec7.png"
    "toToken": {
        "address": "0xaf3889ba617ac973b358513d9031778d2bc783df",
        "decimals": 9,
        "symbol": "SHIBIC",
        "name": "Shiba Inu Classic",
        "logoURI": "https://etherscan.io/images/main/empty-token.png",
        "isCustom": true
    "toTokenAmount": "1502695685638969",
    "fromTokenAmount": "1000000000000000000",
    "protocols": [
                    "name": "PANCAKESWAP_V2",
                    "part": 100,
                    "fromTokenAddress": "0x55d398326f99059ff775485246999027b3197955",
                    "toTokenAddress": "0xaf3889ba617ac973b358513d9031778d2bc783df"
    "tx": {
        "from": "0x06125e1457f833E29D5aebAaCdF8eED8A6Febaee",
        "to": "0x1111111254fb6c44bac0bed2854e76f90643097d",
        "data": "0x7c0252000000000000000000000000003790c9b5a9b9d9aa1c69140a5f01a57c9b868e1e0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000018000000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000af3889ba617ac973b358513d9031778d2bc783df0000000000000000000000005f59de0d25fc7b40d797df79f91c2bd55aa5f26100000000000000000000000006125e1457f833e29d5aebaacdf8eed8a6febaee0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000004e95bb724e90b00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a4b757fed60000000000000000000000005f59de0d25fc7b40d797df79f91c2bd55aa5f26100000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000af3889ba617ac973b358513d9031778d2bc783df0000000000000000002625a01111111254fb6c44bac0bed2854e76f90643097d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cfee7c08",
        "value": "0",
        "gas": 1089735,
        "gasPrice": "15000000000"

there is 1Inch plugin that you can install on your server and use: https://moralis.io/plugins/1inch/

I kind of used it, but seems there is now way to pass fees and referal address here, hence had to move to 1inch API directly.

you could try to make a transaction with these details

Thanks @cryptokid

But I want to make this call from frontend, without going to backend server.
So it should be done using javascript directly.
Any Moralis method available for this?

you may need to use web3 directly if you want to set to set gas and gasPrice, if not you could try with Moralis.transfer

1 Like

this may help someone:

let txData = [{
    "from": "0x06125e1457f833E29D5aebAaCdF8eED8A6FebXXX",
    "to": "0x1111111254fb6c44bac0bed2854e76f906430XXX",
    "data": "<DATA>",
    "value": "0",
    "gas": "1089735",
    "gasPrice": "15000000000"

  // window.ethereum
  try {
    const transactionHash = await ethereum.request({
      method: 'eth_sendTransaction',
      params: txData
  } catch (error) {
1 Like

Working on the same problem. At the moment I’m coding:

  const signTransaction = async (unsignedTx, title) => {
    if (unsignedTx.tx) {
      console.log('Tx to sign:', unsignedTx.tx);
      return await window.ethereum
          method: 'eth_sendTransaction',
          params: unsignedTx.tx,
        .catch((error) => {
          console.log('Tx signature error:', error);
    } else {
      console.log('Skipping Tx signature for ' + title);

And I’m getting:

Not sure what to make of it. Perhaps I need to get addresses first? If anybody has any better ideas I’m all ears.

1 Like

its because your not providing a “from” address as the error says. now your unsigned tx is an object but im not sure what other items you have in it. you will need the address tho. so to do this pass in the params as the entire unsigned tx object maybe, but you need to specify the drom address. consider the example below

      method: 'eth_sendTransaction',
      params: [
          from: accounts[0],
          to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
          value: '0x29a2241af62c0000',
          gasPrice: '0x09184e72a000',
          gas: '0x2710',

something like this. so just modify your unsigned tx object to have a similar form to this example above. theres also mych more params you can include but this is just to give you and idea

alternatively you could use (if your using metamask) the following format for the from address

from: ethereum.selectedAddress

Well…ok. If you’ll read the code I posted above, more specifically the console log of the params input I posted with it…my params input is and always has been an object with the following fields, including a “from” address. I’ve just blacked out the address because…well…it’s my address and shouldn’t be posted.

Screen Shot 2022-03-02 at 1.07.54 PM

Something else is wrong with the syntax. I am most definitely giving them a from address. But somehow it’s not hearing it. Something’s wrong with the syntax and I’m not seeing it. There’s not much for documentation as to what the syntax should be either. So I’m down to crude trial and error to figure out what it wants and how it wants it.

1 Like

if this was me i would debug by hardcoding the values of the Tx in the params option. use the format from my post above with the values you have in your object and this should work. if you can get it to work by hardcoding so you can see the exact format then its much easier to backtrack and get your unsignedtx object var to work. also i notice your passging in unsignedTx.tx. why .yx, is this object nested or something? if its not a nested object then what about passing just unsignedTx. but do try hardcoding first will make it easier to figure out what the problem with your one is

Per some actually useful web3 documentation the request params field is expecting an array of objects even though there’s typically only one object in the array. I was sending it an object. That was the usual stupid tiny detail I wasn’t seeing. I’ve bracketed the [object] as an array and It’s working now. Thanks.

1 Like

no wories great you got it working