Published 7 December 2018

Using of Credits API in JS (demo).

Requirements

Requirements

Any OS (Windows 10 is used for the example)

Download Credits API files

Create a folder to which you are going to copy the JS files, create an empty html file, for example, index.html.

Create application

Create application

Put changes to index.html

Add Credits API Script Files

   <script src="thrift.js"></script>

   <script src="API.js"></script>

   <script src="Signature.js"></script>

   <script src="api_types.js"></script>

Add connection to Thrift and request balance

   <title>Credits API JS Demo</title>

   <script>

       function run()

       {

           var url = "http://localhost:8081";

           var transport = new Thrift.Transport(url);

           console.log("debug step 1");

           var protocol = new Thrift.Protocol(transport);

           console.log("debug step 2");

           var client = new APIClient(protocol);

           console.log("debug step 3");

           var PublicKeyFrom = "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b";

           var BytePublicKeyFrom = Base58.decode(PublicKeyFrom);

           console.log("debug step 4");

           var obj = client.BalanceGet(BytePublicKeyFrom, 1);

           console.log("debug step 5");

           console.log(obj);

           console.log("debug step 6");

           document.getElementById('pc-id').innerText = PublicKeyFrom;

           document.getElementById('bl-id').innerText = 'Integral: ' + obj.amount.integral + ', Fraction: ' + obj.amount.fraction;

       }

       function ready() {

           run();

       }

       document.addEventListener("DOMContentLoaded", ready);

   </script>

Check the performance. Call the GetBalance Method.

Check the performance. Call the GetBalance Method.

As the result of the program we see the requested balance:
page view:

View via developer tool:

GitHub link

11 Useful article

Contents