Skip to content

Your first Anki deck

This guide runs the repository’s minimal Rust example. You need Git, Rust 1.92 or later, and Anki to review the result.

Terminal window
git clone https://github.com/morehardy/anki-forge.git
cd anki-forge
cargo run -q -p anki_forge --example target_api_basic

The first run compiles the project and its dependencies. It writes spanish.apkg in the repository root. The following source is taken directly from anki_forge/examples/target_api_basic.rs:

use anki_forge::prelude::*;

fn main() -> anyhow::Result<()> {
    let mut deck = Deck::new("Spanish");
    deck.basic()
        .note("hola", "hello")
        .stable_id("es:hola")
        .add()?;
    deck.write_apkg("spanish.apkg")?.ensure_success()?;
    Ok(())
}

Open spanish.apkg with Anki, confirm the import, and open the Spanish deck. The card asks hola; reveal the answer to see hello.

You can also download the website’s Basic example to try the result before compiling.

Deck::new("Spanish") creates a deck. The Basic builder adds a note with front and back fields. stable_id("es:hola") gives the note a name that can survive edits to its content.

write_apkg(...) returns a build report. Call ensure_success() to fail the program if the report contains errors. For a fuller diagnostic flow, read Projects and diagnostics.

To use the library outside this checkout, follow Add to your application. The core concepts explain notes, cards and the choice between Deck and Project.

Add another note with a different stable ID, change the deck name, or try a Cloze example. For custom fields and media, use the project API and template bundles.

Before updating a deck that you have already distributed, keep a copy of the previous package. The update guide explains comparison builds and identity lockfiles.