You need an endpoint on API side which provides you with the data, and get that data using fetch. Then you need to pass the data through the function you have to write that will generate code similar to above.

That will be for getting data once. You can set that on interval, but best solution would be using, which needs to be implemented on the server side and in your js code.

Related Query