Discourse API Documentation

api

(Blake Erickson) #1

Discourse API

Please view the Discourse API Documentation site for detailed info:

http://docs.discourse.org

The Content-Type must be set to “application/x-www-form-urlencoded” or “multipart/form-data” otherwise you may receive a bad request response.

Here is an example POST request via cURL:

curl -X POST "http://127.0.0.1:3000/categories" \
-H "Content-Type: multipart/form-data;" \
-F "api_key=714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19" \
-F "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


Creating a user through RESTful Users API
Discourse API Authentication
API to create a user without sending out activation email?
How can we create post/topic in Discourse using API
Reading topics in a forum with JavaScript
API: Can I authenticate without putting the key in the URL?
Get user data remotely (API, database, cURL)
Discourse Add Group API
How can I update trust level via SSO site
Do we have any API or way to get latest post details for a particular Topic in discourse?
Deeply integrating Discourse into website
Online User API?
How can i implement discourse into a website
Quick Messages Plugin
Update current user fields through JavaScript?
Discourse and Wordpress Integration
Discourse 1.2 Extensibility Game Plan
API call to watch categories and tags
Use discourse_post_id to get single topic data from API
How can we create post/topic in Discourse using API
'Invalid access' when using API
Create group 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
Simple plugin that offers an HTTP POST endpoint to mailgun or any other mail service
Using the REST API with other languages
Working with different needs in raw/cooked content
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?
What determines the order of frequent poster icons in the topic list?
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
Can You Embed Individual's Discourse Stats Live on a Web Page?
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?
Find user through API by user field?
Can't send PM via API (HTTPUnprocessableEntity 422)
How to contribute to the Discourse API Docs
Swagger, WADL or similar for the REST API?
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
Discourse documentation
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
(Kane York) #4

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


the instructions in the SSO thread to log a user
Using the Discourse API Ruby Gem
(Rafael Rinaldi) #15

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?


(Kane York) #16

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.


(Rafael Rinaldi) #17

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

Cheers!


(Mathias Källberg) #144

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.


(Blake Erickson) #145

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:


(Mathias Källberg) #146

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:


(Blake Erickson) #147

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


(Mathias Källberg) #148

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:


(Blake Erickson) #149

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.


(Mathias Källberg) #150

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:


(Blake Erickson) #151

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.


(Mathias Källberg) #152

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


(Blake Erickson) #153

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.


(Andre Kosak) #160

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.


(Mittineague) #161

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


(Blake Erickson) #205

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


#208

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?


(Blake Erickson) #209

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.