Twitch API in an extension

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

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

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?

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

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 if the user has logged into the extension.

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.