Using the REST API with other languages


(Buddy) #1

I wanted to create somewhere to consolidate information about the REST API, so that people wishing to consume the API from code won’t need to reinvent any wheels.

First off, here are some API projects in various languages:

This post is a wiki; if you know of any other API projects, please feel free to add them directly.

Next, I would like to discuss how to do some common operations using the api, but before we go ahead with that, I think it would be best to discuss what the use cases are. Has anyone tried else tried to use the Discourse API? What for? What were your requirements? How did you achieve them?

(Ryan Bolger) #2

One of the main use cases for me with the REST API is more freedom to write an importer from another forum using a language other than Ruby and having to get down and dirty in the docker console. It’s not that I can’t learn Ruby enough to read and modify one of the existing importers. It’s just a complication that seems unnecessary.

(Admir Hodzic) #3

I’m dying to make .NET (c#) client which will consume Discourse api.
My company resident in .NET sea but We use discource for internal communication.
There is huge sets of business process which I’ll love to integrate (communicate) with Discourse.
What I found useful is that when you add .JSON extension to Discourse URI it return JSON that JSON
is easly converted to classes (models) by I am trying to begin making opensource .NET client which will be able to communicate with discource. But I do not have enough courage to begin by my own (because of lacm my .NET knowledge) . If someone will begin .NET project I’ll be happy to contribute and work as junior developer listening instructions from others.

(Buddy) #4

Interesting. I wonder if the API would be able to support as much flexibility with importing as Ruby scripts would? I’m thinking things like backdating posts, or setting post html contents directly instead of putting them though the markdown parser. I suppose you would need to be an admin for the task you are talking about anyway.

Another thing that can be useful is to examine the rails routes to get an overview of what endpoints are available.

Something that you will need when making api requests is a csrf token, which can be gained by doing a GET on /session/csrf.json, then adding the token that you get back to your headers as "X-CSRF-Token".

(Apparently Archetype) #5


we* use the rest API for various ahem purposes…


we wrote a bot, ok?

. GitHub - SockDrawer/SockBot: Sockbot - A sock puppet Robot worthy of TheDailyWTF that interfaces with SO META!

it’s hardcoded to be directed at that forum. the one that gave us all those C.W. Montgomery Burns clones. Yeah it could be changed to point at meta.d but you’d need to understand our code a fair bit to do that, so it’s fairly spam safe(ish)

* royal we because funny?

(Buddy) #6

What I like about Accalia’s project is that I know it works. I’ve seen SockBot operating on, watching notifications and replying when summoned. This project is always the first I check when I want to know how something works.

(Apparently Archetype) #7

yep. there are a variety of clones of sockbot running around that forum.

by far the most popular one is Zoidberg.

all he does is reply to a @summon with a random zoidberg quote, but that’s still extremely popular. it ammuses us greatly to see how popular “he” is!

(Alvaro del Castillo) #8

Our use case is to gather information from discourse and generate a dashboard with stats like we do now for askbot. For example in OpenStack:

We are deciding the right approach now, and in a first iteration, it seems that going directly to JSON version of discourse pages could be enough. The Python API could be useful because our gathering tools are implemented in Python.

As soon as design is finished we will start working in the development and the idea is to share with the community the project. The final product will be open source (and ideally the development will be done in an open way).

Which is the more complete language API? Some other projects doing similar things?

(Sam Saffron) #9

the most complete api implementation is at GitHub - discourse/discourse_api: Ruby API for Discourse but it is not 100% complete, some stuff still needs adding.

(Alvaro del Castillo) #10

Great Sam! I have reached the same conclusion looking to github and when I found the GitHub - discourse/discourse_api: Ruby API for Discourse as an “official” project inside discourse github. So I will use it as the main reference.

In this step we need to decide if reuse our current tools (including data models) and create a new backend for discourse or we need to create a new tool. We have tools for analyzing mailing lists and also for forums like askbot. Time to thought which approach is better.

Discourse is pretty cool. This project is going to be funny.

(Alvaro del Castillo) #11

After some checkings, our db model for Sybil maps pretty well to discourse so the plan is to add a backend to Sybil for discourse.

(Alvaro del Castillo) #12

Sam, we have a first version of the Sibyl backend for Discourse and it is working pretty well.

But when gathering the posts for a category, we get at max 30 posts.

We are using the URL:

where support is the category name. Is it possible to paginate over the posts in this category so we can get all posts for the category?

Ups, I have seen the:

u’more_topics_url’ and u’per_page’

keys. Time to play with them for pagination!

Working with it! I hope I can share with you a dashboard with metrics soon.

All the code is now in:

(Sam Saffron) #13

Doable, look at chrome dev tools Ajax calls while scrolling through category

(Khoa Nguyen) #14

I use Discourse API to write importer with PHP. But it has many limitation when doing it with API.
I’m writing a commemt system base on API with my PHP project (user can post directly from website)

(Admir Hodzic) #15

Can you please provide some URI for getting more data please .
I am also stacked wit 30 topics per call. and do not know how to get all topics.

(Alvaro del Castillo) #16

Sure Admir.

Using the Discourse API Ruby Gem
(Alvaro del Castillo) #17

Using the current API we have finished the discourse backend for Sibyl.

We have created a sample dashboard with the data:

The statistics are similar to the one you have in:

except the post number. We have gathered 65K posts and according to your stats there are 91.3K.

The posts (answers) gathering is a bit tricky and maybe we are loosing posts in it:

Using the Discourse API Ruby Gem
(Kiko Lobo) #19

Is there a definition of the data that the api throws back?? Say a model for a topic? What are all the fields returned?

(Apparently Archetype) #20

Not that i’ve ever found.

but everything is somewhere in this repo: GitHub - discourse/discourse: A platform for community discussion. Free, open, simple. (let me know if you make sense of it, i’m still trying to wrap my brain around it.)

I think all the serializers are here but what selects which one to use when, and how the data is fed into them (or indeed where ti comes from) i still havent found.

I’m also still looking for how exactly the message-bus feed is formed, because that’s something i integrate heavily with on my project. Havent made much progress on that though.

I’m not terribly familiar with Ruby, but i’ve got to say discourse has been one of the hardest projects i’ve ever tried to grok. Not sure if that’s my lack of familiarity with ruby or if it’s just that discourse is not terribly new developer friendly.

I’ve been documenting what i can figure out as part of my own project, but that’s unofficial documentation so it shouldn’t be taken as gospel or anything like that. <= Any documentation under api/external is describing discourse may be helpful.

(Kiko Lobo) #21

Thank you very much. Is super useful!