Discourse API Documentation

Discourse API

Please view the Discourse API Documentation site for detailed info:

http://docs.discourse.org

The Content-Type can be set to “application/x-www-form-urlencoded”, “multipart/form-data” or “application/json”.

Here is an example POST request via cURL:

curl -X POST "http://127.0.0.1:3000/categories" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: 714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19" \
-H "Api-Username: discourse1" \
-F "name=89853c20-4409-e91a-a8ea-f6cdff96aaaa" \
-F "color=49d9e9" \
-F "text_color=f0fcfd"

Here is an example of what the ReDoc site looks like:

Postman

In addition to the docs site, you can also import the postman JSON files into
postman to test making real API calls and see the JSON output.

Postman files:

Start by importing the environment JSON file. After you import it be sure to
change the base_url, api_username, and api_key values to match your
discourse instance.

Now you can import the collection JSON file and start making API requests.

Here is a demo:

Consuming the API

You can consume the API using cURL commands, but we recommend using the discourse_api gem so that you can use Ruby.

Reverse engineering API endpoints

69 Likes
API: Can I authenticate without putting the key in the URL?
Discourse Add Group API
Online User API?
How can I update trust level via SSO site
Deeply integrating Discourse into website
Do we have any API or way to get latest post details for a particular Topic in discourse?
Use discourse_post_id to get single topic data from API
Create group via API
Update current user fields through JavaScript?
API call to watch categories and tags
Quick Messages Plugin
Using the Discourse API Ruby Gem
Vague error from clicking on a tag link on this site
Work Around to posting
Discourse dislike Api?
A list of server-side functions for weekend/novice Discourse developers?
Api documentation on creating private forums
Seeding a new forum by creating users and content
How to implement Discourse with an already built Rails project
Using the API, can't PUT to topic unless slug is on the URL
Using the API, can't PUT to topic unless slug is on the URL
Incoming webhooks (Incoming into Discourse)
JSON API, get topics sorted by created_at
Managing site customization via API
Discourse API Documentation Using RAML
Is it possible to mount a playable video game into a discourse forum?
How to upload bulk post
How to update profile information via API
What determines the order of frequent poster icons in the topic list?
What is Discourse Blog built with?
What is correct aproach to update topic via ajax?
How to Create a Script to "Bulk Remove" from a Group?
Working with different needs in raw/cooked content
Creating a new topic on behalf of a user
How to get notifications via the API
API: Can I authenticate without putting the key in the URL?
Possible for admins to create bookmarks for users?
Locations Plugin
Dev Category sidebar
Shopify Integration
Which CMS for Discourse
Blocking user via API, returns 404
Interact with discourse from Python?
Display username when login in banner
Creating alternate user directory
How can I update trust level via SSO site
Questions about support with hosted Discourse
Synchronizing Changes for SSO users
[Announce] Search & replace / batch process Discourse posts
API Error: You are not permitted to view the requested resource
Migrating from Jive Clearspace to Discourse
Can an admin change a user's email address?
How to get uploaded image url using the Discourse API?
Can't send PM via API (HTTPUnprocessableEntity 422)
How to contribute to the Discourse API Docs
Swagger, WADL or similar for the REST API?

For the most up-to-date URL endpoints (without parameter details), check this file: discourse/routes.rb at master · discourse/discourse · GitHub

14 Likes

I’m trying to fetch all the replies from a given thread but for some reason, some of the replies doesn’t show up on the results. Here’s what I’m doing:

curl http://my-forum.com/t/thread-name/numeric-thread-id.json

Am I missing something? Can someone help me?

That gets you a block of 20 posts. For the next posts, try:

http://my-forum.com/t/-/:thread_id/20.json

With the correct thread name, of course, otherwise you’re using two requests for every page.

7 Likes

Thanks @riking! That’s exactly what I was looking for.

Cheers!

Hello everyone, my first post in this forum.

I’m trying to get all posts in a topic with:
/t/id.json

This works fine, except for that I only recieve the first 20 posts. I see the info about the rest of the posts under “stream”, but the posts themselves are missing. I can’t find a setting for this anywhere either. Any idea what I’m doing wrong? :slight_smile:

Running v1.6.0.beta2 +78 btw.

1 Like

You will need to pass in all the id’s from the “stream” as url parameters to this url:

/t/#{topic_id}/posts.json?

like:

http://localhost:3000/t/26/posts.json?post_ids%5B%5D=212&post_ids%5B%5D=211&post_ids%5B%5D=210&post_ids%5B%5D=209&post_ids%5B%5D=208&post_ids%5B%5D=207&post_ids%5B%5D=206&post_ids%5B%5D=205&post_ids%5B%5D=204&post_ids%5B%5D=203&post_ids%5B%5D=202&post_ids%5B%5D=201&post_ids%5B%5D=200&post_ids%5B%5D=199&post_ids%5B%5D=198&post_ids%5B%5D=197&post_ids%5B%5D=196&post_ids%5B%5D=195&post_ids%5B%5D=194&post_ids%5B%5D=193&_=1462882841434

I would still make sure you break them up in chunks of 20.

Also I recommend using the ruby discourse_api gem:

1 Like

Thank you!

So currently there’s no way to request a specific “page” not counting replies?

What I would like to do is get the last 5 posts, plus replies if there are any. But in “stream” i can’t see if a post is a reply or not, correct?

Can’t use the ruby gem I’m afraid, since our project is written in php. I’m stuck with curl. :slight_smile:

Nope. No paging, but the highest post_id’s on a topic should give you the latest ones (well I think there is a way to get paging, but it will return html, not json. Just browse your site with javascript disabled).

In the “stream” no, but in the json data for each post there are these fields:

"reply_count":0,
"reply_to_post_number":103,

which should help you get the reply info

If we really would like to have native support for this in the api, is there someone we could convince? Or hire?

My problem with this, if I’ve got this right, is that I don’t know which posts that are replies until I’ve retrieved them fully. So if I have a topic with 2000 posts I would have to get all 2000 posts to look for replies to the first post. Well, there is a reply_count so I could be lucky and stop the search when all is found, but worst case scenario is that I have to look through all posts. So it would be really nice to have an api call for this instead, right? :slight_smile:

Maybe, but that isn’t a super expensive call to do. You could probably fetch posts in batches of 100+ (or 2000 :scream:)

but you could probably just [quote=“mathias.kallberg, post:146, topic:22706”]
get the last 5 posts
[/quote]

and still get what you are looking for.

You could post a topic in the marketplace category here on meta and see if someone is willing to do it.

A quick question: Is there anything you’re not allowed to do via the Master API Key?

My college was trying to create users via the api, and didn’t succeed. After a bit if a struggle he’d created a second api key, for the system user, and got it working, but I wonder if that had anything to do with it? :slight_smile:

nope, the master API Key can do all the things.

What was the username he was using with the Master API Key? Even if you are using the Master API Key you still need to pass in a username with it.

His own username I think. So maybe his user didn’t have enough power, could that be it?

If he was using the Master API key though I don’t think it would matter, but if he was using just the api key for his user it would matter.

How can i get the original post? For example, its markdown version.
If i request https://meta.discourse.org/t/using-the-rest-api/21699.json i get only cooked versions of all posts.

Does adding ?include_raw=1 work for you? eg.

https://meta.discourse.org/t/discourse-api-documentation/22706/161.json?include_raw=1

Discourse API Documentation

3 Likes

Okay, we finally have updated API Docs! Also, contributions are welcome.

10 Likes

Glad to finally see real API docs, thanks for that.

I have been having issues importing the Swagger/OpenAPI definition into Postman. I’ve also asked the Postman community about this. Has anyone done this successfully?

1 Like

I’m not sure about importing the swagger files, but you should be able to import the postman environment and collection json files into postman.