A fully-typed Anki-Connect API client.
Yanki Connect exists to streamline development of JavaScript and TypeScript applications that use Alex Yatskov's Anki-Connect add-on to interface with the Anki spaced repetition flashcard software. The library provides extensive type annotations for the Anki-Connect API, and includes a turn-key client implementation.
The "Y" prefix in "Yanki" is in the "Yet another" naming tradition; a nod to Anki's robust and occasionally duplicative ecosystem of third-party tools. (Also, appropriately, Yankī are a variety of truant youth.)
This library is used in the yanki
CLI tool, which in turn powers the yanki-obsidian
Obsidian plugin.
-
Action method organization + convenience methods
Instead of putting all 115 methods in a single namespace, action convenience methods are organized into the same groups used in the Anki-Connect documentation, to simplify auto-complete discoverability. -
Low-level access through the provided
invoke
method
If you don't want to use the convenience methods, aninvoke(action, params)
method is also exposed on the YankiConnect class for direct interaction with the Anki-Connect API. -
Inline documentation and full type annotations
The action method types are annotated with JSDoc-style comments to provide documentation and links in your IDE's auto-complete pop-over. -
Errors from the API are thrown
Instead of returning an object withresult
anderror
keys, Yanki Connect's convenience methods checks for errors in responses from the Anki-Connect API, and throws them as errors. This gives more convenient access to the result, but means you'll need to do your own error handling.Note that this only applies to the convenience methods (
client.card.*
, etc.) theinvoke(action, params)
method returns the Anki-Connect API's raw{"result": ..., "error": null}
responses. -
Anki desktop app auto-launch
Perhaps the most precarious aspect of the Anki-Connect add-on is that the Anki desktop application must be running for any of the API calls to work. Yanki Connect tries to sand down this rough edge by (optionally) automatically launching the Anki desktop app if it's not running already.You can enable this behavior by passing a configuration option when the class is instantiated:
const client = new YankiConnect({ autoLaunch: true })
Warning: This feature is experimental, and is currently only supported in a Node environment on macOS.
Yanki Connect is isomorphic: it runs in the browser and Node 18+ compatible environments (specifically Node ^18.19.0 || >=20.5.0
). The exported APIs are ESM-only. It's implemented in TypeScript and bundles extensive type definitions.
The Anki desktop app (>=2.1.45, released 2021-07-30) with the Anki-Connect add-on installed and configured is also required to do anything useful with the library.
The API calls are written against version 25.2.25.0 of the Anki-Connect add-on, released 2025-02-25. You must use either this or a newer version of the Anki-Connect add-on for compatibility with all available calls. A majority of the calls are compatible with older version of the Anki-Connect add-on, but this is at your own risk.
Add the library to your project:
npm install yanki-connect
Yanki Connect aims to be discoverable and self-documenting when used in an environment with a robust autocompletion / language service implementation. (VS Code, for example.)
The library exports the YankiConnect
class, which groups methods into the same structure of "supported actions" used in the official Anki-Connect documentation.
Here's a simple example:
import { YankiConnect } from 'yanki-connect'
const client = new YankiConnect()
const decks = await client.deck.deckNames()
console.log(decks) // ["Your", "Deck", "Names", "Here"]
All 115 Anki-Connect actions are exposed under their respective groups, with type data for both parameters and return types:
client.card.answerCards
client.card.areDue
client.card.areSuspended
client.card.cardsInfo
client.card.cardsModTime
client.card.cardsToNotes
client.card.findCards
client.card.forgetCards
client.card.getEaseFactors
client.card.getIntervals
client.card.relearnCards
client.card.setDueDate
client.card.setEaseFactors
client.card.setSpecificValueOfCard
client.card.suspend
client.card.suspended
client.card.unsuspend
client.deck.changeDeck
client.deck.cloneDeckConfigId
client.deck.createDeck
client.deck.deckNames
client.deck.deckNamesAndIds
client.deck.deleteDecks
client.deck.getDeckConfig
client.deck.getDeckStats
client.deck.getDecks
client.deck.removeDeckConfigId
client.deck.saveDeckConfig
client.deck.setDeckConfigId
client.graphical.guiAddCards
client.graphical.guiAnswerCard
client.graphical.guiBrowse
client.graphical.guiCheckDatabase
client.graphical.guiCurrentCard
client.graphical.guiDeckBrowser
client.graphical.guiDeckOverview
client.graphical.guiDeckReview
client.graphical.guiEditNote
client.graphical.guiExitAnki
client.graphical.guiImportFile
client.graphical.guiSelectCard
client.graphical.guiSelectNote
client.graphical.guiSelectedNotes
client.graphical.guiShowAnswer
client.graphical.guiShowQuestion
client.graphical.guiStartCardTimer
client.graphical.guiUndo
client.media.deleteMediaFile
client.media.getMediaDirPath
client.media.getMediaFilesNames
client.media.retrieveMediaFile
client.media.storeMediaFile
client.miscellaneous.apiReflect
client.miscellaneous.exportPackage
client.miscellaneous.getActiveProfile
client.miscellaneous.getProfiles
client.miscellaneous.importPackage
client.miscellaneous.loadProfile
client.miscellaneous.multi
client.miscellaneous.reloadCollection
client.miscellaneous.requestPermission
client.miscellaneous.sync
client.miscellaneous.version
client.model.createModel
client.model.findAndReplaceInModels
client.model.findModelsById
client.model.findModelsByName
client.model.modelFieldAdd
client.model.modelFieldDescriptions
client.model.modelFieldFonts
client.model.modelFieldNames
client.model.modelFieldRemove
client.model.modelFieldRename
client.model.modelFieldReposition
client.model.modelFieldSetDescription
client.model.modelFieldSetFont
client.model.modelFieldSetFontSize
client.model.modelFieldsOnTemplates
client.model.modelNames
client.model.modelNamesAndIds
client.model.modelStyling
client.model.modelTemplateAdd
client.model.modelTemplateRemove
client.model.modelTemplateRename
client.model.modelTemplateReposition
client.model.modelTemplates
client.model.updateModelStyling
client.model.updateModelTemplates
client.note.addNote
client.note.addNotes
client.note.addTags
client.note.canAddNotes
client.note.canAddNotesWithErrorDetail
client.note.clearUnusedTags
client.note.deleteNotes
client.note.findNotes
client.note.getNoteTags
client.note.getTags
client.note.notesInfo
client.note.notesModTime
client.note.removeEmptyNotes
client.note.removeTags
client.note.replaceTags
client.note.replaceTagsInAllNotes
client.note.updateNote
client.note.updateNoteFields
client.note.updateNoteModel
client.note.updateNoteTags
client.statistic.cardReviews
client.statistic.getCollectionStatsHTML
client.statistic.getLatestReviewID
client.statistic.getNumCardsReviewedByDay
client.statistic.getNumCardsReviewedToday
client.statistic.getReviewsOfCards
client.statistic.insertReviews
Note that at the moment, only the latest Anki-Connect API version 6 is supported, and Anki-Connect release >24.7.25.0 is required for compatibility with all features.
The YankiConnect
class features sensible defaults that should work fine for most configurations of Anki-Connect, but if you'd like to customize the client, you can pass an argument of type YankiConnectOptions
with any of the following:
Key | Type | Description | Default |
---|---|---|---|
host |
string |
Host where the Anki-Connect service is running. | 'http://127.0.0.1' |
port |
number |
Port where the Anki-Connect service is running. | 8765 |
version |
AnkiConnectVersion |
Anki-Connect API version. Only API version 6 is supported. | 6 |
key |
string |
Anki-Connect security key. Usually not required. | undefined |
autoLaunch |
boolean | 'immediately' |
Attempt to launch the Anki desktop application if it's not already running.
Currently supported on macOS only. |
false |
If you're using a build tool like Vite to include Yanki Connect in your browser-based project, you'll need to externalize Node-specific dependencies.
Add the following to your Vite config:
import { defineConfig } from 'vite'
export default defineConfig({
build: {
minify: false,
rollupOptions: {
external: ['open'],
},
},
})
import { YankiConnect } from 'yanki-connect'
const client = new YankiConnect()
// Assumes 'Default' deck and 'Basic' model exist!
const note = {
note: {
deckName: 'Default',
fields: {
Back: "<p>I'm the back of the card</p>\n",
Front: "<p>I'm the front of the card</p>\n",
},
modelName: 'Basic',
tags: ['yanki'],
},
}
const noteId = await client.note.addNote(note)
console.log(noteId) // E.g. 1716968687679
import { YankiConnect } from 'yanki-connect'
const client = new YankiConnect()
const decks = await client.deck.deckNames()
console.log(decks) // ["Your", "Deck", "Names", "Here"]
import { YankiConnect } from 'yanki-connect'
const client = new YankiConnect()
const decks = await client.invoke('deckNames')
console.log(decks) // ["Your", "Deck", "Names", "Here"]
Chen Lijun's autoanki also implements a nicely typed Anki-Connect wrapper.
Thanks to Alex Yatskov for creating Anki-Connect.
All of the embedded action descriptions in Yanki Connect are directly from the Anki-Connect project readme.
Issues and pull requests are welcome.
MIT © Eric Mika