Discourse API Documentation

api

(Boopathi S) #95

is it possible to get the groups details through rest api call? possible to add users to groups and category via rest api call.please give suggestions


(Jared Needell) #96

When I try to get a user info via API I get this…


(cpradio) #97

The colon in your username in front of the .json seems odd to me and may need to be escaped or written as %3a (if your username really does have a colon in it)


(Jared Needell) #98

It doesn’t have a colon, I was going by documentation, I tried without the colon and got the same result.


(cpradio) #99

If you run it against Meta do you get the redirect?
https://meta.discourse.org/users/cpradio.json

Hmm… just tried it and it works fine for me.


(Jared Needell) #100

Yes

I figured it out that I had to use an API key since the site isn’t public

Any way to get it to list ALL users?


(cpradio) #101

Hmmm… that I’m not sure of. As I’m not sure there is a single place in Discourse, including the Admin area, where all of the users are listed.


(Mittineague) #102

AFAIK the only way to get all users currently is to do a CSV export


(cpradio) #103

The Data Explorer Plugin could probably provide it too (not sure if API can interact with that though)


(Kane York) #104

You need to manually set up the query, then you can make API requests, but only with admin authorization.


(Jonathan Sick) #105

How can I add or edit tags through the API?

My use-case would be a bot that monitors the forum and tags posts if the content includes some heuristic; or alternatively enforcing a set vocabulary of tags.


(Kane York) #106

Just follow the instructions here and make your own http requests:


(Gerrit Visscher) #107

I wondered why I could not get the private messages but instead received HTML telling me the page does not exist/is private.
Turns out you need to supply the header “Accept: application/json” to get the data. This is not necessary for some/all of the other requests. Maybe you can add this to the description? Or should this be a bug report?


(Bob Sheth) #108

Hi. I’m new to using the api via discourse_api gem. I have managed to create a new topic using client.create_topic(…). Is there a way I can get the topic_id back to save in my rails app (so I can link to the newly created topic later).


(Bob Sheth) #109

Looking at the gem source I have a workaround which is call: client.topics_by("username").first["id"]
after the new topic is posted. Would still be grateful for guidance on a better way to do this. Thanks.


(Blake Erickson) #110

When you create a new topic it will return the topic_id in the response. You just need to store the response in a variable.

topic = {
  title: "my test topic title",
  raw: "the content for my test topic"
}

new_topic = client.create_topic(topic)

puts new_topic['topic_id']

You can then link to it via https://localhost:3000/t/<topic_id>


(Bob Sheth) #111

Thank you! I’m kicking myself for not trying that!
Do you know if there is a method to update the topic with the api gem? I can’t see it anywhere in the source though there seems to be a put cURL way to do it directly?


(Blake Erickson) #112

I think you can use client.edit_post(new_topic['id'], raw) to edit the topic.


(Bob Sheth) #114

Thanks Blake - that works perfectly. I was initially confused between topic_id and id that is returned on a create_topic call. id is the post id and what you stated works as it should.


(Rafael dos Santos Silva) #115

How to set Discourse to readonly mode through an API request?

I’m trying this:

curl 'https://example.company.com.br/forum/admin/backups/readonly' -X PUT --data 'enable=true&api_key=my_api_request&api_username=system' --compressed --insecure