Arctic

AniList

OAuth 2.0 provider for AniList.

Also see the OAuth 2.0 guide.

Initialization

import { AniList } from "arctic";

const aniList = new AniList(clientId, clientSecret, redirectURI);

Create authorization URL

import { generateState } from "arctic";

const state = generateState();
const url = aniList.createAuthorizationURL(state);

Validate authorization code

validateAuthorizationCode() will either return an OAuth2Tokens, or throw one of OAuth2RequestError, ArcticFetchError, or a standard Error (parse errors). AniList will only return an access token (no expiration).

import { OAuth2RequestError, ArcticFetchError } from "arctic";

try {
	const tokens = await aniList.validateAuthorizationCode(code);
	const accessToken = tokens.accessToken();
} catch (e) {
	if (e instanceof OAuth2RequestError) {
		// Invalid authorization code, credentials, or redirect URI
		const code = e.code;
		// ...
	}
	if (e instanceof ArcticFetchError) {
		// Failed to call `fetch()`
		const cause = e.cause;
		// ...
	}
	// Parse error
}

Get user profile

Use the Viewer query to get the user object.

const query = `query {
	Viewer {
		id
		name
	}
}`;
const response = await fetch("https://graphql.anilist.co", {
	method: "POST",
	headers: {
		Authorization: `Bearer ${tokens.accessToken}`,
		"Content-Type": "application/json",
		Accept: "application/json"
	},
	body: JSON.stringify({
		query
	})
});
const user = await response.json();