Steem API Guide - How To Get Recent Posts(getDiscussionsByCreated) and Load More

in #steemjs8 years ago (edited)

Steem platform's lack of documentation frustrates third-party developers.
I've spent couple of days to figure this out. As I am posting the secret, you don't have to waste too much time on it.

I'll talk about Steem API get_discussions_by_created The real usage currently hidden in the document.

Language: JavaScript
Dependency: Steem.js

How to get recent 10 posts in a tag

<script src="./steem.min.js"></script>
<script>
var oldestPermLink = ""
steem.api.getDiscussionsByCreated({"tag": "kr", "limit": 10}, function(err, result) {
    if (err === null) {
        var i, len = result.length;
        for (i = 0; i < len; i++) {
            var discussion = result[i];
            console.log(i, discussion);
        }
    } else {
        console.log(err);
    }
});
</script> 

limit range: 1 ~ 100 (a number greater than 100 will be reject by steemd)

So far, it's easy as it is "documented" but below is the secret

How to get the next 10 posts in the tag

<script src="./steem.min.js"></script>
<script>
steem.api.getDiscussionsByCreated({"tag": "kr", "limit": 10}, function(err, result) {
    if (err === null) {
        var i, len = result.length;
        for (i = 0; i < len; i++) {
            var discussion = result[i];
            console.log(i, discussion);
            // Store the last permlink and author
            if (i == len - 1) {
                window.permlink = discussion.permlink;
                window.author = discussion.author;
            }
        }
    } else {
        console.log(err);
    }
});

....

//In the load more button click event,
steem.api.getDiscussionsByCreated({"tag": "kr", "limit": 10, "start_permlink": window.permlink, "start_author": window.author}, function(err, result) {
    // now result has the next 10 posts
}
</script> 

Secret

The secret is start_permlink and start_author.
You have to give both of these "optional parameter"
That will give you the posts list starting from that post and older posts as many as limit
So there will be a redundant post. So you may have to implement your own logic to ignore the redundant post.
At least, the secret is now revealed...

Sort:  

Thanks, this was useful.

Thats and awesome secret my friend, thanks :)

Heh good stuff man.

Hi @morning,

Thank you for sharing this info!

I'm looking for an API that allows me to create content (POSTs). I have been reading the documentation and it seems that we can only retrieve information or just create a comments or up-vote.

I wonder if you could point me to some API that allows creating POST in steemit.com.

Thanks,

@realskilled

comment is post if you pass empty string '' at parentAuthor and parentPermlink

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

Thanks... was lost for so long. The documentation could have make it better.

wow, worthy post........ just a question to my dear friend , how can i write window.author = discussion.author in node.js, and by using getDiscussionsByCreated can it shown all the post content that store in steemit

Thank you. Just what I was looking for!

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 64572.94
ETH 2630.79
USDT 1.00
SBD 2.82