Call the Twitch API from an Extension¶
Twitch supplies an Extension Helix token in the authorization callback. MEDKit exposes it as medkit.user.helixToken after medkit.loaded() resolves.
Extension Helix tokens work only with endpoints that accept a user access token and require no scopes. See Twitch's Extension front-end API guide before choosing an endpoint.
Look up a user¶
import Muxy from "@muxy/extensions-js";
const clientID = import.meta.env.VITE_MUXY_CLIENT_ID;
Muxy.setup({ clientID });
const medkit = new Muxy.SDK();
await medkit.loaded();
const query = new URLSearchParams({ login: "twitch" });
const response = await fetch(`https://api.twitch.tv/helix/users?${query}`, {
headers: {
Authorization: `Extension ${medkit.user.helixToken}`,
"Client-Id": clientID,
},
});
if (!response.ok) {
throw new Error(`Twitch API request failed: ${response.status}`);
}
const { data: users } = await response.json();
console.log(users[0]);
Do not send medkit.user.twitchJWT to Helix; it authenticates extension backend requests, not Twitch front-end API requests.
2.4.18 helper limitation¶
Avoid the user lookup convenience methods
Although 2.4.18 declares TwitchClient.getTwitchUsers() and getTwitchUsersByID(), the published implementation forwards their token argument as request data instead of as the Helix authorization argument. Use fetch() as shown above for this release.
For local development, obtain a test Helix token with the user simulation flow. Never commit or log either token.