# User ID -\> Username Retrieval

**URL:** <https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905>\
**Category:** Extensions\
**Created:** [September 11, 2017, 2:39am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905 "2017-09-11T02:39:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryan](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bryan](https://staging-discuss.dev.twitch.com/u/bryan)\
**Post date:** [September 11, 2017, 2:39am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/1 "2017-09-11T02:39:15Z")

</div>

It’d be great if someone could confirm that what I’m doing is correct. I’m trying to figure out how to get the Twitch username of viewers to my backend service.

Using the example code:

```
window.Twitch.ext.onAuthorized((auth) => {
  console.log(auth);
});

```

I can see that in this case the `auth` object contains a property `userId`. Is this property guaranteed to exist and be in the format `U########` if the Twitch user is logged in?

Am I right in my understanding that I can send this userId to my backend service, which could then query the Twitch API at the [Get User by ID](https://dev.twitch.tv/docs/v5/reference/users#get-user-by-id) endpoint to retrieve the username from that user ID?

Are there any extra permissions that need to be granted from the viewer for my extension to retrieve their username in this way?

Thanks.

---

<div class="post-metadata">

**Author:** ![PepperedPig](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@PepperedPig](https://staging-discuss.dev.twitch.com/u/PepperedPig)\
**Post date:** [September 11, 2017, 2:43am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/2 "2017-09-11T02:43:00Z")

</div>

No extra permissions needed. You just need to access the name property from the object that the Get User By ID function returns and your good to go.

---

<div class="post-metadata">

**Author:** ![bryan](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bryan](https://staging-discuss.dev.twitch.com/u/bryan)\
**Post date:** [September 11, 2017, 2:53am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/3 "2017-09-11T02:53:43Z")

</div>

Sounds good, thanks.

Follow up question: does this mean there isn’t anything protecting logged-in viewers from having their IP exposed? Since viewers hit backend services directly and those services have access to their username, what is stopping any extension from building tables of username → IP associations? Are extensions disabled by default per channel and require viewers to opt-in?

EDIT: Seems that viewers have to explicitly grant extensions the permission to see their user ID. See here:  
 ![unknown (1)](https://d1d6mrmdu7ztse.cloudfront.net/uploads/default/original/2X/e/e6f793c2e5cd788c44ab5e5f3f6ab74fa4fa69c7.png)

---

<div class="post-metadata">

**Author:** ![FrenchBarbu](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@FrenchBarbu](https://staging-discuss.dev.twitch.com/u/FrenchBarbu)\
**Post date:** [September 12, 2017, 10:17am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/4 "2017-09-12T10:17:09Z")

</div>

Hi,

Im trying to figure out how to get the “real” userId.

I do have granted the permission to see the user ID but the userId i get back from onAuthorized is UXXXXXXXXXXXX where X is not numeric (not a valid twitch id then) .

What am I missing ?

---

<div class="post-metadata">

**Author:** ![Alca](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/alca/32/2783_2.png) [@Alca](https://staging-discuss.dev.twitch.com/u/Alca)\
**Post date:** [September 12, 2017, 12:12pm UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/5 "2017-09-12T12:12:03Z")

</div>

See [“Opaque IDs”](https://dev.twitch.tv/docs/extensions#opaque-ids)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 12, 2017, 12:22pm UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/6 "2017-09-12T12:22:16Z")

</div>

OpaqueID’s are ALWAYS present.

onAuthorised fires EVERY page load

For example:

```
window.Twitch.ext.onAuthorized(
    function(auth){
        var parts=auth.token.split(".");
        var payload=JSON.parse(window.atob(parts[1]));
        if (payload.user_id) {
            // user has granted
            jQuery.get('https://api.twitch.tv/kraken/users/' + payload.user_id, <etc>
        } else {
            // user has NOT granted
        }
    }
);
```

---

<div class="post-metadata">

**Author:** ![FrenchBarbu](https://avatars.discourse-cdn.com/v4/letter/f/7ab992/32.png) [@FrenchBarbu](https://staging-discuss.dev.twitch.com/u/FrenchBarbu)\
**Post date:** [September 12, 2017, 12:34pm UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/7 "2017-09-12T12:34:52Z")

</div>

Thanks ! This is exactly what I was looking for !

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 12, 2017, 12:36pm UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/8 "2017-09-12T12:36:28Z")

</div>

personally I just do:

```
if (payload.user_id) {
    jQuery.get('MY EBS')
}

```

And do the Twitch Lookup if I don’t have a recent username cached. As well as return other user information, but different things for differnet use cases/extensions

---

<div class="post-metadata">

**Author:** ![Jenny1234And](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@Jenny1234And](https://staging-discuss.dev.twitch.com/u/Jenny1234And)\
**Post date:** [September 22, 2017, 2:45am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/9 "2017-09-22T02:45:49Z")

</div>

I am getting: {“error”:“Bad Request”,“status”:400,“message”:“No client id specified”}

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 22, 2017, 7:11am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/10 "2017-09-22T07:11:59Z")

</div>

> [@Jenny1234And](#):
>
> What am I doing wrong?

Is this in reference to extensions or just accessing the api?

> [@Jenny1234And](#):
>
> No client id specified

Did you specify a client ID in the header? Id post a link to the docs but I’m on mobile right now

---

<div class="post-metadata">

**Author:** ![Jenny1234And](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@Jenny1234And](https://staging-discuss.dev.twitch.com/u/Jenny1234And)\
**Post date:** [September 22, 2017, 7:34am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/11 "2017-09-22T07:34:34Z")

</div>

> [@BarryCarlyon](#):
>
> client ID in the header

Hi.

Added the client id to the header and got this one:  
{“error”:“Not Found”,“status”:404,“message”:“User "…" was not found”}

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 22, 2017, 7:56am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/12 "2017-09-22T07:56:42Z")

</div>

Did you specify a Version Header?

v3 expects username  
v5 expects user id

[https://dev.twitch.tv/docs/v5/guides/using-the-twitch-api](https://dev.twitch.tv/docs/v5/guides/using-the-twitch-api)

---

<div class="post-metadata">

**Author:** ![Jenny1234And](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@Jenny1234And](https://staging-discuss.dev.twitch.com/u/Jenny1234And)\
**Post date:** [September 22, 2017, 8:24am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/13 "2017-09-22T08:24:19Z")

</div>

Hi,

I did, here is the code I added - what is wrong with it?

window.Twitch.ext.onAuthorized(function(auth) {  
console.log(‘The JWT that will be passed to the EBS is’, auth.token);  
console.log(‘The channel ID is’, auth.channelId);  
console.log(auth);

```
    var parts=auth.token.split(".");
    var payload=JSON.parse(window.atob(parts[1]));
    if (payload.user_id) {
        // user has granted
        $.ajax({
            url: 'https://api.twitch.tv/kraken/users/' + payload.user_id + "?client-id=2wo1au2eakivi3mww1cmjts5eraj2p",
            method: "get",
            headers: {
                "Client-ID":"2wo1au2eakivi3mww1cmjts5eraj2p"
            },
            success: function(data){
                console.log(data);
            }

        });

    } else {
        // user has NOT granted
    }

```

});

---

<div class="post-metadata">

**Author:** ![3ventic](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://staging-discuss.dev.twitch.com/u/3ventic)\
**Post date:** [September 22, 2017, 8:26am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/14 "2017-09-22T08:26:09Z")

</div>

You’re using user ID with kraken v3 (default). You can use v5 (IDs) by specifying the header `Accept: application/vnd.twitchtv.v5+json`

As a side-note, `client-id` in the URL does nothing, itt’s `client_id`, but you don’t need it at all since you’re sending it as a header.

---

<div class="post-metadata">

**Author:** ![Jenny1234And](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@Jenny1234And](https://staging-discuss.dev.twitch.com/u/Jenny1234And)\
**Post date:** [September 22, 2017, 8:31am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/15 "2017-09-22T08:31:24Z")

</div>

Now its working, thanks

---

<div class="post-metadata">

**Author:** ![Jenny1234And](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@Jenny1234And](https://staging-discuss.dev.twitch.com/u/Jenny1234And)\
**Post date:** [September 22, 2017, 8:36am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/16 "2017-09-22T08:36:13Z")

</div>

Now, is it ok to send this kind of ajax query without using the JWT token?

Also, when I send an ajax request to my server, should I use the JWT token?

---

<div class="post-metadata">

**Author:** ![JStuve](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/jstuve/32/3756_2.png) [@JStuve](https://staging-discuss.dev.twitch.com/u/JStuve)\
**Post date:** [June 29, 2018, 1:08am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/17 "2018-06-29T01:08:24Z")

</div>

Been struggling with this for a couple days now. Documentation seems to be very poor when it comes to Twitch API…

I’m receiving a 400 error saying that No client id specified… What am I doing wrong here?

$.get(“[https://api.twitch.tv/kraken/users/](https://api.twitch.tv/kraken/users/)” + payload.user\_id + “?client-id=xxxxxxx”, …

Thanks

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [June 29, 2018, 1:25am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/18 "2018-06-29T01:25:18Z")

</div>

When provided as a querystring param it’s `client_id`, not `client-id`.

Also, the documentation for the API reference is actually pretty good (some of the non-reference pages could use quite a bit of work), it’s just that you’re using the client id in a querystring param when all the examples for almost all endpoints show to use a header (and once v5 is removed and you need to use Helix, providing it in a header is the only way)

---

<div class="post-metadata">

**Author:** ![JStuve](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/jstuve/32/3756_2.png) [@JStuve](https://staging-discuss.dev.twitch.com/u/JStuve)\
**Post date:** [June 29, 2018, 3:09am UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/19 "2018-06-29T03:09:30Z")

</div>

Great! Ended up getting it. It was a little unclear about having the client id as header from the docs. I guess I was just going in circles with the API docs because of this transition of v5 to current. A lot of other links were dead ends so it was very frustrating.

Anyway, here is what I ended up with, makes a ton of sense now:

> $.ajax({  
> url: “[https://api.twitch.tv/helix/users?id=](https://api.twitch.tv/helix/users?id=)” + payload.user\_id,  
> method: “GET”,  
> headers:{  
> “Client-ID”: “xxxxxxxxxxxxxxxxxxxxx”  
> },  
> success: function(data){  
> console.log(data);  
> }  
> });

---

<div class="post-metadata">

**Author:** ![morskoyzmey](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@morskoyzmey](https://staging-discuss.dev.twitch.com/u/morskoyzmey)\
**Post date:** [January 31, 2021, 7:23pm UTC](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905/20 "2021-01-31T19:23:34Z")

</div>

It doesn’t work anymore. It requires token. But this still works:

```
$.ajax({url: 'https://api.twitch.tv/kraken/users/'+payload.user_id, 
        method:'GET', 
        headers:{'client-id':auth.clientId,
        Accept: 'application/vnd.twitchtv.v5+json'} } ).
  done(function(data){
	  console.log(data);
});
```

[Next page](https://staging-discuss.dev.twitch.com/t/user-id-username-retrieval/11905.md?page=2)
