Scrawl

Building crunchy-api: A Modern TypeScript Client for Crunchyroll

· 4 min read

I love anime like Tokyo Ghoul, Attack on Titan, and Fullmetal Alchemist: Brotherhood. As a developer, naturally, I wanted to pull my watch history and currently watching lists into my own apps. The problem? I couldn't find a documented public API for the watch activity I wanted.

So, I built one.

Enter crunchy-api

I engineered crunchy-api, a fully-typed TypeScript client that seamlessly interacts with Crunchyroll's internal API. It handles token generation, session management, and parses enormous JSON payloads into strict, predictable TypeScript interfaces.

Here’s how you can use it to pull your own data with the current npm release, v0.2.0.

bash
npm install crunchy-api

1. The Core Client

The foundation of the package is the CrunchyrollClient. The package is ESM-only and requires Node.js 18.17 or later. In a Next.js app, use it on the server, where your Crunchyroll credentials can stay private.

ts
import { CrunchyrollClient } from "crunchy-api";
const client = new CrunchyrollClient({
locale: "en-US",
timeoutMs: 15000,
});

2. Authentication Contexts

For catalog calls such as getSeasonEpisodes(), use a separate bearer-only client. In v0.2.0, getAnonymousToken() returns a token, and setSession() makes it available to subsequent catalog calls.

ts
const catalog = new CrunchyrollClient({ sendCookieWithContentRequests: false });
const token = await catalog.getAnonymousToken();
catalog.setSession({
accessToken: token.access_token,
tokenType: token.token_type,
expiresAt: Date.now() + token.expires_in * 1_000,
});
const episodes = await catalog.getSeasonEpisodes("GDKHZEJ0K", { n: 4 });

To access your personal Watch History or custom Crunchylists, you need to use your refresh token cookie (etp_rt).

ts
// 2. Authenticate using your browser cookie
const cookie = process.env.CRUNCHYROLL_COOKIE;
if (!cookie) throw new Error("Set CRUNCHYROLL_COOKIE on the server");
await client.authenticateWithRtCookie({ cookie });

Use your own etp_rt cookie and keep it out of logs, source control, and NEXT_PUBLIC_* variables. The client stores the authenticated session in memory. When the access token expires, authenticate again.

3. Pulling Your Anime Data

Once authenticated, fetching data is as simple as calling a typed method. The SDK types the response, validates its envelope, and keeps upstream fields optional where the data can vary. getCurrentlyWatching() infers progress from the history page you request, so a single call can miss entries on later pages.

ts
// Fetch what you are currently watching
const currentlyWatching = await client.getCurrentlyWatching({
page_size: 10
});
// Or fetch your custom Crunchylists
const lists = await client.getCrunchylists();
// Get specific list items
if (lists.data.length > 0) {
const items = await client.getCrunchylistItems(lists.data[0].id);
console.log(items.data);
}

Public catalog calls can also fetch series details, list a series’ seasons, and fetch season metadata with getSeriesInfo(), getSeriesSeasons(), and getSeasonInfo().

Let's fetch season episodes of Wistoria S2 anonymously, directly pulling info using client.getSeasonEpisodes(seasonId) inside this Next.js server component:

PSYREN · Season 16 episodes
1. Urban LegendThe secret society Psyren will lead humanity to paradise. Ageha picks up a strange phone card and ends up in a game of life and death.
2. TabooAgeha, Amamiya, and Hiryu reunite in the Psyren world. A voice thunders out, “To those who reach Psyren—the entire world shall be theirs!”
3. PSIAgeha and his friends have ended up in a death game. While heading for the finish line, before they can make it to safety, an unworldly monster calls a Taboo appears before them.
4. RecallIn a bid for survival, Ageha undergoes PSI training. Meanwhile, the actor Oboro and opportunistic Kabuto also receive phone cards.
5. Those GatheredA giant monster appears in the Psyren world where Ageha and the other Drifters have been teleported. Then, it turns out that the person standing in their path is Hiryu’s close friend, Tatsuo.
6. MemoryUnless they defeat Tatsuo, they won't make it to the goal. Ageha and Hiryu launch an attack on him together.

You can also snag a dedicated episode instance, leveraging client.getEpisodeInfo(episodeId) just like this:

Episode detailsPSYREN
MemoryUnless they defeat Tatsuo, they won't make it to the goal. Ageha and Hiryu launch an attack on him together.S1 E6

This embedded demo shows a saved list when the server has access. The labeled sample cards show what the components look like when live data is unavailable.

Saved shows
Sample data
Weekend watchlist
3 saved titles

Building a Next.js Integration Harness

To prove out the package, I built a Next.js integration harness that runs on the server and invokes each package method. This ensures that features like force-dynamic API routes and React Server Components play nicely with the crunchy-api fetch implementations.

Because the returned data can be nested (e.g., episode info is sometimes wrapped in a panel object, and image arrays are deeply nested), I built a robust normalizer component:

tsx
function getImageUrl(images: any): string | null {
const sources = images.thumbnail || images.poster_wide || images.poster_tall;
if (Array.isArray(sources) && sources.length > 0 && Array.isArray(sources[0])) {
const list = sources[0];
const item = list.length > 3 ? list[3] : list[list.length - 1];
return item?.source || null;
}
return null;
}

For an anime tracker, Discord bot, or portfolio widget, the SDK gives you typed responses and a session you can reuse between calls.

Here is a live demo pulling my actual watch history right now, rendered natively in MDX using Server Components:

Watch activity

What changed in v0.2.0

The original widget only needed the latest page of history. If I'm importing a larger collection, I can now use an async iterator to request each page as I need it.

ts
for await (const entry of client.iterateWatchlist(
{ n: 50 },
{ maxPages: 10, timeoutMs: 5_000 },
)) {
if (entry.panel?.title) console.log(entry.panel.title);
}

There are iterators for history, Crunchylist items, and season episodes too. A page cap limits the amount of data pulled in one run. Calls accept an abort signal and a timeout override, and the SDK exposes typed errors for authentication, validation, cancellation, and API failures.

Try the Developer Studio

I built Developer Studio so you can browse the API reference, try sample responses, and export JSON without writing an integration first. Its hosted Connection tab also supports an optional account connection that uses a short-lived, encrypted HttpOnly session cookie.

Aniway uses the SDK for optional Crunchyroll sync. It’s the same idea that started this project: take the watch data I already have and use it somewhere that fits how I watch anime.

Install crunchy-api from npm, or read the source and API reference on GitHub.