Using the Discourse API Ruby Gem


(Admir Hodzic) #19

I hope that more detail can get from @acs Please read this post

(Jeff Atwood) #20

What does “continuous content tracking and analysis” mean? Can you provide some specific examples?

(Kane York) #21

Sounds like you want a latest posts feed :wink:

(Rob Rhinehart) #22

I want to be able to programmatically analyze the content of the forums in real time for reporting and analytics purposes.

@riking yes! something like that. Is there an endpoint I can use to get topics and / or posts by date?

Connect to pg database from console in Discourse installation?
(Kane York) #23

Well, for topics by date, you have /latest.json?page=n. And for posts by date, once that gets merged, you have /posts.json?before=id.

(cviebrock) #24

I’m trying to follow the instructions in the SSO thread to log a user out of Discourse from my other app.

First, I get the Discourse userId based on my app’s user id via GET /users/by-external/{MY_ID}.json. Then, I’m trying to log them out with POST /admin/users/{DISCOURSE_ID}/logout.

At first, this gave me a 403 Forbidden error. So I generated a global API key and passed that in my POST data as api_key=xxxxxx. That throws a 500 Internal Server Error. So I also tried adding api_username=xxxx to the POST data. There are several user accounts on our Discourse installation that are marked as Admin, so I’m not sure which I should use. I chose the system account. Now I get a 404 Not Found response.

I took a look through the list of endpoints as mentioned here, but don’t see anything for logout.


Official Single-Sign-On for Discourse (sso)
(Adam Capriola) #25

Try a user other than system as the admin – I think I had trouble using it before.

(Adam Capriola) #26

Oh, also: it’s /admin/users/{DISCOURSE_ID}/log_out (not logout, there should be an underscore).

(cviebrock) #27

Thanks @AdamCapriola … switching to the correct URL worked.

FWIW, I’m still using the system user (after having tried some other admin-level users).

(Руслан Корнев) #28

Hi, any news on this feature?

(Sam Saffron) #29

discourse api support write access just fine, there may be a few endpoints you need to decorate there PRs welcome.

(Руслан Корнев) #30

Ahh, today tried discourse_api and found that there is already this functionality. README on discourse_api gem seems outdated.

For those who also searhing this here is example.

$discourse.create_topic :title => 'Это еще одна тема созданная с использованием api', :raw => 'Я должен был придумать другой текст для того чтобы Discourse не считал, что текст темы такой же.'
[6] pry(main)> ls $discourse
DiscourseApi::API::Categories#methods: categories  category  category_latest_topics  create_category
DiscourseApi::API::Search#methods: search
DiscourseApi::API::SSO#methods: sync_sso
DiscourseApi::API::Topics#methods: create_topic  delete_topic  latest_topics  new_topics  recategorize_topic  rename_topic  topic  topics_by
DiscourseApi::API::Posts#methods: create_post  edit_post  get_post  wikify_post
  activate     grant_admin   list_users  revoke_admin   update_email        update_user      user
  create_user  invite_admin  log_out     update_avatar  update_trust_level  update_username
DiscourseApi::API::Groups#methods: create_group  group_add  group_remove  groups
DiscourseApi::API::Invite#methods: disposable_tokens  invite_user  invite_user_to_topic
DiscourseApi::API::PrivateMessages#methods: private_messages
DiscourseApi::API::Notifications#methods: notifications
DiscourseApi::API::Badges#methods: badges  grant_user_badge  user_badges
DiscourseApi::API::Email#methods: email_settings  list_email
DiscourseApi::API::ApiKey#methods: api  generate_master_key  generate_user_api_key  regenerate_api_key  revoke_api_key  revoke_user_api_key
DiscourseApi::API::Backups#methods: backups
DiscourseApi::Client#methods: api_key  api_key=  api_username  api_username=  connection_options  delete  get  host  patch  post  put  ssl  user_agent
instance variables: @api_key  @api_username  @connection  @connection_options  @host  @user_agent

Thank you @sam for all you work!


For those of us that installed discourse using Docker, how do we do the following?

Open the discourse_api/examples/example.rb file, and modify following information:

(Sam Saffron) #32

Thats actually in the API gem:


Thanks Sam for getting back to me. Where do we insert this?

$LOAD_PATH.unshift File.expand_path(’…/…/lib’, FILE)

(Sam Saffron) #34

you should not need that … just replace that with require 'discourse_api'


Sorry for being unclear. I’m using the docker install, and I’m not familiar with the process of editing files within docker.

How would I navigate from
root@discourse: /var/discourse
to where I can edit the API?

(Jeff Atwood) #36

As I mentioned before, you need to have a complete Ruby and JavaScript software development environment set up. That is what “using the Discourse API Ruby Gem” means.

@techapj I think this guide could use some updating since it is confusing people.

(Arpit Jalan) #37

Okay, I updated the first post to make it clear that Discourse development environment is required for installing and using Discourse API Ruby Gem.

(M Zain Damlag) #38

@techAPJ Is there a way or a command to generate the master API key via console

like disabling sso: SiteSetting.enable_sso = false