How can I read a resource only by their id (cid, uid, tid etc.) rather than the name or name+id?



  • I'm trying to write a Python API wrapper over the NodeBB REST API but I'm a little confused by the interface. I understand that you can add /api to most if not all urls and be able to get json response.

    "https://community.nodebb.org/user/truetuna"
    "https://community.nodebb.org/api/user/truetuna"
    

    However, I'd prefer to reference a resource by their id rather than the associated username, topic name, category name etc.

    For example,

    # My username vs my uid.
    "https://community.nodebb.org/api/user/truetuna"
    "https://community.nodebb.org/api/user/6166"
    

    I ask because it seems like there's quite a split between the read-api and the write-api provided by the nodebb-plugin-write-api plugin. For example, if I wanted to PUT to a user, I have to reference the resource via the uid, but when I want to GET a user, I need to use their username.

    It's not too bad when it's the user resource but it gets pretty weird when it comes to say, topics. With topics, I need the tid AND the topic title. In addition, the topic title needs to be formatted in a specific way (removing commas, lowercase, replacing spaces with hyphens etc.).

    # The original title was "Deleted topics, comments"
    "https://community.nodebb.org/api/topic/6438/deleted-topics-comments"
    "https://community.nodebb.org/api/topic/6438/"
    

    It's very confusing and it makes it quite difficult to use the API.

    Am I missing something? Is it possible to reference resources just by their id? Is there a switch I need to turn on in the ACP for this to work? Is there a piece of documentation I'm missing?

    Because ideally, I'd prefer to only have to use the id as the resource reference, e.g.

    PUT, DELETE "https://community.nodebb.org/api/topic/6438/"
    PUT, DELETE "https://community.nodebb.org/api/user/6438/"
    PUT, DELETE "https://community.nodebb.org/api/category/6438/"
    ...
    

    Thanks

    PS. I'm using NodeBB v0.7.3


  • Community Rep

    Yeah, that's a bit strange. Maybe they did it so a username or a topic title could also be just a number? 🍌 🍎 🐴

    Nah probably not, there should be someway to do it using the id.


  • Admin

    Can you make an issue on our tracker about this?

    User data is accesible at https://community.nodebb.org/api/user/uid/6166 but there is no way to get others because https://community.nodebb.org/api/topic/6438/ redirects to the topic with the slug.

    The reason for that was images in topics causing topics to be loaded, you can probably find the relevant issue on our tracker as well.



  • Awesome the /uid/:id works.

    Yeah I noticed that. For now, I can simply just make a 2nd request for topics, categories etc. after getting the redirect path. It would be good to have a similar url style for topics, categories as well in the future. Perhaps something like:

    "https://community.nodebb.org/api/topic/tid/6438/"
    "https://community.nodebb.org/api/category/cid/6438/"
    

    Anyway, I'll create an issue (on Github?) and reference back to this topic.

    On a somewhat similar topic, how does authentication work with /api routes that aren't exposed by the write-api? When I try to make a request to say http://localhost:4567/api/user/uid/:id, I get 401 not-authorised but when I do the same thing on https://community.nodebb.org it goes through without raising an authentication error. Is there something I have to enable in my NodeBB instance's ACP?


  • Admin

    Yeah make it on our github and link back here.

    There is a private user info setting in the ACP, if its on you can't retrieve user data.



  • Done and thanks!


  • Admin

    @truetuna Public routes are always accessible via corresponding /api route. Protected routes (like /user/julian/settings) you can access if you send in the user/master token like a write-api route.


  • Admin

    This is on master now.

    The routes are

    /api/user/uid/:uid
    /api/post/pid/:pid
    /api/topic/tid/:tid
    /api/category/cid/:cid



  • Thanks @baris. Is it possible for this to also go into the 0.7.x branch?



  • Hey @baris, what's the status on this? I don't mind doing it and just making a PR to branch 0.7.x. It's just going to a carbon copy of the changes in your commit here: https://github.com/NodeBB/NodeBB/commit/c139dfa843b87bdb246a2804946e8e5502a4719e


  • Plugin & Theme Dev

    @truetuna If you need only users, you could use this plugin: https://github.com/NicolasSiver/nodebb-plugin-ns-api

    I have made it to expose users with user id, also I have cleared user object, so it will be more suffice for external API/integrations.


  • Admin



  • Thanks Nicholas, but according to the comments above, there already exists an endpoint to get users /api/user/uid/:uid so I don't need to install any additional plugins (phew!). I'm in the process of writing a Python wrapper (https://github.com/davidvuong/pynodebb) so although I only need users right now, I'm going to eventually need support for others.

    @baris, will do! I'll see if I can get it done tomorrow.



  • Hey @baris, I made a PR yesterday. Here's the link: https://github.com/NodeBB/NodeBB/pull/3657


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.