# How to interact with discourse platform using REST API's

**URL:** <https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859>\
**Category:** BITS\_2020\
**Tags:** rest-apis\
**Created:** [May 20, 2020, 10:13am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859 "2020-05-20T10:13:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [May 20, 2020, 10:13am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/1 "2020-05-20T10:13:32Z")

</div>

This is a simple documentation which i made for you. here i am going to show you how to interact with discourse platform i.e [https://metastudio.org/](https://metastudio.org/) using discourse API’s.This documentation gives you a overview of a DISCOURSE APIs using _curl._

For interacting to discourse platform using API’s, one important thing you need is API Key. Without API key you can not interact with discourse platform i.e [https://metastudio.org/](https://metastudio.org/)

To create an API key, Make sure you are admin of the site. once you have a proper access to the site, then follow the below steps as shown in the images to create an api key:

 ![screenshot(1)](https://metastudio.org/uploads/default/original/2X/1/1a6bc4dda75346b0d714f172f072b36d92e8ca76.png)

 ![screenshot](https://metastudio.org/uploads/default/original/2X/6/6e3f279193a83660e62fabe38b5397d5842e4553.png)

 ![screenshot(2)](https://metastudio.org/uploads/default/original/2X/8/8c3674e5b92725c9d6484af47cf87a9e18c4cc6c.png)

> [size=6]GET API’s:[/size]

The GET request simply looks like this, for getting a specific resources. Once you have your API Key you can pass it in along with your API Username as an HTTP header like this:

_ **Get some groups:** _

`curl -X GET "https://metastudio.org/groups.json" -H "Api-Key: 9179xa8f80c6943ead9c7ab19ad3c67f542838a8ae2a854910d3a1dxd14773cf" -H "Api-Username: username"`

Most API endpoints provide the same content as their HTML counterparts. For example the URL `/groups` (i.e [https://metastudio.org/groups](https://metastudio.org/groups)) serves a list of groups, the `/groups.json` (i.e [https://metastudio.org/groups.json](https://metastudio.org/groups.json)) API provides the same information in JSON format.

_ **Get some categories:** _

`curl -X GET "https://metastudio.org/categories.json" -H "Api-Key: 91798a8f80c6943ead9c7ab19ad3c67f542838a8ae2a814910d3a1ded14773cf" -H "Api-Username: username"`

URL `/categories` (i.e [https://metastudio.org/categories](https://metastudio.org/categories)) serves a list of categories, the `/categories.json` (i.e [https://metastudio.org/categories.json](https://metastudio.org/categories.json)) API provides the same information in JSON format.

_ **Get a single Topic:** _

`curl -X GET "https://metastudio.org/t/project-ideas-wiki-page/3801.json" -H "Api-Key: 91798a8f80c6943ead9c7abe9ad3c67f542s38a8ae2a854510d3a1ded14773cf" -H "Api-Username: username"`

Note: Api-Username: name of the registered user of discourse platform i.e [https://metastudio.org](https://metastudio.org).  
Api-key: Api key can be created by admin only.

> [size=6]POST API’s[/size]

The Content-Type for POST and PUT requests can be set to `application/x-www-form-urlencoded` , `multipart/form-data` , or `application\json` .

_ **Create a single topic:** _

```
curl -X POST "https://metastudio.org/posts.json" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: 91798a8f80c6943ead9c7ab19ad3c17f542838a8ae2a854910d3a1ded14773cf" \
-H "Api-Username: username" \
-F "title=new topic creating for testing" \
-F "raw=desc of new topic creating for testing" \
-F "archetype=regular" \
-F "category=5"

```

> Likewise you can use PUT and DELETE API’s.

Refer below urls for more info:

[https://blog.discourse.org/2017/03/discourse-api-documentation-released/](https://blog.discourse.org/2017/03/discourse-api-documentation-released/)

[https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576)

[https://www.braze.com/docs/api/postman\_collection/#what-is-postman](https://www.braze.com/docs/api/postman_collection/#what-is-postman)

[https://www.guru99.com/postman-tutorial.html](https://www.guru99.com/postman-tutorial.html)

[http://www.gregreda.com/2015/02/15/web-scraping-finding-the-api/](http://www.gregreda.com/2015/02/15/web-scraping-finding-the-api/)

> [https://developer.mozilla.org/en-US/docs/Tools/Network\_Monitor/request\_details](https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor/request_details)

[https://www.baeldung.com/curl-rest](https://www.baeldung.com/curl-rest)

when js framework is finalized to this project, after that i will make one more documentation on that framework. where i will show you how to write api calls using that framework.

Please feel free to ask any query.

---

<div class="post-metadata">

**Author:** ![punkish](https://metastudio.org/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@punkish](https://metastudio.org/u/punkish)\
**Post date:** [May 21, 2020, 10:33am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/2 "2020-05-21T10:33:52Z")

</div>

> [@Siddhu\_Dhangar](#):
>
> This is a simple documentation which i made for you. here i am going to show you how to interact with discourse platform i.e [https://metastudio.org/](https://metastudio.org/) using discourse API’s.This documentation gives you a overview of a DISCOURSE APIs using _curl._
> 
> For interacting to discourse platform using API’s, one important thing you need is API Key. Without API key you can not interact with discourse platform i.e [https://metastudio.org/](https://metastudio.org/)

I would like to suggest that all API routes be available under a single, unified prefix. My suggestion would be `/api/v<version>/`, so for example

```html
https://metastudio.org/api/v1/groups.json
https://metastudio.org/api/v1/posts.json

```

This ensures a clean separation from the API endpoint and the HTML endpoint.

---

<div class="post-metadata">

**Author:** ![punkish](https://metastudio.org/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@punkish](https://metastudio.org/u/punkish)\
**Post date:** [May 21, 2020, 11:25am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/3 "2020-05-21T11:25:58Z")

</div>

> [@Siddhu\_Dhangar](#):
>
> or interacting to discourse platform using API’s, one important thing you need is API Key. Without API key you can not interact with discourse platform i.e [https://metastudio.org/](https://metastudio.org/)

@Siddhu_Dhangar @surendra @gnowgi Please create an API key for me. That way I can tinker around with the API as well. Many thanks.

---

<div class="post-metadata">

**Author:** ![surendra](https://metastudio.org/user_avatar/metastudio.org/surendra/32/84_2.png) [@surendra](https://metastudio.org/u/surendra)\
**Post date:** [May 22, 2020, 7:44am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/4 "2020-05-22T07:44:59Z")

</div>

I will shortly create a api key on testing discourse testing instance and share details on telegram.

---

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [May 22, 2020, 8:05am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/5 "2020-05-22T08:05:52Z")

</div>

hi,  
@punkish it is a good suggestion actually. but As you know that metastudio site is running on discours platform and all discourse apis have been developed by discourse team.  
To modify all those urls of apis, we will have to rewrite all the apis with new url prefix which is “api/v1/” using discourse plugin.

what should we do now ? should we use existing and well tested apis for developing the app ? or should we write new apis with “api/v1/” prefix ?

---

<div class="post-metadata">

**Author:** ![GN](https://metastudio.org/user_avatar/metastudio.org/gn/32/671_2.png) [@GN](https://metastudio.org/u/GN)\
**Post date:** [May 22, 2020, 10:57am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/6 "2020-05-22T10:57:45Z")

</div>

For content that is public, we do not need an API key to get the data. If a topic is published under a private group etc, it is not possible to get the data unless the user is eligible to see the data.

For put commands we certainly need API key. So, to get a simple readonly UI, we do not need much. With this data, you would know how to manipulate the DOM, and render the content in HTML through JS.

---

<div class="post-metadata">

**Author:** ![punkish](https://metastudio.org/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@punkish](https://metastudio.org/u/punkish)\
**Post date:** [May 22, 2020, 3:16pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/7 "2020-05-22T15:16:00Z")

</div>

Discourse has a boatload of settings on the server side. You might want to check if you can change the prefix of the API endpoint. If yes, great, do it. If not, well, then we live with that restriction.

---

<div class="post-metadata">

**Author:** ![punkish](https://metastudio.org/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@punkish](https://metastudio.org/u/punkish)\
**Post date:** [May 22, 2020, 3:18pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/8 "2020-05-22T15:18:49Z")

</div>

ah, so let’s us clarify the remit – you want a read-only application. Absolutely no user feedback. Just read only.

But then, in that case, why even bother making anything? There are many lightweight RSS readers that do the job very well. Actually, I now exclusively read Stemgames via an RSS reader on my phone and laptop. I have to go the the actual website only when I want to respond.

Just learning how to code is a good enough goal, and I have no quibbles with that. But I hope that is learn to everyone involved.

Of course, I could be completely wrong in my assessment above so do set me right as needed.

---

<div class="post-metadata">

**Author:** ![GN](https://metastudio.org/user_avatar/metastudio.org/gn/32/671_2.png) [@GN](https://metastudio.org/u/GN)\
**Post date:** [May 22, 2020, 6:57pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/9 "2020-05-22T18:57:38Z")

</div>

No, what I meant is not a read-only-client. I only wanted to point out that data of public resources can be obtained without API keys. Since @Siddhu_Dhangar’s examples for get command also supplies API key, I wanted to mention that it is not necessary for reading public data.

Also, those who wanted to get the UI ready need not wait till all the functions are written. The learning required for CSS and HTML, and a little bit JS part of the App can be done. Another team can begin to write functions to add replies and posts, likes etc.

---

<div class="post-metadata">

**Author:** ![surendra](https://metastudio.org/user_avatar/metastudio.org/surendra/32/84_2.png) [@surendra](https://metastudio.org/u/surendra)\
**Post date:** [May 25, 2020, 4:23am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/10 "2020-05-25T04:23:14Z")

</div>

It will look clean for sure.

But presently our main focus should be on increasing the performance & removing the boatload.

If we get into changing api url prefix, everytime the discourse server is upgraded there are chances we might be break the instance. (At least one discourse upgrade is available every month)

Also changing the API URL prefix will not have any change on data that is fetched and also will not provide any kind of optimisation. Given the time constrain I suggest we can skip this part for now.

---

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [May 27, 2020, 12:06pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/11 "2020-05-27T12:06:36Z")

</div>

**Problem:**

I am able to get the data from by using the API with a status code 200 but while parsing it, the local server crashes with an error: [Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse].

It is happening while trying to get users, posts, or even categories.

**Solution:**

```auto
const fetch = require('node-fetch');
let url = 'https://metastudio.org/groups.json';

let settings = { method: "Get" };

fetch(url, settings)
    .then(res => res.json())
    .then((json) => {
        console.log(json.groups);
        console.log(json.groups[0]);
    });

```

===========================================================

```auto
const fetch = require('node-fetch');
let url = 'https://metastudio.org/categories.json';

let settings = { method: "Get" };

fetch(url, settings)
    .then(res => res.json())
    .then((json) => {
        console.log(json);
        //console.log(json);
    });

```

Hi Bir,

I am able to fetch the groups and categories data successfully using node-fetch library. can you try to replace your code with this ? and try once again.  
i hope this answer helps you.

---

<div class="post-metadata">

**Author:** ![surendra](https://metastudio.org/user_avatar/metastudio.org/surendra/32/84_2.png) [@surendra](https://metastudio.org/u/surendra)\
**Post date:** [May 28, 2020, 8:10am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/12 "2020-05-28T08:10:15Z")

</div>

@punkish  
@f20180261  
@GN @Siddhu_Dhangar @mahesh.tirthakar

Presently you can use below URL to view & test the site.  
[https://apptest.metastudio.org/](https://apptest.metastudio.org/)

SSH access and code build related details to @f20180261 will be shared soon as I am working on it.

---

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [June 17, 2020, 4:23am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/13 "2020-06-17T04:23:25Z")

</div>

@gnowgi @surendra @mahesh.tirthakar @f20180261

[https://meta.discourse.org/t/should-the-password-be-plain-text-when-creating-a-user-through-the-api/66024/4](https://meta.discourse.org/t/should-the-password-be-plain-text-when-creating-a-user-through-the-api/66024/4)

Yesterday i asked discourse team about the login issue. they suggested me that try to use SSO(Single Sign ON) or certificate pinning. Today, I am looking Keycloak SSO which was already deployed by surendra two months ago. I am trying to understand this keycloak SSO like how it works and how we can authenticate the registered users which are already registered on metastudio site. For this i am taking surendra’s help.

References:

[https://developers.redhat.com/blog/2020/01/29/api-login-and-jwt-token-generation-using-keycloak/](https://developers.redhat.com/blog/2020/01/29/api-login-and-jwt-token-generation-using-keycloak/)

[https://stackoverflow.com/questions/48220504/login-to-keycloak-using-api](https://stackoverflow.com/questions/48220504/login-to-keycloak-using-api)

[https://www.keycloak.org/docs-api/5.0/rest-api/index.html](https://www.keycloak.org/docs-api/5.0/rest-api/index.html)

[https://scalac.io/user-authentication-keycloak-1/](https://scalac.io/user-authentication-keycloak-1/)

---

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [June 17, 2020, 4:07pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/14 "2020-06-17T16:07:40Z")

</div>

@gnowgi @f20180261 @surendra @mahesh.tirthakar

I tested the keycloack (SSO) integration with node js locally. I took clone from the following git url i.e [https://github.com/austincunningham/keycloak-express](https://github.com/austincunningham/keycloak-express).

I followed all the instructions which was given on the above github url for testing the integration part. some settings need to be configured on keycloak. so i did all the required configurations. finally login part is working fine.

References:  
[https://dev.to/austincunningham/keycloak-and-express-1cdd](https://dev.to/austincunningham/keycloak-and-express-1cdd)

[https://medium.com/@ramandeep.singh.1983/enterprise-web-app-authentication-using-keycloak-and-node-js-c10b0e26b80d](https://medium.com/@ramandeep.singh.1983/enterprise-web-app-authentication-using-keycloak-and-node-js-c10b0e26b80d)

---

<div class="post-metadata">

**Author:** ![GN](https://metastudio.org/user_avatar/metastudio.org/gn/32/671_2.png) [@GN](https://metastudio.org/u/GN)\
**Post date:** [June 17, 2020, 4:12pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/15 "2020-06-17T16:12:07Z")

</div>

Since we want to use a single sign on across all our platforms, this is the way to go. Thanks for exploring this. Please pass this or send a patch to the App.

---

<div class="post-metadata">

**Author:** ![Siddhu\_Dhangar](https://metastudio.org/user_avatar/metastudio.org/siddhu_dhangar/32/508_2.png) [@Siddhu\_Dhangar](https://metastudio.org/u/Siddhu_Dhangar)\
**Post date:** [June 17, 2020, 4:30pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/16 "2020-06-17T16:30:15Z")

</div>

If i am wrong then please correct me. From now on, In lightweight app, login and registration part will be handled by keycloak SSO only. right.

---

<div class="post-metadata">

**Author:** ![GN](https://metastudio.org/user_avatar/metastudio.org/gn/32/671_2.png) [@GN](https://metastudio.org/u/GN)\
**Post date:** [June 17, 2020, 4:32pm UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/17 "2020-06-17T16:32:51Z")

</div>

Not only for the app, but for all other services we might deploy, we shall use SSO.

@surendra is [https://interx.metastudio.org](https://interx.metastudio.org) also using SSO? If not, can we use the same?

---

<div class="post-metadata">

**Author:** ![surendra](https://metastudio.org/user_avatar/metastudio.org/surendra/32/84_2.png) [@surendra](https://metastudio.org/u/surendra)\
**Post date:** [June 18, 2020, 8:08am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/18 "2020-06-18T08:08:13Z")

</div>

Yes SSO can be integrated with [https://interx.metastudio.org](https://interx.metastudio.org/).

---

<div class="post-metadata">

**Author:** ![surendra](https://metastudio.org/user_avatar/metastudio.org/surendra/32/84_2.png) [@surendra](https://metastudio.org/u/surendra)\
**Post date:** [June 18, 2020, 8:18am UTC](https://metastudio.org/t/how-to-interact-with-discourse-platform-using-rest-apis/3859/19 "2020-06-18T08:18:19Z")

</div>

@f20180261@GN @Siddhu_Dhangar @punkish @mahesh.tirthakar

We need OpenID connect (an extension of Oauth) integrated with our [apptest.metastudio.org](http://apptest.metastudio.org).

There are many users on [metastudio.org](http://metastudio.org) who do not have password login, they always using login with OpenID connect which is a Keycloak instance (This instance has login with google/facebook or password based login).

Please look regarding integration of keycloak with nodejs. There will be a library already available for it.

Below share are links that may help getting started.

[https://github.com/keycloak/keycloak-nodejs-connect](https://github.com/keycloak/keycloak-nodejs-connect)

[https://medium.com/@ramandeep.singh.1983/enterprise-web-app-authentication-using-keycloak-and-node-js-c10b0e26b80d](https://medium.com/@ramandeep.singh.1983/enterprise-web-app-authentication-using-keycloak-and-node-js-c10b0e26b80d) (already shared by siddhu above)
