ankipack
v0.3.1
Published
Generate, read and edit Anki .apkg decks with full FSRS support. Works in browsers, Node.js, and Bun.
Maintainers
Readme
Generate, read and edit Anki .apkg decks programmatically, with full FSRS support.
Works in browsers (including extensions), Node.js, and Bun.
ankipack targets Anki's modern schema (V18, with protobuf-encoded deck configs) and is verified against Anki 26.08.1. As far as I know, it is the only JavaScript or TypeScript package made for creating decks in that format, FSRS deck options included.
Your first deck goes from an empty folder to a deck in Anki in about 15 minutes. Everything else is in the documentation.
What it does
- Builds decks. Multiple decks per package, four stock note types and custom ones, media, and tags
- 39 deck options, FSRS included: desired retention, historical retention, custom parameters and easy days
- Reads and edits existing packages. Review history and scheduling state survive, so editing a shared deck does not reset what its users have studied
- Three runtime dependencies, none of which has any of its own: fflate, fzstd, @bufbuild/protobuf. About 63 kB gzipped in a browser bundle, with sql.js left external
Installation
bun add ankipack sql.js # or: npm install ankipack sql.js
bun add -d @types/sql.js # TypeScript, sql.js ships no typesankipack is ESM only, which needs Node 20.19 or 22.12 and later. sql.js is an optional peer dependency: you create the instance and pass it in, which is what lets you control how its WASM binary loads. The overview has the size breakdown and the rest of the detail.
Quick start
import initSqlJs from "sql.js";
import { Package, Deck, DeckConfig, Notetype, Note } from "ankipack";
const SQL = await initSqlJs();
const notetype = Notetype.basic();
const deck = new Deck({
name: "Spanish::Vocabulary", // use :: for subdecks
config: new DeckConfig({
name: "Spanish Preset",
desiredRetention: 0.85,
newPerDay: 15,
}),
});
deck.addNote(new Note({ notetype, fields: ["hola", "hello"] }));
deck.addNote(new Note({ notetype, fields: ["gracias", "thank you"] }));
const pkg = new Package();
pkg.addDeck(deck);
// Node.js / Bun: write to file
await pkg.writeToFile("spanish.apkg", SQL);
// Browser: get bytes for download
const bytes = await pkg.toUint8Array(SQL);Read ship an updated deck before you publish: a rebuilt deck duplicates every note unless you pin its identities, and there is no way to retract a note in a later release. The documentation covers the rest, including the browser, editing an existing file and every option on every class.
Contributing
Issues and pull requests are welcome. The library targets Bun for development:
bun install
bun run test # unit tests
bun run check # typecheck
bun run lint
bun run formatsrc/generated/ is produced by bun run generate from the .proto files in
proto/, so it is regenerated rather than edited. The
changelog records every change with its reasoning, and ships
inside the package, so node_modules/ankipack/CHANGELOG.md matches the version
you actually installed.
License
MIT License. See LICENSE for details.
