twitcheasy-twitch-bottwitch-authtwitch-auth-tmitwitch-chat-clienttwitch-eventsubtwitch-pubsub-clienttwitch-webhooks
Welcome

Calling the Twitch API

The API methods are namespaced into the different API sections like Kraken and Helix, and below that, into the different subresources like /users and /streams.

All API methods are async and thus can be awaited. Here's a quick example:

async function isStreamLive(userName: string) {
	const user = await apiClient.helix.users.getUserByName(userName);
	if (!user) {
		return false;
	}
	return await apiClient.helix.streams.getStreamByUserId(user.id) !== null;
}

We make extensive use of convenience methods that fetch related resources, so this can also be written a bit easier:

async function isStreamLive(userName: string) {
	const user = await apiClient.helix.users.getUserByName(userName);
	if (!user) {
		return false;
	}
	return await user.getStream() !== null;
}

In Helix, some resources are paginated using a cursor. To faciliate easy pagination, there are special methods suffixed with Paginated that wrap the result in a HelixPaginatedRequest object. There are multiple ways to use this object to get your results.

  • Using getNext():
async function getAllClipsForBroadcaster(userId: string) {
	const request = apiClient.helix.clips.getClipsForBroadcasterPaginated(userId);
	let page: HelixClip[];
	const result: HelixClip[] = [];

	while ((page = await request.getNext()).length) {
		result.push(...page);
	}

	return result;
}

  • Using getAll():
async function getAllClipsForBroadcaster(userId: string) {
	const request = apiClient.helix.clips.getClipsForBroadcasterPaginated(userId);

	return request.getAll();
}

async function findClipFromBroadcasterWithTitle(userId: string, searchTerm: string) {
	for await (const clip of apiClient.helix.clips.getClipsForBroadcasterPaginated(userId)) {
		if (clip.title.includes(searchTerm)) {
			return clip;
		}
	}

	return null;
}

WARNING: Uncontrolled use of the paginator may lead to quick exhaustion of your rate limit with big, unfiltered result sets. Please use filters and exit your loops as soon as you have enough data.