[HIVE Patched] SteemJS Full Tutorial - All The Functions - All The Abilities

in SteemDevs4 years ago (edited)

![apidoc.png]()

---

#### Repository

[https://www.npmjs.com/package/steem-js-patched](https://www.npmjs.com/package/steem-js-patched)

# All of this tutorial is patched for Hive, so it works for Hive and steem!

#### All of the examples and results is from the old tutorial but still same for hive!

---

# The Tutorial

So first I want to say that this post is to clarify the steemjs Lack of information documentation, it's not understandable to know what I need to do, let's take an example:

![image.png]()

there is literally no explanation... I can't understand from here what to do...

***A quick notifier, I will go on the most useful functions and not all over the documentation [but I will go on most of the documentation functions]***

so let's get started with the tutorial!

### - How to use it on HTML?

we will use the CDN, go to your HTML file and add this script -

```

<script src="//cdn.steemjs.com/lib/latest/steem.min.js></script>

```

if you want go to [CDN](//cdn.steemjs.com/lib/latest/steem.min.js) and right click "Save Page As" `steem.min.js`

![image.png]()

![image.png]()

and move it to your js folder and use the script like that

```

<script src="javascript/steem.min.js"></script>

```

### - How to use it on Node.JS

first, go to your nodejs project folder,

open the console and install `steem`

`npm install steem --save`

![image.png]()

now open your javascript file and add steem API,

```

var steem = require('steem');

```

![image.png]()

### - Config - Change RPC Node, address prefix and Chain ID

- set the rpc node

```

steem.api.setOptions({ url: 'https://api.hive.blog' });

```

you can use any RPC node, if you're using sites like golos, weku, whaleshares etc.

you can set the API RPC node to this sites as well.

## To use Hive node you need to use one of the hive nodes.

- set the address prefix

```

steem.config.set('address_prefix','STM');

```

for example on other sites like weku it will be `WKU` or on WhaleShares `WLS` etc.

## STM is the same for HIVE

- set chain id

```

steem.config.set('chain_id','782a3039b478c839e4cb0c941ff4eaeb7df40bdd68bd441afd444b9da763de12');

```

this is the chain id of golos, this is to use other steem clones like golos, weku, whaleshares and more...

it's not really used on steem, so know that.

## The Hive blockchain follows the same chaid_id as steem

### - Starting With The API - Tags

get trending tags

```

steem.api.getTrendingTags(afterTag, limit, function(err, result) {

console.log(err, result);

});

```

so let's find what does it do,

`afterTag` - is the tag it's starting from, for example, if I start from steemit which is the popular one it will give from there and lower,

or for example, I will start with photography it will give from there and lower, the popularity made by the total payouts, comments, and top posts.

example for usage:

```

steem.api.getTrendingTags('hive', 5, function(err, result) {

console.log(err, result);

});

```

the tag I'm using - `hive`, limit - `5`

results -

![image.png]()

as you can see it sorting it by the `trending`,

hive trending - 1146.021 HBD

hive-132971 trending - 1123.536 HBD

etc.

from what it looks the trending is calculating from the top posts, net votes and total payouts with an exclusive calculation of steem and we get from that the popular tags.

- Get discussions by [Trending / Hot / Created]

so everything is the same, just change the function

```

steem.api.getDiscussionsByTrending({tag: 'photography', limit: 1}, function(err, result) {

console.log(err, result);

});

steem.api.getDiscussionsByCreated({tag: 'photography', limit: 1}, function(err, result) {

console.log(err, result);

});

steem.api.getDiscussionsByHot({tag: 'photography', limit: 1}, function(err, result) {

console.log(err, result);

});

```

all is the same just getting the post from trending or created or hot feed.

that's all the change, the query stays the same

so first, results

![image.png]()

so you need to choose a tag to get the posts from, I used `photography` and a limit for the number of posts,

I chose 1 because I don't need anymore.

and from there you can do what you want to do!

- getState

```

steem.api.getState('/@lonelywolf', function(err, result) {

console.log(err, result);

});

```

so from here, you can get literally any information,

from the post feed to the main witness and more...

just get the link you want to check, for example - `/@lonelywolf/transfers` and check it.

### Global Functions

- getConfig

get the full config of the hive blockchain

```

steem.api.getConfig(function(err, result) {

console.log(err, result);

});

```

result -

![image.png]()

- global properties

```

steem.api.getDynamicGlobalProperties(function(err, result) {

console.log(err, result);

});

```

this function gives you the global properties of the blockchain, for example -

the supply, the latest block number, the current witness working on the block, the total vests and more...

result -

![image.png]()

- chain properties

```

steem.api.getChainProperties(function(err, result) {

console.log(err, result);

});

```

this function gives the creation fee per account,

the maximum block size,

the sbd interest rate,

the budget & decay.

result -

![image.png]()

- get medain history

```

steem.api.getCurrentMedianHistoryPrice(function(err, result) {

console.log(err, result);

});

```

this function gives the average/value of the hbd/hive rate.

I didn't use this function so much so I can't tell 100% surly what it gives at the result.

result -

![image.png]()

- get hardfork version

```

steem.api.getHardforkVersion(function(err, result) {

console.log(err, result);

});

```

gives the version of hive

result - `0.23.0`

- get reward fund

```

steem.api.getRewardFund('post', function(err, result) {

console.log(err, result);

});

```

this function gives the reward fund of the post, I didn't found anything else, it's giving the reward fund of posts

it's useful for calculation of steem power (vesting to SP) for example and much more

result -

![image.png]()

- get vesting delegations

```

steem.api.getVestingDelegations(`ocdb`, ``, 50, function(err, result) {

console.log(err, result);

});

```

result -

![image.png]()

### Accounts

- get account information

```

steem.api.getAccounts(['lonelywolf'], function(err, result) {

console.log(err, result);

});

```

so because this function get multiple accounts we need to give it a json object, so put `['account']` and not `'account'`

you can add as many accounts as you want.

it gives all of the information about the account, from name to last votes, mana reputation and more...

this is the most useful function on hive API.

- get account count

```

steem.api.getAccountCount(function(err, result) {

console.log(err, result);

});

```

this function gives the count of all of the accounts on steem

result - `1,376,722` [raw result - `1376722`]

- get account history

```

//steemjs documentation

steem.api.getAccountHistory(account, from, limit, function(err, result) {

console.log(err, result);

});

//usage version

steem.api.getAccountHistory('lonelywolf', -1, 100, function(err, result) {

console.log(err, result);

});

```

account - your account name (or the one you want to see in), from (the first transaction), limit (the limit of the transactions)

the data of the transaction stored in the `op` object.

### Market

- get orders

```

steem.api.getOrderBook(1, function(err, result) {

console.log(err, result);

});

```

this function gives the latest orders on the market by limit `1 - limit` change 1 to any limit you want.

- get open orders

```

steem.api.getOpenOrders('lonelywolf', function(err, result) {

console.log(err, result);

});

```

this function gives the open orders on the market by account name,

### Votes

- get active votes

```

//doc version

steem.api.getActiveVotes(account ,permlink, function(err, result) {

console.log(err, result);

});

//usage version

steem.api.getActiveVotes('lonelywolf' , 'my-only-problem-with-hive-and-how-it-compares-to-steem', function(err, result) {

console.log(err, result);

});

```

account - account name

permlink - the permlink of the post, comes after your account name (@lonelywolf) like that - `@account_name_here/bla-bla-blaaa`

this function gives the active votes of the post you entered.

result -

![image.png]()

- get account votes

```

// doc version

steem.api.getAccountVotes(voter, function(err, result) {

console.log(err, result);

});

//usage verison

steem.api.getAccountVotes('lonelywolf', function(err, result) {

console.log(err, result);

});

```

this function gets the votes of a specific account

### Content

- get content

```

//doc version

steem.api.getContent(author, permlink, function(err, result) {

console.log(err, result);

});

//usage version

steem.api.getContent('lonelywolf', 'my-only-problem-with-hive-and-how-it-compares-to-steem', function(err, result) {

console.log(err, result);

});

```

author - the account author, the creator of the post/comment

permlink - the permlink of the post / comment [ example in the code / above ]

this function gives the post details, from the author to the title, body and much more...

result -

![image.png]()

- get post replies (comments)

```

//doc version

steem.api.getContentReplies(author, permlink, function(err, result) {

console.log(err, result);

});

//usage version

steem.api.getContentReplies('lonelywolf', 'my-only-problem-with-hive-and-how-it-compares-to-steem', function(err, result) {

console.log(err, result);

});

```

this function gives all of the replies(comments) of the post with all of the details (voters, payout, etc.).

### Witness

- get witness

```

steem.api.getWitnessByAccount('someguy123', function(err, result) {

console.log(err, result);

});

```

use by account name, this function gives the witness information.

- get witness count

```

steem.api.getWitnessCount(function(err, result) {

console.log(err, result);

});

```

get the amount of witnesess on steemit

result - `14,890` [Raw result - `14890`]

### Broadcast

- delegate vesting shares

```

steem.broadcast.delegateVestingShares(wif, delegator, delegatee, vesting_shares, function(err, result) {

console.log(err, result);

});

```

`wif` - the active wif key, used to send a delegation, use the market, send transfers and more...

`delegator` - the account that sent the delegation

`delegatee` - the account that gets the delegation

`vesting_shares` - the number of vests you send

- witness vote

```

steem.broadcast.accountWitnessVote(wif, account, witness, approve, function(err, result) {

console.log(err, result);

});

```

`wif` - the active wif key

`account` - the account that is voting (your account)

`witness` - the witness account (the one that gets the vote)

`approve` - the state of the approve (1 to approve, 0 to not approve) [1 = vote, 0 = unvote]

- comment (post & comment)

```

steem.broadcast.comment(wif, parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata, function(err, result) {

console.log(err, result);

});

```

`wif` - your posting wif key

`parentAuthor` - the "parent" author, if it's a comment the parentAuthor will be the author of the post, if it's a post leave blank

`parentPermlink` - same as `parentAuthor` just with permlink.

`author` - the author (the account creating the post / comment)

`permlink` - the permlink of the comment / post (create new permlink, you can use a random function that will make a custom number or something like that for the permlink)

`title` - the title of the comment / post

`jsonMetadata` - the json metdata, most of the times is the same :

```

JSON.stringify({

tags: "hive",

app: 'hive/0.1',

users: ["lonelywolf"],

format: "markdown"

}),

```

`tags` - the tags of the post / comment

`app` - the application used, leave steemit/0.1 if you're not a "company" or something like that

'users' - the user made the post/comment, just leave your own name, you can remove this line if you don't want it (it's not necessary)

`format` - the format of the post/comment (leave markdown)

- custom Json

```

steem.broadcast.customJson(wif, requiredAuths, requiredPostingAuths, id, json, function(err, result) {

console.log(err, result);

});

```

this function used most for following and stuff like that

`wif` - the posting wif key

`requiredAuths` - which auths needed, for following, for example, nothing is required (use for the active key, if needed to transfer or something like that)

`requiredPostingAuths` - the posting auths needed, for following is `follower`

`id` - the identifier of the customJson, for follow we use `follow`

`json` - the json object for the function

follow example using customJson -

```

/** Follow an user */

var follower = 'lonelywolf'; // Your username

var following = 'someguy123'; // User to follow

var json = JSON.stringify(

['follow', {

follower: follower,

following: following,

what: ['blog']

}]

);

steem.broadcast.customJson(

postingWif,

[], // Required_auths

[follower], // Required Posting Auths

'follow', // Id

json, //

function(err, result) {

console.log(err, result);

}

);

```

follower - the follower account (your account)

following - the follow after account (the other account)

what - the identifier of the follower (which is a blog)

then we're using the customjson function to follow the account.

- delete comment

```

steem.broadcast.deleteComment(wif, author, permlink, function(err, result) {

console.log(err, result);

});

```

`wif` - the account posting wif key

`author` - the author of the post/comment, the creator

`permlink` - the permlink of the post / comment

## It only works if there is no votes on the comment.

- vote

```

steem.broadcast.vote(wif, voter, author, permlink, weight, function(err, result) {

console.log(err, result);

});

```

`wif` - the posting wif key

`voter` - the voter account, the account that is voting

`author` - the author of the post (the post creator)

`permlink` - the permlink of the post

`weight` - the weight of the vote, max is `10000`(10k), so to make it easier just do that: `10*100` (10 = percentage), max = 100%

- vest to steem

```

//doc usage

var steemPower = steem.formatter.vestToSteem(vestingShares, totalVestingShares, totalVestingFundSteem);

//real usage

steem.api.getDynamicGlobalProperties((err, result) => {

var steemPower = steem.formatter.vestToSteem(500000, result.total_vesting_shares, result.total_vesting_fund_steem);

console.log(steemPower);

});

```

this function gets the vests and the global properties and converts it to Steem power.

---

Guys, it was really painful to get all of the non-clarified functions to be clarified to use if you struggling with anything let me know in the comments and I will help you and update the post.

if you have any suggestions to add to this post comment down, I will add it to the post!

hope I helped you!

# Have a great day!