[SOLVED] GetPrice API Help

Hi,

Im very new to all of this, ive got a HTML and CSS site for my crypto currency and im trying to use the GetPrice API (https://docs.moralis.io/reference/gettokenprice)

Its all working fine on that page when i run it i can see my token and price however im a noob so need help displaying this over on my html site, any idea how to do it if someone could provide a code snippet it would be greatly appreciated.

thanks
K

There is a JavaScript code sample on the getTokenPrice page you can use with axios (or just use fetch like in the example below).

After you get the response back from the API, you can set some element to use the value from this response.

// some element in your HTML where you want to display something
<input id="tokenprice" />

...

// in your JavaScript code, swap out address f
async function fetchPrice() {
 fetch(
    'https://deep-index.moralis.io/api/v2/erc20/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2/price?chain=eth',
    {
      headers: { accept: 'application/json', 'X-API-Key': 'test' },
    }
  )
    .then((response) => response.json())
    .then((data) => {
      console.log('data', data); // find out what data you can use

      document.getElementById('tokenprice').value = data.usdPrice; // set value of this input, can use .innerHTML for other elements like <p>
    });
}

fetchPrice(); // call somewhere

It would be a good idea to follow a course like this one to learn the fundamentals if you want to be able to fetch data or use APIs like Moralis.

1 Like

Hi,

Awesome got it working however when using BSC token its saying no liquuidity pool found however on the Moralis get price page its working fine and code is pretty much same.

Here is what im using

// some element in your HTML where you want to display something// in your JavaScript code, swap out address f
async function fetchPrice() {
  fetch(
    'https://deep-index.moralis.io/api/v2/erc20/CONTRACT/price',
    {
  params: {chain: 'bsc'},
  headers: {accept: 'application/json', 'X-API-Key': 'test'}
    }
  )
    .then((response) => response.json())
    .then((data) => {
      console.log('data', data); // find out what data you can use

      document.getElementById('tokenprice').value = data.usdPrice; // set value of this input, can use .innerHTML for other elements like <p>
    });
}

fetchPrice(); // call somewhere

My guess is that is still trying to fetch data from ETH even know ive typed BSC on chain, this is how the example on the moralis page is too and works fine there.

thanks again

I have edited the code example, I missed using the chain parameter correctly for fetch (from the axios example).

Also make sure to use your own API key instead of test.

Yeah got my own API, thanks so much im going to give it a whirl now!