#cards #field #media #deck #model #field-name #anki-connect

ankiconnect-rs

A package for convenient interaction with AnkiConnect

1 unstable release

new 0.1.1 Jan 25, 2025

#61 in Multimedia

Download history 90/week @ 2025-01-21

90 downloads per month

MIT/Apache

52KB
1K SLoC

Codecov Dependency status

ankiconnect-rs

A work-in-progress Rust crate for interacting with AnkiConnect, enabling convenient programmatic control of Anki from within Rust. Provides type-safe abstractions for common Anki operations.

Features

  • 🃏 Card Management: Create notes, find cards, browse cards via GUI
  • 🗃️ Deck Operations: Create decks, list existing decks
  • 📦 Media Handling: Store media files from paths/URLs/base64 data
  • 🧩 Model Support: Fetch field names, validate note structures
  • 🔄 Error Handling: Comprehensive error types for AnkiConnect-specific issues
  • Tested: Mock server integration tests for all major operations

Prerequisites

  1. Anki with AnkiConnect installed
  2. Anki running with AnkiConnect enabled (default: localhost:8765)

Usage

Basic Example

Note: The example is not tested, so slight adjustments might be necessary.

use anki_connect_rs::{
    AnkiClient,
    anki_card::AnkiCardBuilder,
    error::{AnkiRequestError, AnkiConnectError}
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Use default url 'localhost' and port '8765'
    let client = AnkiClient::default();

    // Get existing decks
    let decks: Vec<AnkiDeck> = client.get_all_decks()?;

    // Get available note types
    let models: Vec<AnkiModelIdentifier> = client.get_all_models()?;

    // Create a note using fetched resources
    let card = AnkiCardBuilder::new_for_model(models.get(0)?.clone())
        .add_field("Front", "¿Dónde está la biblioteca?")
        .add_field("Back", "Where is the library?")
        .add_tag("spanish-vocab")
        .build();

    // Add note to Anki
    match client.add_note(decks.get(0)?.clone(), card, false, None) {
        Ok(note_id) => println!("Added note with ID: {}", note_id),
        Err(AnkiRequestError::AnkiConnectError(AnkiConnectError::DeckNotFound(_))) => {
            eprintln!("Create the Spanish deck in Anki first!");
        }
        Err(e) => return Err(e.into()),
    }

    Ok(())
}

Dependencies

~9–18MB
~339K SLoC