# Can't update a topic's body by API

**URL:** https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640
**Category:** Development
**Tags:** rest-api
**Created:** [May 11, 2023, 12:05pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640 "2023-05-11T12:05:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dotila\_Li](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dotila_li/32/303426_2.png) [@Dotila\_Li](https://meta.discourse.org/u/Dotila_Li)
#### Post date: [May 11, 2023, 12:05pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640/1 "2023-05-11T12:05:50Z")

</div>

I did follow the [docs](https://docs.discourse.org/#tag/Topics/operation/updateTopic) and， however, it only update the topic’s title, without updating the body.

My code (Javascript):

```js
async function updateDiscourseTopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/-/${topic_id}.json`;

  const payload = {
    title: 'session_1',
    raw: "new body(doesn't apply)"
  }

  const response = await fetch(url, {
    method: 'PUT',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload)
  });

}

```

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [May 11, 2023, 1:01pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640/2 "2023-05-11T13:01:55Z")

</div>

just a guess from looking at the code, but perhaps it may be because you are using `topic_id` instead of `post_id`? because first post is the topic in Discourse, more or less?

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [May 11, 2023, 3:31pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640/3 "2023-05-11T15:31:52Z")

</div>

@Lilly’s right.

A _topic_ is defined by attributes like title, category, tags, etc.

But the content belongs to a _post_.

If you want to update the title and the content, you need 2 requests.

1. PUT on the topic

2. PUT on the first post. You can find the post’s ID in the HTML code of the page. Look for the `data-post-id` attribute in `<article>`.  
Example:

---

<div class="post-metadata">

### Author: ![Dotila\_Li](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dotila_li/32/303426_2.png) [@Dotila\_Li](https://meta.discourse.org/u/Dotila_Li)
#### Post date: [May 11, 2023, 4:11pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640/4 "2023-05-11T16:11:39Z")

</div>

Wow, you are right, it works 💖

And I figured out how to retrieve the ID of the first post in a topic using the Discourse API:

```js
async function getFirstPostIdFromOnetopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/${topic_id}/posts.json`;

  const response = await fetch(url, {
    method: 'GET',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    }
  });
  const data = await response.json();
  const firstPostId = data.post_stream.posts[0].id;
  
  return firstPostId;
}

```

Thank you and @Lilly 😇

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 10, 2023, 4:12pm UTC](https://meta.discourse.org/t/cant-update-a-topics-body-by-api/264640/5 "2023-06-10T16:12:19Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
