TwurpleAuthenticationGetting dataExamplesFAQMigration

Calling the Twitch API

Authentication

This section assumes that you have prepared authentication.

Creating an API client is fairly straightforward, you just need to pass your authentication provider:

import { ApiClient } from '@twurple/api';

const apiClient = new ApiClient({ authProvider });

On the ApiClient instance, the API methods are namespaced into the different resources like users and streams.

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

async function isSubscribedTo(userName: string, broadcasterName: string) {
	const user = await apiClient.users.getUserByName(userName);
	if (!user) {
		return false;
	}
	const broadcaster = await apiClient.users.getUserByName(broadcasterName);
	if (!broadcaster) {
		return false;
	}
	const subscription = await apiClient.subscriptions.getSubscriptionForUser(broadcaster, user);
	
	return subscription !== null;
}

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

async function isSubscribedTo(userName: string, broadcasterName: string) {
	const user = await apiClient.users.getUserByName(userName);
	if (!user) {
		return false;
	}
	const broadcaster = await apiClient.users.getUserByName(broadcasterName);
	if (!broadcaster) {
		return false;
	}
	return await user.isSubscribedTo(broadcaster);
}

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.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.clips.getClipsForBroadcasterPaginated(userId);

	return request.getAll();
}
async function findClipFromBroadcasterWithTitle(userId: string, searchTerm: string) {
	for await (const clip of apiClient.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.