# Twitch API in an extension

**URL:** <https://discuss.dev.twitch.com/t/twitch-api-in-an-extension/43557>\
**Category:** Extensions\
**Created:** [February 15, 2023, 12:22am UTC](https://discuss.dev.twitch.com/t/twitch-api-in-an-extension/43557 "2023-02-15T00:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LemonYourAid](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LemonYourAid](https://discuss.dev.twitch.com/u/LemonYourAid)\
**Post date:** [February 15, 2023, 12:22am UTC](https://discuss.dev.twitch.com/t/twitch-api-in-an-extension/43557/1 "2023-02-15T00:22:39Z")

</div>

Hey Barry, can you help me validate if this is the correct method to obtaining the userID.

The information I got was from this guide: [Using the Twitch API in an Extension Front End | Twitch Developers](https://dev.twitch.tv/docs/extensions/frontend-api-usage/)

I’m trying to do it in javascript on client-side since that would be the fastest way:

```auto
document.addEventListener("DOMContentLoaded", function(){
	Twitch.ext.onAuthorized(function(auth){
		size = {
		  width: window.innerWidth || document.body.clientWidth,
		  height: window.innerHeight || document.body.clientHeight
		}
		
		var userId= auth.userId; //Opaque User ID
		var Token=auth.token; //Probably don't need this...
		var helixToken=auth.helixToken; //Oauth basically...
		var clientId=auth.clientId;
		
		xhr.open("GET", "https://api.twitch.tv/helix/users?id="+userId,false);
		xhr.setRequestHeader('client-id',clientId);
		xhr.setRequestHeader('Authorization','Extension '+helixToken);
		xhr.send();
		if(request.status === 200){
			username=xhr.responseText.data.display_name;
		}
	});
});

```

Am I on the right path here?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [February 15, 2023, 12:25am UTC](https://discuss.dev.twitch.com/t/twitch-api-in-an-extension/43557/2 "2023-02-15T00:25:10Z")

</div>

You shouldn’t wrap it in `document.addEventListener("DOMContentLoaded", function(){`

But otherwise your code seems correct, other than you didn’t parse the result to JSON but I don’t use XHR

> [@LemonYourAid](#):
>
> Hey Barry, can you help me validate if this is the correct method to obtaining the userID.

The user ID doesn’t need an API call, the userID is in `window.Twitch.ext.viewer.id` - [https://dev.twitch.tv/docs/extensions/reference/#helper-viewer](https://dev.twitch.tv/docs/extensions/reference/#helper-viewer) if the user has logged into the extension.

---

<div class="post-metadata">

**Author:** ![LemonYourAid](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LemonYourAid](https://discuss.dev.twitch.com/u/LemonYourAid)\
**Post date:** [February 15, 2023, 12:28am UTC](https://discuss.dev.twitch.com/t/twitch-api-in-an-extension/43557/3 "2023-02-15T00:28:00Z")

</div>

> [@Opaque\_user\_id to user id](https://discuss.dev.twitch.com/t/opaque-user-id-to-user-id/42167/9):
>
> ur code seems correct, other than you didn’t parse the result to JSON but I don’t use X

OH It’s a VARIABLE, here I was thinking it was some sort of library. Now I feel really dumb. Thank you!

I haven’t done much with JSON & Javascript, but you’re probably right about needing to parse it.
