# Unable to add new post using API

**URL:** https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861
**Category:** Support
**Created:** [October 21, 2024, 10:12am UTC](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861 "2024-10-21T10:12:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mryogi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mryogi/32/457437_2.png) [@mryogi](https://meta.discourse.org/u/mryogi)
#### Post date: [October 21, 2024, 10:12am UTC](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861/1 "2024-10-21T10:12:46Z")

</div>

Hi I am unable to add new post on topic in my discourse forum using API, my code:

```plaintext
function postComment(topicId, comment) {
  const url = `${DISCOURSE_API_URL}/t/${topicId}/posts.json`; // Ensure this URL is correct
  Logger.log(`Posting to URL: ${url}`);
  const headers = {
    "Api-Key": DISCOURSE_API_KEY,
    "Api-Username": DISCOURSE_API_USERNAME
  };

  const payload = {
    post: {
      topic_id: topicId,
      raw: comment
    },
  };

  const options = {
    method: "post",
    contentType: "application/json",
    headers: headers,
    payload: JSON.stringify(payload),
    muteHttpExceptions: true, // To capture the full error response
  };

  try {
    const response = UrlFetchApp.fetch(url, options);
    const jsonResponse = JSON.parse(response.getContentText());

    // Log the response for debugging
    Logger.log(`Response Code: ${response.getResponseCode()}`);
    Logger.log(`Response Body: ${JSON.stringify(jsonResponse, null, 2)}`);

    // Check if the posting was successful
    if (response.getResponseCode() === 200) {
      Logger.log(`Posted comment successfully: ${JSON.stringify(jsonResponse, null, 2)}`);
    } else {
      Logger.log(`Failed to post comment: ${JSON.stringify(jsonResponse, null, 2)}`);
    }

    return jsonResponse; // Return the API response
  } catch (error) {
    Logger.log(`Error posting comment: ${error}`);
    return null; // Return null on error
  }
}

```

getting this response:

```plaintext
Info
Response Code: 404
3:35:29 PM
Info
Response Body: {
  "errors": [
    "The requested URL or resource could not be found."
  ],
  "error_type": "not_found"
}

```

even though forum topic exists and I can post on it directly on forum website.

Please help.

---

<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: [October 21, 2024, 12:24pm UTC](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861/2 "2024-10-21T12:24:24Z")

</div>

404 not likely means that your api key is bad or not getting passed correctly.

Unless it means that you’re actually using the wrong path. If you [Reverse engineer the Discourse API](https://meta.discourse.org/t/reverse-engineer-the-discourse-api/20576) for a post, is that the path that’s used?

---

<div class="post-metadata">

### Author: ![mryogi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mryogi/32/457437_2.png) [@mryogi](https://meta.discourse.org/u/mryogi)
#### Post date: [October 22, 2024, 4:47am UTC](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861/3 "2024-10-22T04:47:59Z")

</div>

I am using this path: `${DISCOURSE_API_URL}/t/${topicId}/posts.json`  
where `DISCOURSE_API_URL = "https://community.xxxxxxxxxxx.com"`

---

<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: [October 23, 2024, 6:00am UTC](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861/4 "2024-10-23T06:00:41Z")

</div>

> [@mryogi](#):
>
> I am using this path: `${DISCOURSE_API_URL}/t/${topicId}/posts.json`

I think the URL for replying to a topic via the API is `/posts.json`. When creating a reply, as opposed to when you’re creating a new topic, the `topic_id` is a required param.

Details are here: [Discourse API Docs](https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM), but you can also just create a reply to a topic through your site’s UI and check the RequestURL that’s set in the “network” section of your browser’s web inspector.
