API: Getting all posts in a topic

I’m working on a script that will evaluate participation in a discussion and produce a number based on how many messages they wrote, likes, and replies and such that will become students’ grade for “participating” in a discussion.

/t/blah/TOPIC_ID.json returns only 20 posts. Is there a way to get all of them, or will I need to do a request for all of them?

I looked a little at what gets passed to poll, but it wasn’t immediately apparent that I could somehow pass it something like a range or number of posts that I wanted.


Maybe the easiest way is to get the data I want from the data explorer plugin.

Now I’m thinking that it would be cool to write a plugin that showed people’s scores next to their profile pic in their posts in that topic.

When you GET /t/blah/TOPIC_ID.json the output will also contain a stream array that has all the id’s for the topic.

You then can call: /t/blah/TOPIC_ID/posts.json?post_ids[] and pass in all the ids from the stream array.

1 Like

Thanks! And the problem with data explorer is that there is no way (that I can see quickly) to pass in the topic_id that I want.

Also its probably a good idea to still break up fetching all the posts into multiple requests rather than 1 big one. So if you have a topic with 100 posts. You should break it up into 5 smaller requests fetching 20 posts at a time.

1 Like

Hi @blake @pfaffman

I don’t know why i’m getting a response from /t/blah/TOPIC_ID.json with only the last 10 posts.
I see the chunk_size parameter (in response) set to 10, but i don’t know why is like that or where can i change this value.

Thank you very much

1 Like

I once wrote code that downloaded all of the posts in a topic.

I don’t know if the code still works, but it looks like how many posts you get is harder to predict than you might think. You pass the first post-id to control what posts you get.

See https://github.com/pfaffman/discourse-downloader/blob/master/discourse-downloader#L69


The best way to download all the posts to a topic via the api is to mimic what Discourse is doing in the web browser so please checkout How to reverse engineer the Discourse API for details. Basically go the the topic in the browser open up your dev tools and look at the xhr requests as you scroll through the topic.

Here are the steps to download all the posts in the topic via the api:

  1. Hit /t/-/{id}.json. This will contain a ‘posts_stream’ hash that contains a ‘posts’ array and a ‘stream’ array. The ‘posts’ array will give you the first 20 posts.

  2. Now you need to loop through the ‘stream’ array which gives you all of the post ids in the topic. Remove the first 20 post ids from the stream (otherwise you are re-downloading them for no reason).

  3. In chunks of 20 pass in all the post_ids to /t/{id}/posts.json like this:


Thanks @blake and @pfaffman for your soon response.

I agree with @blake steps for get all posts in a topic.

According with step 1
I just wanted to know if there are any parameter (maybe in the header of request) to set the chunk_size in /t/blah/TOPIC_ID.json request, because if i made a request from POSTMAN i get the first 20 posts as described previosly, but if i made the request using my web app using angular, i just get the first 10.

So i think there are something in the request that change the response from discourse server.

I use this request because from her i can get the post stream and the first 20 posts in one request. So i take her like my first request base to get all post in a topic.

I know this question is not critical, i can figure out to get my solution using multiple request. I am just curious to know why

For some reason your angular app is triggering slow_chunk_size.




So that might be something to look into.

There is not a chunk_size parameter you can set, but if you pass in print=true like /t/-/{id}.json?print=true it will set the chunk size to 1000.


Thank you so much :ok_hand:

That is the trick. I am running my app (actually is an ionic v3 app) from chrome dev tools on android device and i alwasy get the first 10. When i swtich to browser mode, i get the 20.

1 Like

this saved my life !


print parameter just saved me :star_struck:


The ?print=true command is great, indeed! It seems however that there is a rate limit for ?print=true commands of five calls per hour. Is there a way to make more API calls per hour?

1 Like

This solution does not work on “mega topics” Do you have any solution for those?

If not, you’ll need to make multiple requests to retrieve the rest. How many does it return?

While ?print=true works (as well as &page=2), there appears to be more Rate limit than without print=true. I wonder how many requests I can do, and considered safe, not to hit status 422.

I am trying to read around 9000 posts, so for 20 posts at once, it will either be very slow, or get rate-limited…

I recommend that you write the code expecting to get rate limited.

It’s in my UserScript. Language highlighting for typescript doesn’t work; and js works strangely. Have to be javascript. Both ts and typescript work in StackOverflow, however.

interface IPost {
  id: number
  username: string
  post_number: number
  cooked: string

interface ITopicResponse {
  actions_summary: {}[]
  archetype: string
  fancy_title: string
  title: string
  post_stream: {
    posts: IPost[]
    stream: number[]
  posts_count: number
  reply_count: number

export async function jsonFetch<T>(url: string): Promise<T | null> {
  const r = await fetch(url)
  if (r.ok) {
    const json = await r.json()
    if (!json.errors) {
      return json

  logger('error', r)
  return null

export async function fetchAll(urlBase: string) {
  const r0 = await jsonFetch<ITopicResponse>(urlBase + '.json?print=true')
  if (!r0) return []

  const posts: IPost[] = r0.post_stream.posts
  let page = 2
  while (posts.length < r0.posts_count) {
    const r = await jsonFetch<ITopicResponse>(
      urlBase + '.json?print=true&page=' + page++
    if (!r || !r.post_stream.posts.length) {
    await new Promise((resolve) => setTimeout(resolve, 1000))

  return posts


In a ts-node script using Axios,

(node:1102374) UnhandledPromiseRejectionWarning: Error: Request failed with status code 422

If I wait long enough, like 10 minutes, it will fail at page 2; but if I just repeat now, I can’t load any URL.

And without print works OK.

Actually, I got it to work by avoiding print=true.

export async function fetchAll(urlBase: string) {
  const r0 = await jsonFetch<ITopicResponse>(urlBase + '.json');
  if (!r0) return [];

  const stream = r0.post_stream.stream || [];
  const chunks: number[][] = [];
  while (stream.length) {
    chunks.push(stream.splice(0, 300));

  const posts: IPost[] = [];
  let isContinue = true;
  while (chunks.length && isContinue) {
    const rs = await Promise.all(
        .splice(0, 10)
        .map((ids) =>
            urlBase +
              '/posts.json?' +
              ids.map((id) => `post_ids[]=${id}`).join('&'),
    ).then((rs) =>
      rs.map((r) => {
        if (!r) {
          isContinue = false;
          return [];
        return r.post_stream.posts;

    rs.map((r) => {

    if (chunks.length) {
      await new Promise((r) => setTimeout(r, 1000));

  if (!isContinue) {
      `Total posts: ${r0.posts_count} != real count: ${posts.length}, due to Rate Limit?`,

  return posts;