Is there REST resource for uploading and assigning Attachments to Posts?

Hi,

  1. My attachments are pdf, images, etc.

  2. I did not find an Attachment parameter in the Post resource.

  3. I thought about using the Upload resource and specifying type=“composer”. But I do not see a way to associate the Upload to a Post. Or, is this the wrong resource to upload Attachments?

TIA.

1 Like

There is no “attachments” field on the Post API directly. To attach an upload to a post you would need to:

  1. Upload the file (get the short URL).
  • First upload your attachment using the /uploads.json endpoint:
  • Endpoint: POST /uploads.json
  • The response will contain a short_url like upload://abcDEF123.pdf.
  1. Edit/create the post to include that short URL, so Discourse recognizes and links the upload to that post. To assign the uploaded attachment to a Post, you need to include the upload short URL in the post’s raw content using Markdown. For an attachment, you can use this format:
[filename.pdf|attachment](upload://abcDEF123.pdf)

Or for images:

![alt text](upload://abcDEF123.png)

This can be done when creating a post (using POST /posts.json ) or editing an existing post (using PUT /posts/{id}.json ), by putting the markdown link in the raw field.

For reference, there are also a couple existing topics about this process here:

1 Like