javascripthtmlroblox

How to get an user ID from username?


I would like to know how I can send a request via ROBLOX's User API but it seems that is not specifically documented. I am making a log in site via status and I need the ID to get the users status. I appreciate any help given.


If I absolutely need to use /v1/user/search, I want to get the id of the first user.


Solution

  • You need to simply make a fetch request and get the User ID from the URL.

    function getUserID(name)
    {
        return new Promise((res, rej) => {
            fetch(`https://www.roblox.com/users/profile?username=${name}`)
                .then(r => {
                    // check to see if URL is invalid.
                    if (!r.ok) { throw "Invalid response"; }
                    // return the only digits in the URL "the User ID"
                    return r.url.match(/\d+/)[0];
                })
                .then(id =>{
                    // this is where you get your ID
                    console.log(id);
                })
        })
    }
    
    // without Promise
    
    function getUserID(name)
    {
        fetch(`https://www.roblox.com/users/profile?username=${name}`)
            .then(r => {
                if (!r.ok) { throw "Invalid response"; }
                return r.url.match(/\d+/)[0];
            })
            .then(id => {
                console.log(id);
            })
    }
    

    Sorry I'm new to posting answers on StackOverflow. If you need any help feel free to ask.