uTalk

Official forum for Utopia Community

You are not logged in.

#1 2021-11-12 18:24:52

youtube
Member
Registered: 2021-09-20
Posts: 28

Utopia API + Node.js example

Good day to all. I am a regular user of utopia. In the process of using utopia, I came up with ideas that I would like to implement using the utopia API. I don't have a lot of programming experience, and I ran into certain problems when interacting with the API. Therefore, I will post here an example of code in node.js that can help people like me in the future.

This is a simple example, how I called the getMiningBlocks() function, and then display the last data.


const got = require('got');

got.post('http://127.0.0.1:20010/api/1.0/', {
    json: {

        method: 'getMiningBlocks',
        token: 'yourtoken'

    } , responseType: 'json'
}).then( response => {

    console.log(response.body['result'][response.body['result'].length - 1]);

    }
)

data

{
  dateTime: '2021-11-12T17:35:04.417Z',
  id: 69490,
  involvedIn: true,
  involvedInCount: 15,
  numberMiners: 28716,
  price: 0.003343083
}


In got.post, pay attention to which port you are referring to (I use 20010) and which protocol (I use http).

You can even try it on Windows.
Installing a headless version of utopia on linux doesn't even bother you.
In order to try enough for you, just open Utopia, go to the API menu and enable it. Of course, before that you need to download the nodejs and npm package "got"

Thank you for your attention, I hope I can help someone

If you have any questions, my public key is:
F89309DC5E7E153F641BD2027B1541498415231F41B9262D2007C0769BF86C1F

Offline

#2 2021-11-15 05:06:54

germanmsh
Member
Registered: 2021-11-13
Posts: 2

Re: Utopia API + Node.js example

I will complete the topic by accessing the API with the .NET platform in the C # language. The console program displays the result of the getProfileStatus function (one of the APIs), based on which it is easy to implement others.

using System;
using System.Text;
using System.Net;

WebRequest request = WebRequest.Create("http://127.0.0.1:20001/api/1.0");
request.Method = "POST";
String mes1 = @"{""method"": ""getProfileStatus"", ""token"": ""7632261400226A4AEBF76EA4AD69CFC8""}";
Byte[] b = Encoding.UTF8.GetBytes(mes1);
string s = null;
Encoding enc8 = Encoding.UTF8;
s = enc8.GetString(b);
Console.WriteLine(s);
request.ContentLength = b.Length;
request.ContentType = "application/json; charset=utf-8";
Stream dataStream = request.GetRequestStream();
dataStream.Write(b, 0, b.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
using (dataStream = response.GetResponseStream())
{
    StreamReader reader = new StreamReader(dataStream);
    string str = reader.ReadToEnd();
    Console.WriteLine(str);
}
response.Close();
Console.ReadKey();

You just need to replace the port and token with your own. good luck writing programs!

Offline

Board footer

Powered by FluxBB