# How to get a previous version/revision of a post through the API

**URL:** https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164
**Category:** Development
**Tags:** rest-api
**Created:** [March 1, 2017, 12:53pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164 "2017-03-01T12:53:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [March 1, 2017, 12:53pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/1 "2017-03-01T12:53:08Z")

</div>

Looking at the API, I don’t find any way to get a previous version/revision of a post. Am I missing something?

Also, I would like to get the diff between 2 versions. Is this computed client-side only or store in the DB?

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [March 1, 2017, 4:57pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/2 "2017-03-01T16:57:07Z")

</div>

Call `GET` method to the URL `/posts/POSTID/revisions/VERSION.json`

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [March 1, 2017, 5:47pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/3 "2017-03-01T17:47:35Z")

</div>

Excellent, thanks @vinothkannans.

It works great for VERSION \>=2, but it doesn’t for VERSION === 1.

```plaintext
Discourse::InvalidParameters in PostsController#revisions

revision

Extracted source (around line #481):
    post_id = params[:id] || params[:post_id]
    revision = params[:revision].to_i
    raise Discourse::InvalidParameters.new(:revision) if revision < 2

    post_revision = PostRevision.find_by(post_id: post_id, number: revision)
    raise Discourse::NotFound unless post_revision

Rails.root: /home/sylvain/discourse

Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:481:in `find_post_revision_from_params'
app/controllers/posts_controller.rb:293:in `revisions'
config/initializers/100-quiet_logger.rb:17:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/missing_avatars.rb:21:in `call'
lib/middleware/turbo_dev.rb:33:in `call'

Request

Parameters:

{"post_id"=>"16",
 "revision"=>"1",
 "format"=>"json"}

```

Any idea why? Any solution to get the first revision of a post?

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [March 1, 2017, 6:24pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/4 "2017-03-01T18:24:45Z")

</div>

`PostRevision` will not have exact post of the specific version. It will have the post with modifications like below

`<del>Hi, </del>This is example post <ins>revision</ins>.`  
~~Hi,~~ This is example post revision.

And it will start from number 2 only. Because revision 2 is the modifications of the post version 1 from version 2.

If you need the exact post instead then you may have to find an another way. Explore the code 👍.

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [March 1, 2017, 9:20pm UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/5 "2017-03-01T21:20:52Z")

</div>

Of course @vinothkannans, thanks again, I should have had a closer look.

For those landing here, notice that Discourse doesn’t really get you revision number n. Instead, it returns html formatted blocks, ready to be displayed in the various tabs of the Discourse “history” window. This is how it looks:

```json
{  
   "created_at":"2017-03-01T17:41:38.441Z",
   "post_id":15,
   "first_revision":2,
   "previous_revision":2,
   "current_revision":3,
   "next_revision":null,
   "last_revision":3,
   "current_version":3,
   "version_count":3,
   [...]
   "body_changes":{  
      "inline":"<div class=\"inline-diff\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa<br class=\"diff-ins\"><ins>bbbbbbb</ins></p></div>",
      "side_by_side":"<div class=\"span8\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa</p></div><div class=\"span8 offset1\"><p><ul>\n<li><p>Why should people use this category? What is it for?</p></li>\n<li><p>How exactly is this different than the other categories we already have?</p></li>\n<li><p>What should topics in this category generally contain?</p></li>\n<li><p>Do we need this category? Can we merge with another category, or subcategory?</p></li>\n</ul><p>aaaaaaa<br class=\"diff-ins\"><ins>bbbbbbb</ins></p></div>",
      "side_by_side_markdown":"<table class=\"markdown\"><tr><td>- Why should people use this category? What is it for?\n\n- How exactly is this different than the other categories we already have?\n\n- What should topics in this category generally contain?\n\n- Do we need this category? Can we merge with another category, or subcategory?\n\n</td><td>- Why should people use this category? What is it for?\n\n- How exactly is this different than the other categories we already have?\n\n- What should topics in this category generally contain?\n\n- Do we need this category? Can we merge with another category, or subcategory?\n\n</td></tr><tr><td class=\"diff-del\">aaaaaaa</td><td class=\"diff-ins\">aaaaaaa<ins>\nbbbbbbb</ins></td></tr></table>"
   },
   "title_changes":{  
      "inline":"<div class=\"inline-diff\"><div>About the subcategory category</div></div>",
      "side_by_side":"<div class=\"span8\"><div>About the subcategory category</div></div><div class=\"span8 offset1\"><div>About the subcategory category</div></div>"
   },
   "user_changes":null,
   "wiki":false,
   "can_edit":true
}

```

---

<div class="post-metadata">

### Author: ![paresy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paresy/32/96471_2.png) [@paresy](https://meta.discourse.org/u/paresy)
#### Post date: [February 21, 2021, 8:44am UTC](https://meta.discourse.org/t/how-to-get-a-previous-version-revision-of-a-post-through-the-api/58164/6 "2021-02-21T08:44:21Z")

</div>

Landed here to find the “first” revision of a Post. My solution is taken from here: [discourse/app/serializers/post\_revision\_serializer.rb at a0bbc346cb5d5b89d1a3efdfa89869349a8b067f · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/a0bbc346cb5d5b89d1a3efdfa89869349a8b067f/app/serializers/post_revision_serializer.rb#L193)

```
 PostRevision.where(post_id: 46747).order(:number).to_a

```

This will give you all raw post revisions including the first.
