Getting Post ID with Topic ID - API Help Needed

סיכום מבוסס בינה מלאכותית

The discussion revolves around retrieving the first post ID from a topic using the Discourse API. P16 asks how to get the first post ID given a topic ID to enable liking the first post directly from the topic preview. fefrei explains that a GET request to /t/{topic_id}.json returns the first post ID via post_stream.posts[0].id, and that liking can be done via a POST to /post_actions with the post ID and post_action_type_id=2. JQ331 adds a clarification that the response includes a data key, so the correct syntax is response.data.post_stream.posts[0].id. Additionally, doserved raises a subfolder routing issue, which neil confirms was fixed in a recent update here.

Hi Everyone,

We are setting up a new Discourse forum and needed some help with the API.

One of the changes we are making is that we show the first post in a topic on the topic page (like Topic Preview). Since most posts will be visual, we show the image, the title, the replies and likes, etc of the topic to the user. Something like this:

We wanted the user to be able to like the first post in the topic by clicking on the heart icon from the main page. How can we get the first post id of a topic from the main page since we only have the topic id?

Also, once we get the post id, we need to send that and the post action type id as 2 to http://DiscourseURL/post_actions
Is that correct?

Thanks!

Using this topic as an example: You topic ID is 44239.

A GET to https://meta.discourse.org/t/44239.json returns an object where post_stream.posts[0].id = 194190.

I then liked your post by POSTing to https://meta.discourse.org/post_actions with id=194190&post_action_type_id=2&flag_topic=false. This request needs to be authenticated by an API key or cookies.

Thank you so much Felix. That helps a lot :slight_smile:

A tangential question.

I am using subfolders.

I can type https://mysite,com/subfolder/t/xx and get to the topic fine.

If I type https://mysite.com/subfolder/p/yyyy it translates to https://mysite,com/t/title/xx/yyyy but does not include the subfolder so it doesn’t display.

Thoughts?

Possibly a bug cc @neil

I fixed that today.

Thanks! I’ll try it out after the plugins I’m using are fixed to work with the latest version of Discourse.

This is helpful. One thing I wanted to note, don’t forget the term “data” when parsing the response. The form of the response is: {"data":{"post_stream":{"posts":[{"id":...

So if you are looking to get the id of the topic post (ie, the first post in the topic), the syntax would be response.data.post_stream.posts[0].id