Create Custom APIs

What is the best method to create custom APIs on the discourse forum.

Eg: I want to create a get request which gives me users.count, topics.count, categories.count
How do I add this API into forum ?

You need to write a plugin. See Beginner’s Guide to Creating Discourse Plugins Part 1: Creating a basic plugin

4 Likes
 /admin/about.json

Has most of that, I think. Or maybe it’s just /about.json.

1 Like

Not exactly the same data, so I want to create custom JSON API.

1 Like

I’ve been using GitHub - discourse/discourse-subscriptions: A Discourse plugin that allows payments and subscription management via Stripe. as a model for adding a model (you don’t need a full model, but discourse-subscriptions/routes.rb at main · discourse/discourse-subscriptions · GitHub gives you an example of how to add a route. That and add_to_class is most of what you need to know. Good luck!

1 Like

Thanks @pfaffman
Will look into it.

1 Like

I was unable to create a custom page/API through plugin.
Is there a beginner’s guide which I could follow ?? or any article that shows the basic steps.
I have followed discourse guide and was able to add settings to plugin but was unable to create a new page.
@pfaffman

I used the subscriptions plugin as a guide for one that I made. The plugin generator will generate a plugin with a new route. It’s complicated because you have both the rails and ember pieces to wrangle. If you want to add just a rails (api) route then it’s not too hard. (others might say that adding just an ember route is easy).

Do you need a new page to render in the browser or just the back end?

1 Like