# Unable to add new post using API

**URL:** <https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861>\
**Category:** Product 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:** 1\
**Showing post:** 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.

---

_[View the full topic](https://meta.discourse.org/t/unable-to-add-new-post-using-api/331861)._
