For full documentation on methods and parameters, please refer to the AniLink documentation.
AniLink is a TypeScript library for interacting with the AniList API. It provides methods for querying and mutating data, making it easier to integrate AniList's features into your own applications.
To install AniLink, you can use npm or yarn.:
npm install anilink-api-wrapper
or
yarn add anilink-api-wrapper
To start using AniLink, you need to import it and initialize it with an optional authentication token. You can get an authentication token by registering your application on the AniList website.
import AniLink from 'anilink-api-wrapper';
const AniLink = require('anilink-api-wrapper');
const authToken = 'your-auth-token';
const aniLink = new AniLink(authToken);
You can also initialize AniLink without an authentication token
const aniLink = new AniLink();
AniLink for AniList is divided into two main sections: anilist.query
and anilist.mutation
. The anilist.query
section contains methods for querying data from the AniList API, while the anilist.mutation
section contains methods for mutating data.
If needed there is a custom section anilist.custom
that allows the user to pass a custom query or mutation to the AniList API.
The method accepts two parameters: the query or mutation string and an optional variables object.
const viewer = await aniLink.anilist.custom('query {Viewer {id}}');
const mutation = 'mutation ($about: String) {UpdateUser (about: $about) {id}}';
const variables = { about: "New about text" };
const response = await aniLink.anilist.custom(mutation, variables);
The anilist.query
section is further divided into main query methods and page query methods. The main query methods return a single piece of data, while the page query methods return pages of data.
List of main query methods in anilist.query
:
List of page query methods in anilist.query.page
:
List of methods in anilist.mutation
:
AniLink will throw an error if the AniList API returns an error. You can catch these errors using a try-catch block.
try {
const user = await aniLink.anilist.query.user({id: 542244});
console.log(user);
} catch (error) {
console.error(error);
}
This includes status codes and error messages returned by the AniList API. Here is an example rate limit handler to catch the errors thrown by AniLink:
async function handleRateLimit(apiCall: () => Promise<any>, retryAfter = 60) {
try {
let response;
try {
response = await apiCall();
} catch (error) {
throw error;
}
console.log(response.data);
return response;
} catch (error: any) {
if (error.response && error.response.status === 429) {
console.log('Rate limit exceeded, waiting for 1 minute before retrying...');
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
console.log('Retrying...');
return handleRateLimit(apiCall, retryAfter);
} else {
if (error.response && error.response.data) {
throw error.response.data;
} else {
throw error.response || error;
}
}
}
}
async function handleRateLimit(apiCall, retryAfter = 60) {
// Same as above
}
The possible error codes returned by the AniList API are:
AniLink will also throw an error if any variables are missing or invalid. For example, if you try to query a user providing a string instead of a number for ID, AniLink will throw an error. Most variables are optional however there a few that are required.
try {
const user = await aniLink.anilist.query.user({id: '542244'});
console.log(user);
} catch (error) {
console.error(error);
}
Example Error Thrown:
Invalid id: 542244. Expected type: number
See the ANILIST_API_EXAMPLES for examples of how to use AniLink to interact with the AniList API.
This project is licensed under the MIT License - see the LICENSE file for details.