0

I'm experimenting with 0x samples. I'm not a back-end dev so I'm NOT running NodeJS. I don't want to go full blown ReactJS on it so I built this html wrapper for the example 0x code for retrieving a swap quote:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>0x Demo Code</title>
  </head>
  <body>
    <script type="module">
    import qs from 'qs';
    const params = {
      // Not all token symbols are supported. The address of the token can be used instead.
      sellToken: 'DAI',
      buyToken: 'WETH',
      // Note that the DAI token uses 18 decimal places, so `sellAmount` is `100 * 10^18`.
      sellAmount: '100000000000000000000',
    }
    export async function getQuote() {
      const response = await fetch(
        `https://api.0x.org/swap/v1/quote?${qs.stringify(params)}`
      );
      console.log(await response.json());
    }
  </script>
  <button onclick="getQuote()">Get Quote</button>
  </body>
</html>

But I can't get import qs from 'qs'; to work at all. Nor can I find any useful example of how to do this online. Thoughts?

1 Answer 1

1

You are getting that error because you need to install first qs library on the browser, which is not natively installed on it.

If you are just trying to see what the API does and what kind of response it generates, you can just paste directly the parameters without the need to use the library like this:

const url = `https://api.0x.org/swap/v1/quote?sellAmount=${params.sellAmount}&sellToken=${params.sellToken}&buyToken=${params.buyToken}`

which translates to the following code URL as follows:

https://api.0x.org/swap/v1/quote?sellAmount=100000000000000000000&sellToken=DAI&buyToken=ETH

1
  • That's really my question. How and from where do I 'first qs library on the browser'? The example is set up for NodeJS. I can't find how to do it in ReactJS. Simple I know, but so am I. May 27, 2022 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.