pjau avatar

How to create a custom_json with node.js and dsteem

pjau

Published: 05 Dec 2018 โ€บ Updated: 05 Dec 2018How to create a custom_json with node.js and dsteem

How to create a custom_json with node.js and dsteem

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@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

Leave How to create a custom_json with node.js and dsteem to:

Written by

I do what I do...

Read more #tutorial posts


Best Posts From pjau

We have not curated any of pjau's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From pjau