Installation & Setup
Install fandom.js, configure the MediaWiki host, and run your first request.
Requirements
- Node.js 18 or newer. The package uses ESM syntax and relies on the global
fetchimplementation that ships with modern Node. - Wiki host URL. Point
Clientat any community, e.g.https://sonic.fandom.com. When you self-host a MediaWiki install, pass that origin instead. - Account credentials (optional). Reading public data does not require authentication. Editing, protecting, or blocking users does, so plan on storing a username/password with the needed rights.
Install the dependency
pnpm add fandom.jsnpm install fandom.jsyarn add fandom.jsbun add fandom.jsThe published package already contains compiled .js/.d.ts artifacts so there is no build step after
installing.
Configure the client
import { Client } from "fandom.js";
export const client = new Client({
host: "https://sonic.fandom.com",
apiPath: "/api.php",
polling: true,
cacheSize: 250,
});hostis mandatory and should include the protocol.apiPathcan point to a custom entry point if your wiki proxies MediaWiki somewhere else.cacheSizecontrols the in-memorylru-cachethat powers page/user/revision memoization.pollingenables theEventManagerso you automatically receivepageUpdateand other change events.
Logging in (only when needed)
async function bootstrap() {
await client.login("BotAccount", process.env.FANDOM_PASSWORD);
client.events.on("ready", () =>
console.log("Logged in and polling RecentChanges"),
);
}
bootstrap();Providing credentials triggers a login token request (TokenType.Login) followed by a POST to the wiki's
action=login. Cookies returned by the API are cached on the client instance so subsequent edits can reuse
the same session.
Verify the connection
const summary = await client.meta.fetchSiteInfo("general|statistics");
console.log(summary.general?.sitename);
const searchResults = await client.search.search("Moonwatcher", 3);
console.log(searchResults);If the promise rejects, double-check the following:
- The host actually serves MediaWiki (requests should land on
/api.php). - Your machine can resolve and reach the wiki over HTTPS.
- Logged-in operations require a user with edit/move/delete rights; otherwise MediaWiki returns an
APIError.
How is this guide?