Interact with discourse from Python?

Hi everyone! Love discourse, it’s the backbone of our community.

We are an open source project with a strong presence on Github. We have a robot written in Python which manages our Github repositories and does things like merge pull requests automatically if they have been approved. Since we have a lot of repositories and not everyone follows every repository it would be awesome if the robot could post in the forums when a new project is created or when a pull request needs to be reviewed.

Can anyone point me in the direction of how I could best call the Discourse API from Python?

3 Likes

There is a python discourse api library that is a few years old:

And a forked version that has been updated somewhat recently:

Hopefully the latter one will work for you.

Also this plugin may be of interest to you:

https://meta.discourse.org/t/the-github-linkback-plugin/66081

7 Likes

Thanks @blake! That is exactly the kind of thing I needed to get started.

I will report back with my successes.

2 Likes

I recently started a project for this, and am actively updating it. Currently working towards the next dot release:

https://github.com/samamorgan/discourse

10 Likes

Did it work out for you? I am trying this as well. I keep getting “DiscourseClientError: Payload Too Large”.

1 Like

I’m suddenly seeing this error too over the weekend when attempting to fetch a topic. This topic fetch code hasn’t changed in over a year, so something about the API must have changed. I’m investigating.

2 Likes

Payload Too Large is because of this change.

I don’t know about the project mentioned above, but here is the status for bennylope’s project. Maybe the same fix is needed on other wrappers as well.

GitHub - pydiscourse/pydiscourse: A Python library for the Discourse API issue #38 fixes the problem, but the fixed version has not yet been pushed to pypi- pydiscourse.exceptions.DiscourseClientError: Payload Too Large · Issue #38 · pydiscourse/pydiscourse · GitHub. Waiting for github @bennylope to do it. So you can wait for pypi to be updated, or git pull master.

5 Likes

Thanks! This worked. I uninstalled pydiscourse, and used a local copy based on what you suggested. And now, it works!
I am trying to use Python’s Sentiment analyzer to get a sentiment analysis of the latest posts. I will keep you updated on how it goes.

1 Like

Is anybody else using this? Are you able to get more than 20 posts per topic using this?

1 Like

You probably need to use pagination. I haven’t tried this for posts specifically, but something like:

pagesize = 20
for page in range(100):
    client.posts(topic_id, limit=pagesize, offset=page*pagesize)
3 Likes

Yes I know this is an old post but since it is essentially what I seek no sense it creating a new post when this one works but just needs to be brought up to date.

While I do know how to program in many programming languages, sorry to say Ruby is not one. Was wondering if there are newer are better answers to this question.

Interact with Discourse from Python?


As a side question, can Discourse Plugins and/or themes be created using Python?

1 Like

Themes, definitely not, that’s only Javascript, CSS & HTML.

Plugins: Kind of.

You can import Python modules into a Ruby (on Rails) based Discourse plugin and call Python functions using Pycall: GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language

I use it in Production for a very specific purpose as there is no equivalent module maintained in the RoR ecosystem so it avoids me having to translate and then maintain all that specialist code in Ruby.

It works surprisingly well.

That said, the base of that Plugin is still written in Ruby.

I would recommend you simply learnt Ruby on Rails as well - it really is a very pleasant language and imho the code is nicer to read than Python :slight_smile:

I should also take the time to learn more Python if/when needs must.

Interesting side note: some of the code in Discourse Chatbot 🤖 that creates the agent behaviour was based on a Python agent called “Funkagent” which I first translated into Ruby on Rails (and then modified, enhanced.). So translation can be an option of course if it makes sense to do so. In that case it made sense.

3 Likes

Thanks.

That is a reasonable answer and a possible option that I will keep on the table.

1 Like

I wrote several import scripts before I really “learned” ruby. Depending on what you want to do you might be able to do more with your current skill set than you think. It’s likely easier to use ruby than figure out how to use something else.

If you’re interacting with the API, though, then you can use whatever language you want.

5 Likes

Ruby is beautifully designed and a doddle to learn, imho.

Ruby on Rails requires a bit more learning and as a framework relying on conventions can be fiddly at times but very powerful once you get the hang of it.

2 Likes

3 posts were split to a new topic: How hard would it be for non programmers to be able to use the Discourse AI - AI bot to help them create plugins and/or themes

You can have a look at GitHub - discourse/all-the-plugins · GitHub as a way to look around at what’s possible.

Choose something that you want to do that seems simple. If you want something like cakeday that changes the definition of an anniversary, then cakeday could be a fine place to start. You’d select on small thing you want to change and do things one tiny step at a time.

2 Likes

hah I am teaching myself Ruby these days, what fun indeed. :exploding_head:

3 Likes

Hi everyone, I’m also interested in getting data from discourse (mainly stats about views and nb of users) from python. A lot of the packagse listed here don’t seem to be active anymore. Is there a new standard way of doing this in 2026?

Thanks!

1 Like

You don’t always need to use someone else’s interface.

It’s not too troublesome to do this via the builtin requests library, and if you use something (e.g. Postman, which I already have set up) you can import our API specification to it and then have it generate code:

and then adapt that to your needs:

import json
import pandas
import requests

url = "https://try.discourse.org/categories.json?include_subcategories=false"

payload = {}
headers = {
  'Accept': 'application/json'
}

response = requests.request("GET", url, headers=headers, data=payload)
response_data = json.loads(response.text)
data = pandas.DataFrame(response_data['category_list']['categories']).set_index('id')

now I have category information:

            name   color text_color  ... uploaded_background_dark                                             topics can_vote
id                                   ...                                                                                     
5        general  25AAE2     FFFFFF  ...                     None  [{'fancy_title': 'Testing dulu ya jangan di hi...      NaN
4         videos  258af1     FFFFFF  ...                     None  [{'fancy_title': 'Ikan ganteng yang’&rdq...      NaN
86      calendar  12A89D     FFFFFF  ...                     None  [{'fancy_title': 'Category Calendar demo topic...      NaN
2           tech     444     FFFFFF  ...                     None  [{'fancy_title': 'Poll: What’s your pref...      NaN
1      discourse  00B355     FFFFFF  ...                     None  [{'fancy_title': 'Welcome to our demo!', 'id':...      NaN
53  Topic Voting  F7941D     FFFFFF  ...                     None  [{'fancy_title': 'Is this topic worth voting f...     True
6         gaming  800080     FFFFFF  ...                     None  [{'fancy_title': 'Impressions Games City Build...      NaN
8         movies  B22222     FFFFFF  ...                     None  [{'fancy_title': 'What’s your all-time f...      NaN
9         sports  0000FF     FFFFFF  ...                     None  [{'fancy_title': 'Modernizing the antiquated b...      NaN

[9 rows x 45 columns]

If you want to access protected information you’ll want to use an API key.

6 Likes