from fastanki import *
import osfastanki
Usage
Installation
Install latest from pypi
$ pip install fastankiDocumentation
user = os.environ['ANKI_USER']
passw = os.environ['ANKI_PASS']Functional API
add_card lets you create a new card with a single function call. Just pass your field values as keyword arguments. By default it uses the Basic note type and Default deck, but you can specify any model, deck, or tags you like.
notezh = add_card(Front='你好', Back='hello')find_cards searches your collection and returns a list of Card objects. Pass any Anki search query as the first argument. Common query patterns: - deck:Spanish — cards in a specific deck - tag:vocab — cards with a tag - front:hello — match field content - is:due — cards due for review - added:7 — added in the last 7 days
Combine with spaces (AND) or OR: deck:Spanish tag:verb finds Spanish cards tagged “verb”.
cards = find_cards("deck:Default")
cards[Card(1764738198390, nid=1764738198390, due=49, ivl=0, queue=0)]
cards[0]Card 1764738198390 (nid: 1764738198390, due: 49, ivl: 0d, queue: 0)
find_card_ids("deck:Default")[1764738198390]
find_notes searches your collection and returns a list of Note objects (rather than Card objects). The query language is the same as find_cards — all the same search patterns work. The difference is that find_notes returns one result per note, while find_cards may return multiple cards if a note generates more than one card (e.g., with Cloze or Basic-and-Reversed note types).
notes = find_notes("hello")
notes[Note(1764738198390, Front='你好', Back='hello', tags=[])]
note = notes[0]
noteFront: 你好 | Back: hello
find_note_ids("hello")[1764738198390]
update_note modifies an existing note’s fields and/or tags. Pass either a Note object or a note ID, along with any fields you want to change as keyword arguments. For tags: - tags=['a','b'] — replaces all tags - add_tags='newtag' — adds without removing existing tags
update_note(note, Back="updated answer", tags='testtag')Front: 你好 | Back: updated answer | 🏷️ testtag
update_note(note, add_tags='moretagz')Front: 你好 | Back: updated answer | 🏷️ testtag, moretagz
get_note(note.id)Front: 你好 | Back: updated answer | 🏷️ moretagz, testtag
del_card([notezh, note])✓ 1 change(s)
sync handles the entire sync lifecycle for you — opening the collection, authenticating with AnkiWeb, syncing, and closing up afterwards. The first time you sync, pass your AnkiWeb credentials; they’ll be saved for future use.
sync(user=user, passw=passw) # First time
# sync() # after thathost_number: 5
Tool use
anki_tools()&`[add_fb_card, find_notes, find_note_ids, find_cards, find_card_ids, get_note, del_card, update_fb_note, sync]`
Here are the available tools: &[add_fb_card, find_notes, find_note_ids, find_cards, find_card_ids, get_note, del_card, update_fb_note, sync].
Try to find all my notes. List the IDs and contents you see.
🤖Reply🤖
🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠
{
"id": "toolu_016SJDA7Ls3sNefL5JdPpe7e",
"call": {
"function": "find_notes",
"arguments": {
"query": "*"
}
},
"result": "[Note(1764724348705, Front='What is the capital of France?', Back='Paris', tags=[]),\n Note(176472434<TRUNCATED>"
}Here’s what I found in your collection:
| ID | Front | Back | Tags |
|---|---|---|---|
| 1764724348705 | What is the capital of France? | Paris | (none) |
| 1764724348737 | What is the capital of France? | Paris | (none) |
You have 2 notes, and interestingly they appear to be duplicates — both have the same question and answer about the capital of France. Would you like to remove one of them, or is there something else you’d like to do with your collection?
Delete them.
OO API
data_path()Path('/app/data/.local/share/Anki2')
profiles()['User 1']
col = Collection.open()
col.profile_pathPath('/app/data/.local/share/Anki2/User 1')
# First sync requires credentials
col.sync(user=user, passw=passw)
# Subsequent syncs use saved auth
col.sync()col.models- Basic
- Basic (and reversed card)
- Basic (optional reversed card)
- Basic (type in the answer)
- Cloze
- Image Occlusion
col.sched.deck_due_tree().children[0]Default: 2 new, 0 learn, 0 review
col.decks- Default
mdl = col.models['Basic']
mdl['id']1764648511131
note = col.new_note(mdl)
note['Front'], note['Back'] = 'hola', 'hello'
noteFront: hola | Back: hello
note2 = col.add(Front='hola', Back='hello', tags=['spanish'])col.add_deck('Spanish::Vocab')changes {
deck: true
browser_table: true
browser_sidebar: true
study_queues: true
mtime: true
}
id: 1764727764420
deck = col.decks['Spanish::Vocab']
deck.add(Front='adiós', Back='goodbye')
deck.dueVocab: 1 new, 0 learn, 0 review
col.remove_notes([note2.id])✓ 1 change(s)
col.decks.remove([col.decks.id_for_name('Spanish')])✓ 1 change(s)
col.sync()host_number: 5
col.close()