# Latest posts API pagination

**URL:** https://meta.discourse.org/t/latest-posts-api-pagination/88589
**Category:** Development
**Tags:** rest-api
**Created:** [5월 28, 2018, 11:53오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589 "2018-05-28T11:53:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![dawid.urbanski](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dawid.urbanski/32/97378_2.png) [@dawid.urbanski](https://meta.discourse.org/u/dawid.urbanski)
#### Post date: [5월 28, 2018, 11:53오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/1 "2018-05-28T11:53:58Z")

</div>

Hello everyone!

I’m getting latest posts from api using this endpoint: [https://community.learnaboutgmp.com/posts.json](https://community.learnaboutgmp.com/posts.json)

How can I get more results from latest posts? Is this possible?

I tried [https://community.learnaboutgmp.com/posts.json?page=2](https://community.learnaboutgmp.com/posts.json?page=2) but it’s not working.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [5월 28, 2018, 2:07오후 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/2 "2018-05-28T14:07:07Z")

</div>

When I figured that out before I followed the instructions in the “reverse engineering” topic. I thought that I’d done it in [GitHub - pfaffman/discourse-downloader: Download a Discourse topic for offline analysis · GitHub](https://github.com/pfaffman/discourse-Downloader), but I don’t see it there.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 30, 2018, 12:49오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/3 "2018-05-30T00:49:24Z")

</div>

You might have to hit `/latest.json?page=2` instead of `/posts.json` and filter out the posts from the result.

[https://meta.discourse.org/latest.json?page=2](https://meta.discourse.org/latest.json?page=2)

EDIT: Ohh you want latest posts, not latest topics. Considering `/posts` by itself doesn’t have a UI, it probably is missing pagination, I’m sure a PR could be created for this. But to get what you want right now you could still parse the `/latest.json` endpoint and parse out the topic\_id and the post number and fetch the post with `/t/<topic_id>/<post_number>`, but yea I can see why pagination on the `/posts` endpoint would be helpful.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [5월 30, 2018, 1:20오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/4 "2018-05-30T01:20:44Z")

</div>

> [@dawid.urbanski](#):
>
> How can I get more results from latest posts? Is this possible?

Okay, now that I’ve looked at [the controller](https://github.com/discourse/discourse/blob/master/app/controllers/posts_controller.rb#L57) for `/posts`:

```
def latest
  params.permit(:before)
  ...

```

there is a “before” parameter, not a “page” parameter, but using it like a “page” is a bit different because it filters out private posts and such. But I think roughly you can use it like this:

- hit `/posts.json` and get the first post id (currently it is 22000)
- subtract 50 (that endpoint only shows at most 50 at a time)
- hit `/posts.json?before=21950`
- repeat

Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

---

<div class="post-metadata">

### Author: ![dawid.urbanski](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dawid.urbanski/32/97378_2.png) [@dawid.urbanski](https://meta.discourse.org/u/dawid.urbanski)
#### Post date: [5월 30, 2018, 6:59오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/5 "2018-05-30T06:59:39Z")

</div>

Thanks guys! I’ll play with it.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [6월 2, 2018, 2:05오전 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/6 "2018-06-02T02:05:15Z")

</div>

> [@blake](#):
>
> Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

as the API designer: it works this way to make things easier on the Discourse server; if the server had to do a bunch of work to always fill up 50 posts anything using the API might start being a perf problem.

Basically it forces the consumers to be “good neighbors”, in a weird sense of the term.

---

<div class="post-metadata">

### Author: ![vsoch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vsoch/32/124967_2.png) [@vsoch](https://meta.discourse.org/u/vsoch)
#### Post date: [9월 13, 2019, 5:18오후 UTC](https://meta.discourse.org/t/latest-posts-api-pagination/88589/7 "2019-09-13T17:18:15Z")

</div>

Question about using the API (which I’m doing through the discourse\_api gem, cool!) - it’s related to pagination (I would also want to use the latest posts endpoint) but for my current example below I’ll provide something from users, since I started working on it first.

I am wanting to get all activity for a user (topics and replies is good) so I’m trying this:

```ruby
vsoch = client.user_topics_and_replies("vsoch")

```

And then I consistently get a list of 30, regardless if the user obviously has more. I tried editing the source code to add a page variable:

```ruby
# frozen_string_literal: true
module DiscourseApi
  module API
    module UserActions
      def user_replies(username, page=1)
        params = { "username": username, "filter": '5', page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end

      def user_topics_and_replies(username, page=1)
        params = { "username": username, "filter": "4,5", page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end
    end
  end
end

```

but it doesn’t seem to do anything - it’s always the same list of 30 regardless of the page number. Is it possible to get more than 30? Am I trying the wrong thing? Any help would be greatly appreciated - if I can figure out how to do it I’d very happily put in a PR so others can too 🙂
