How to create a custom_json with node.js and dsteem

in #tutorial6 years ago

This is a mini tutorial on how you can post a custom_json using node.js and the dsteem JavaScript library. This tutorial assumes you have an understanding of node.js and terminal usage. It's not difficult to do, just Google it. 😁


Install dsteem

First we need to install dsteem, so in your terminal type the following:

npm install dsteem

Let's code

Now create a new file called customjson.js


First we need to require the dsteem library and connect to a Steem node, I have picked @anyx node, but you can pick any of the others that are available.

const dsteem = require('dsteem')
const client = new dsteem.Client('https://anyx.io')

Next, we need to declare our account name and active key.

const account = 'pjau'
const active_key = 'put_your_active_key_here'

Next, let's create a function for sending our data to Steem. This function will take in an id string (this is a maximum of 32 characters) and also our JS object data. First we need to get our key from our active key and then we can broadcast the json to Steem.

const custom_json = (my_id, my_data) => {
    const key = dsteem.PrivateKey.fromString(active_key)

    client.broadcast.json({
        required_auths: [],
        required_posting_auths: [account],
        id: my_id,
        json: JSON.stringify(my_data),
    }, key).then(
        result => { console.log(result) },
        error => { console.error(error) }
    )
}

Now let's call our function with the data we wish to make into a custom_json:

custom_json('my_test', {my_name: 'pjau', my_message: 'This is a message'})

Here is the complete script

const dsteem = require('dsteem')
const client = new dsteem.Client('https://anyx.io')

const account = 'pjau'
const active_key = 'put_your_active_key_here'

const custom_json = (my_id, my_data) => {
    const key = dsteem.PrivateKey.fromString(active_key)

    client.broadcast.json({
        required_auths: [],
        required_posting_auths: [account],
        id: my_id,
        json: JSON.stringify(my_data),
    }, key).then(
        result => { console.log(result) },
        error => { console.error(error) }
    )
}

custom_json('my_test', {my_name: 'pjau', my_message: 'This is a message'})

Run the script

You can now run it by typing the following in your terminal:

node customjson.js

You should get a json response in the terminal which looks similar to the following:

response.PNG


After a minute it will show up on steemd as follows:

runit.PNG


And that's it, you have posted a custom_json, how neeto is that. 😉


Vote for me as Witness

https://steemit.com/~witnesses: type in pjau and click vote

Vote @pjau via Steemconnect

THANKS FOR LISTENING

Sort:  

Hi @pjau!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 5.754 which ranks you at #417 across all Steem accounts.
Your rank has not changed in the last three days.

In our last Algorithmic Curation Round, consisting of 215 contributions, your post is ranked at #161.

Evaluation of your UA score:
  • You've built up a nice network.
  • The readers appreciate your great work!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server