I’m just starting to learn how to use the Discourse API.
I’ve got data coming back when I send an api_key and api_username to an endpoint.
However, specifying the user by their username seems mysterious and brittle. I need to know the right username but the user can change their username and I don’t know what it actually is from outside.
My Discourse users can only import an account from my Bubble app. In the SSO section there’s an ‘external id’ that looks like it’s the account’s unique id from Bubble.
Is there a way to specify the user I want based on their external id instead of their username?
So when using the api you can search for them by external_id, but you still need to reference their username in most api calls, like in order to get their notifications.
You would call this endpoint, and specify their username:
Correct, but you can cache it depending on what you are doing, so you don’t have to look it up everytime.
Nope.
Do your usernames change a lot though? I do kind of agree with you, but even if it was by id you would still have to look up the users id based on the external_id and use that.
I’m not totally sure though but some endpoints might take either one (username or id), I know this isn’t a user endpoint, but for example the topic endpoint you can do something like this and leave out the slug:
https://meta.discourse.org/t/-/87383.json
I digress though, since most endpoints do key off of the username.
Is there a way to get the current number of unread notifications? Like the number that displays in Discourse? I want to display that number back in the Bubble app so users know to check the forum.
It looks like what’s returned from notifications.json is a list of notifications?
Does Discourse have an immutable id for each account? I guess I just assumed it would since that’s how everything I’m familiar with works.
Oh, cool! Thanks.
Okay, so it looks like that endpoint is only giving me my session info. The info for the username in api_username. When I add another “username” parameter it seems to be ignored.
This sounds similar Session/current always returns api_user
Is there a better way to discover this stuff than bothering people in the forum? docs.discourse.org looks pretty sparse, like I can’t even find that “session” endpoint when I search for it specifically.
and also looking at the source code for the controller of the endpoint you are trying to hit is very helpful:
This is because that endpoint doesn’t have support for a “username” param.
The api docscould use will always need some work, but they do document most of the heavier used endpoints. If you find a new one we would very much appreciate the help of the community to help document it. I will keep trucking along myself though and continue to document things at a steady pace as time permits.
I’m not familiar with Bubble. Is this what you are referring to?
Either way, Discourse by nature has an api endpoint for most things, but that doesn’t necessarily mean that it will always be super friendly when you are trying to call things from external sites instead of natively from inside Ember with a logged in user cookie. This may mean that it will be more productive to write a custom plugin or use the data explorer plugin to write a custom query and then make an api request to that saved query. Also PR’s are always welcome to improve something.