# Posting a new topic via API

**URL:** https://meta.discourse.org/t/posting-a-new-topic-via-api/93539
**Category:** Development
**Tags:** rest-api
**Created:** [July 31, 2018, 5:44am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539 "2018-07-31T05:44:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![vavalomi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vavalomi/32/105717_2.png) [@vavalomi](https://meta.discourse.org/u/vavalomi)
#### Post date: [July 31, 2018, 5:44am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/1 "2018-07-31T05:44:56Z")

</div>

Hello,

I cannot seem to make posting work and unfortunately couldn’t find much neither in api docs, nor on the forum -

What type of encoding/cleaning (if any) do I have to do for the title and raw post body strings? I’m strangely getting 404 when posting on /posts.json with {api\_username, api\_key, title, raw, created\_at, achetype, skip\_validations} fields with form-data header (trying with python but postman gives the same). Any pointers to what am I doing wrong?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [July 31, 2018, 5:52am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/2 "2018-07-31T05:52:51Z")

</div>

Standard www-form encoding is all. Make sure you’re sending either (api\_username, api\_key) or (Cookie, X-CSRF-Token).

> [@Reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576):
>
> Discourse is backed by a complete JSON api. Anything you can do on the site you can also do using the JSON api. The API is documented at [docs.discourse.org](https://docs.discourse.org). You can also use the [discourse\_api](https://github.com/discourse/discourse_api) Ruby gem as a client library. However, not every endpoint is documented. To determine how to do something with the JSON API here are some steps you can follow. Example: recategorize a topic. Go to a topic and start editing a category: Open Chrome dev tools, switch to the Network tab, select …

---

<div class="post-metadata">

### Author: ![vavalomi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vavalomi/32/105717_2.png) [@vavalomi](https://meta.discourse.org/u/vavalomi)
#### Post date: [July 31, 2018, 6:05am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/3 "2018-07-31T06:05:47Z")

</div>

But am I supposed to receive 404 in any case? I do include api\_username and api \_key in the form…

Update: if I send the same request under admin user / master api\_key it goes through, but I get 404 if I submit under another user but still with master api\_key, bug or my mistake?

---

<div class="post-metadata">

### Author: ![marcozambi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/marcozambi/32/119505_2.png) [@marcozambi](https://meta.discourse.org/u/marcozambi)
#### Post date: [July 31, 2018, 3:49pm UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/4 "2018-07-31T15:49:28Z")

</div>

Can you provide more info and maybe a code snippet of your posting attempt?

---

<div class="post-metadata">

### Author: ![iajrz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iajrz/32/106158_2.png) [@iajrz](https://meta.discourse.org/u/iajrz)
#### Post date: [January 4, 2019, 8:57pm UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/5 "2019-01-04T20:57:15Z")

</div>

Hello - I don’t want to create a new topic because I seem to be having the exact same issue; I’m using the latest `discourse_api` gem, and I have generated a master API key; I’m creating posts on behalf of existing users (fetching content they put elsewhere).

A normal call

```plaintext
c.create_topic category: category, title: new_title,
                   raw: new_content, api_username: username

```

Works on some users, and not on others; I get a `{"status"=>404, "error"=>"Not Found"}` response.

Only differences I can tell are:

- The user for whom I can create is in staff (others aren’t)
- The user for whom I can create has a user name identical to the name, and is lowercase (others don’t and aren’t)

Furthermore, if I go to the web interface, create the post, and transfer ownership, it works. So far I’m letting the automaton do its job with the ones that can be created, and give me the text to manually create the ones that can’t. But at over 1000 posts that won’t be created automatically, I despair a little 😅

EDIT: verified name situation is not the issue… It _may_ be related to the staff thing.

EDITEDIT: I worked around this by creating in a user’s name, then changing ownership to another user. The mere fact that this is possible on the same API key makes me lean towards the 404 behavior being a bug.

---

<div class="post-metadata">

### Author: ![Neeraj\_Saini](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neeraj_saini/32/129927_2.png) [@Neeraj\_Saini](https://meta.discourse.org/u/Neeraj_Saini)
#### Post date: [August 21, 2020, 9:50pm UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/6 "2020-08-21T21:50:20Z")

</div>

did you figure this out?

---

<div class="post-metadata">

### Author: ![Tarun\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tarun_sharma/32/483195_2.png) [@Tarun\_Sharma](https://meta.discourse.org/u/Tarun_Sharma)
#### Post date: [January 29, 2025, 7:12am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/7 "2025-01-29T07:12:16Z")

</div>

How to send an image in the create post api. Because in docs in api request there is no key mention for image.  
[https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM](https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM)

```plaintext
{
  "title": "string",
  "raw": "string",
  "topic_id": 0,
  "category": 0,
  "target_recipients": "blake,sam",
  "target_usernames": "string",
  "archetype": "private_message",
  "created_at": "string",
  "reply_to_post_number": 0,
  "embed_url": "string",
  "external_id": "string"
}

```

---

<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: [January 29, 2025, 11:11am UTC](https://meta.discourse.org/t/posting-a-new-topic-via-api/93539/8 "2025-01-29T11:11:25Z")

</div>

You must use the `uploads` endpoint: [https://docs.discourse.org/#tag/Uploads](https://docs.discourse.org/#tag/Uploads)  
Then, use the returned `short_url` value to insert it in your post.
