Using the Discourse API Ruby Gem


(Arpit Jalan) #1

So you want to use Discourse API? Great! Let’s get started.

Set up Discourse development environment

Set up Discourse development environment using our Windows, OS X or Ubuntu guide.

Clone Discourse API Gem

Now that you have set up Discourse development environment, you should already have Git and Ruby installed on your system. You can install Discourse API gem by running following command from console:

git clone ~/discourse_api

Install dependencies

Open the discourse_api directory and type:

bundle install

This will install any required gem dependencies.

Generate Master API Key

Generate Master API Key for your Discourse instance by visiting /admin/api, to interact with Discourse API.

Provide API Credentials

Now that you have cloned Discourse API gem and generated master API key, let’s start using it!

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

client ="http://localhost:3000")
client.api_key = "YOUR_API_KEY"
client.api_username = "YOUR_USERNAME"

Replace http://localhost:3000 with the url of your discourse instance, eg:

Replace YOUR_API_KEY with the master API key of your discourse instance, eg: b1f3175cb682b3e9b6ca419db77772120b19af993cbc14ebed80fea08e3bbd66

Replace YOUR_USERNAME with the Admin username of your discourse instance, eg: codinghorror

Access Discourse API

Now in console, from discourse_api directory run:

ruby examples/example.rb

This command will print out latest topics from your Discourse instance.

That’s it. Start using Discourse API today.

Discourse api key
Discourse api tutorial
Python Twisted Client
If i insert rows in topic table directly in database will it serve as topic in discourse?
Thoughts from community metrics conference
Discourse SSO Logout
Can You Embed Individual's Discourse Stats Live on a Web Page?
Swagger, WADL or similar for the REST API?
API Error: You are not permitted to view the requested resource
API not working
Configuring Reply via Email :e-mail:
(Theyagarajan Sundaramoorthy) #5

I cannot see any documentation for per user API key. Is it available yet? or planned in 1.0?

(Theyagarajan S) #6

I’m looking at an API for creating user accounts. Unable to find an endpoint to do that. Is it possible to achieve this?

(PJH) #7

The only API key stuff I’ve found are under admin only and are

  1. A Generate button on the user’s profile page under admin (/admin/users/USERNAME)
  2. The list of current API keys already generated - again admin (/admin/api)

I’ve not seen anything from a <= moderator perspective yet.

(James Milligan) #8

@taggy I believe it is, but I haven’t tried it yet myself. I’ve been using the PHP API client from Discourse Hosting (@michaeld). See GitHub - discoursehosting/discourse-api-php: PHP API client for Discourse for the bits and bobs

If i insert rows in topic table directly in database will it serve as topic in discourse?
(Jason May) #9

@sam @techAPJ The discourse_api gem currently does not appear to support updates, only data-fetching. Is this in the works? Or should I go ahead and implement: I want to create topics, posts, categories and groups via API.

(Sam Saffron) #10

Sure add PRs that add those end points.

(Rinold Simon) #11

how to visit /admin/api and get my api_key and username credentials ?

(Blake Erickson) #12

Using the discourse_api gem v0.3.6 you can use:


And it will return all the api keys you have generated:

    "id": 1,
    "key": "test_d7fd0429940",
    "user": {
      "id": 1,
      "username": "test_user",
      "uploaded_avatar_id": 7,
      "avatar_template": "/user_avatar/localhost/test_user/{size}/7.png"

@Rinold_Simon Not sure if that is what you are referring to. You have to be an admin to visit /admin/api to generate an api_key or ask an administrator to generate one for you.

(Sudhanshu Mishra) #13

Is there a way to change/create global notice using Discourse API?

(Kane York) #14

If you’re talking about the site setting, change any site setting and watch your browser’s network logger for the request to make.

If you’re talking about making a banner topic, well, same thing.

(Rob Rhinehart) #15

Is there an API function to retrieve all topics and / or posts short of a whole export?

(Jeff Atwood) #16

What is the goal? Export is what you would expect if you want all topics/posts.

(Admir Hodzic) #17

You may look at this great tool Sibyl.
They do extracting all topics using api

(Rob Rhinehart) #18

The goal is continuous content tracking and analysis. Export will work for now but live tracking would be nice. Maybe I could pass a parameter to latest_topics or a similar function and get all topics by date?

@adopilot I can’t find the tool you mentioned could you link me to it?

Using the REST API with other languages
(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.