# Fetch All Posts from a Topic Using the API

**URL:** https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886
**Category:** Integrations
**Tags:** rest-api, how-to
**Created:** [April 6, 2023, 11:19pm UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886 "2023-04-06T23:19:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Discourse](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discourse/32/148734_2.png) [@Discourse](https://meta.discourse.org/u/Discourse)
#### Post date: [April 6, 2023, 11:19pm UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886/1 "2023-04-06T23:19:50Z")

</div>

> 📔 This is a how-to guide explaining how to fetch all posts from a topic using the Discourse API

The results returned by the Discourse API for many routes are paginated.

For example, the API endpoint for [“Get A Single Topic”](https://docs.discourse.org/#tag/Topics/operation/getTopic) - Ex: `https://examplesite/t/{id}.json` will only return 20 posts by default, even if the topic contains more than 20 posts.

Due to this behavior, there are two ways you can use the Discourse API to fetch all posts for a topic using the `.../t/{id}.json` endpoint.

# Append a Query Parameter

The easiest way fetch all posts from a topic is to add a `print=true` query parameter to the URL you are making the request to.

Ex: `https://examplesite/t/{id}.json?print=true`

When the `print=true` query parameter is added, Discourse sets the `chunk_size` for the number of posts that are returned to 1000. That means that this is a good approach to use as long as you are certain that your topics have no more than 1000 posts.

# Multiple API Requests

The other method for fetching all posts is to make multiple API requests to get all of the posts from the topic:

1. First, you would make an initial `GET` request to the `.../t/{id}.json` endpoint. 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. You can then make additional requests to the [“Get Specific Posts From a Topic”](https://docs.discourse.org/#tag/Topics/operation/getSpecificPostsFromTopic) `.../t/{id}/posts.json` endpoint, append `post_ids[]`, and pass in all the ids from the `stream` array in chunks of 20. Ex ` .../t/{id}/posts.json?post_ids[]=46&post_ids[]=47&post_ids[]=48&post_ids[]=49&post_ids[]=50&post_ids[]=51&post_ids[]=52&post_ids[]=53&post_ids[]=54&post_ids[]=55&post_ids[]=56&post_ids[]=57&post_ids[]=58&post_ids[]=59&post_ids[]=60&post_ids[]=61&post_ids[]=62&post_ids[]=63&post_ids[]=64&post_ids[]=65`

# Rate Limits

If you encounter an `Error: you have performed this action many times, please try again later` message when making multiple API calls this indicates that you are running into API key rate limits.

Discourse has a limit on the number of `print=true` requests that can be made per hour, which is controlled by the `max prints per hour per user` site setting. This setting defaults to only allow users to print 5 topics per hour. Setting this to `0` will disable printing entirely (requests with `print=true` will return a 403 error).

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/5/7/e573f24b803bfb79ba720bccefa033412da77ff2.png)

Note that the rate limit will not be applied if the requesting user is an admin. That means you can use an API key with an admin’s username (for example `system`) for the request’s `Api-Username` parameter to bypass the print rate limit.

If you are running into rate limit errors with API requests that do not contain `print=true`, we recommend adding a timeout to your API script so that you don’t exceed the rate limits. Alternatively, you can listen for `429` (too many requests) error codes and backoff on the requests when you receive that response.

For reference, the default rate limits listed below apply to our standard and business hosted plans:

> <https://github.com/discourse/discourse/blob/main/config/discourse_defaults.conf#L251-L257>

> ❕ _Self Hosted Only_ - See: [Available settings for global rate limits and throttling](https://meta.discourse.org/t/available-settings-for-global-rate-limits-and-throttling/78612) for details about adjusting Discourse API rate limits.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [April 9, 2024, 6:04am UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886/2 "2024-04-09T06:04:10Z")

</div>

Is the `?page` query param supported? It works, but in a surprising way - `?page=1` only returns the first post in the topic, so to paginate with the parameter you need to start with `https://example.com/t/slug/topicId.json`, then skip to `https://example.com/t/slug/topicId.json?page=2`, then keep going until you eventually get a `404` response.

---

<div class="post-metadata">

### Author: ![SaraDev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saradev/32/335139_2.png) [@SaraDev](https://meta.discourse.org/u/SaraDev)
#### Post date: [April 25, 2024, 10:51pm UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886/5 "2024-04-25T22:51:24Z")

</div>

Hi Simon,

Using the `?page=1` parameter with the [Get a single topic endpoint](https://docs.discourse.org/#tag/Topics/operation/getTopic) will return the first 20 posts from a topic, and each subsequent page number will return up to 20 posts.

When there are no most posts available (EX: the page is too high and not valid), you’ll get a 404 response.

If you don’t specify a page number, the code will set a page number of 1, so `?page=1` is the same as not appending an explicit page to the topic request.

If you wanted to use this method to fetch all posts from a topic, you should be able to do so, even though it is not mentioned within the [Discourse API Docs](https://docs.discourse.org/).

---

<div class="post-metadata">

### Author: ![irregular](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@irregular](https://meta.discourse.org/u/irregular)
#### Post date: [August 11, 2024, 10:48pm UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886/7 "2024-08-11T22:48:51Z")

</div>

Thanks! Working on a matrix bot to fetch post!

---

<div class="post-metadata">

### Author: ![fokx](https://avatars.discourse-cdn.com/v4/letter/f/958977/32.png) [@fokx](https://meta.discourse.org/u/fokx)
#### Post date: [December 17, 2024, 2:58am UTC](https://meta.discourse.org/t/fetch-all-posts-from-a-topic-using-the-api/260886/8 "2024-12-17T02:58:50Z")

</div>

> [@Discourse](#):
>
> You can set that setting to `0` to disable the rate limit.

I think this is wrong. Set to 0 will disable print at all. The latest description for this setting is `Maximum number of /print page impressions (set to 0 to disable printing)`.

> <https://github.com/discourse/discourse/blob/68e57190df85210f70471c67e532a6ea55b3430b/app/controllers/topics_controller.rb#L96>
