Get only top level posts in topic via discourse API

I would like to be able to get the 20 most recent posts that are top level on a topic without having to recursively re-fetch and filter post replies until I have 20. Is there a filter for this and I have just missed it somewhere in the api?

Right now it just looks like the stream array in response includes post replies by post number. So, if I have 4 posts, [1,2,3,4] and post a reply to post 1, the stream will now be [1,2,3,4,5] and the only way I can tell that 5 is a reply to 1 is by examining the post itself, finding “reply_to_post_number”=>1 and filtering it out. Am I missing something?

1 Like

That is correct. There is not a filter that can be added to the topic endpoint to get what you are after. However we do have the

which you could use to write a sql query to get what you are after. You can then save that query and hit the api endpoint for the saved query and pass it a topic_id as a param.

2 Likes

There isn’t a real way to get what you actually want out of this, because the metadata is messy, and people are messy and don’t really care about your analytics too much :wink:

2 Likes

Thanks, I will give this a shot.

We now have the filter_top_level_replies=true query parameter that will handle this for you.

Here is an example of it here on meta https://meta.discourse.org/t/linkify-words-in-post/82193.json?filter_top_level_replies=true