Discourse API: Get user by id

api

(Stephen R Foster) #1

The api call for latest topics returns this bit about the original poster:

posters:

  • extras:
    description: Original Poster
    user_id: 101

However, the /users endpoint requires a username: e.g. /users/Steve.json. Doing /users/101.json returns a 404.

Is there a way to get the original poster’s username from the id returned in the topic response?


(Kane York) #2

/latest.json also returns a list of user id -> user as the first part of the response, which you can use to get the usernames.

{
users: [
{
id: 13301,
username: "MrSteve",
uploaded_avatar_id: null,
avatar_template: "/letter_avatar/mrsteve/{size}/2.png"
},

(Stephen R Foster) #3

Cool! That’ll work.

I was using the latest_topics method in this library GitHub - discourse/discourse_api: Ruby API for Discourse and it wasn’t returning the users portion of the response. I’ll just use GET /latest.json directly.


(Erik Chan) #4

I am using category_latest_topics and am running into the same problem as the topic creator. Its kind of silly that user_id and user_name are not interchangeable and no way to get one another.

I can’t really use /latest.json because I need this to be a specific category


(Robin Ward) #5

/latest.json exists for every category. For example:

https://meta.discourse.org/c/bug/l/latest.json

That will give you the latest in the bug category. Also I should mention we’re not against having an endpoint to retrieve users by ID, we’ve just not needed it for our particular client. If you want to create a PR that supplies such a route it would likely be accepted!


(Erik Chan) #6

Thanks @eviltrout

I went the long way around and did three calls using the ruby API 1) ‘category_latest_topics’ to get the topic id 2) ‘topic’ to get the username, and then 3) ‘user’ to get the stats.


(Melissa) #7

Hi @eviltrout! I also need to find a user by the id and not username. Just confirming that a PR to supply the route would need to be made to Discourse Core and then an endpoint added to the API?

I looked around for any route currently in the Core and did not find anything. Every route seems to use :username.

Thanks!


(Robin Ward) #8

You would need a PR to discourse yes with such an endpoint. Can you explain why you need it though? To date we haven’t had a good reason to implement such a thing so I’m wondering why you need it!


(Melissa) #9

Sure, @eviltrout. For a client, we created an app for students to take his courses. He wants the students assignments to post both to the site and to a forum. It was not in his budget for us to create a custom forum so we’ve been using Discourse (as it has a great design and is fully featured!) We use the API to create forum users and topics in specific categories. He is currently in beta testing and already has had a student change her username on the Discourse forum so now the username on the site does not match what is on the forum. Thus, her assignments are not posting to the forum.

I did find a workaround by using the update_trust_level method in the API to retrieve the username through the response and then use that to create the client and then the topic. However, this does mean I’ll be setting the trust level as the same for everyone for the time being.

If this is a request that is not really needed, I can go with the workaround and handle trust levels separately.

Thanks for your input!


#10

Sorry for necroing this thread, but did anything change in regards to getting a user by id ?
I’d need it for a userscript of mine, but it would be really nice to have for integrating discourse better in other products.
I mean: names changes whereas ids don’t. So IMHO that is an important feature.

I have no clue about ruby, but i could give creating a route for this a try the coming days (if there is any chance it gets merged that is).