Search code examples
anki

Ankiconnect will not add note


I am trying to use Ankconnect to add a note, the deck and the model exists and the fields are correct, but I just get "null" back (and the card is not added).

Running this bash script

echo "---------- The deck exists:"

curl localhost:8765 -X POST -d '{
    "action": "getDeckConfig",
    "version": 6,
    "params": {
        "deck": "Foo"
    }
}'


echo "---------- The model exists:"

curl localhost:8765 -X POST -d '{
  "action": "modelFieldNames",
  "version": 6,
  "params": {
    "modelName": "Auto-generated"
  }
}'

echo "---------- But adding a card fails:"

curl localhost:8765 -X POST -d '{
  "action": "addNotes",
  "version": 6,
  "params": {
    "notes": {
      "deckName": "Foo",
      "modelName": "Auto-generated",
      "fields": {
        "Question": "why?",
        "Answer": "because!",
        "Card ID": "foo"
      },
      "options": {
        "allowDuplicate": true
      },
    }
  }
}'

Produces these results

---------- The deck exists:

{"result": {"dyn": false, "usn": 82, "timer": 0, "replayq": true, "name": "Default", "id": 1, "lapse": {"mult": 0.0, "minInt": 1, "delays": [10], "leechAction": 0, "leechFails": 8}, "autoplay": true, "rev": {"hardFactor": 1.2, "ivlFct": 1.0, "ease4": 1.3, "perDay": 200, "fuzz": 0.05, "minSpace": 1, "bury": false, "maxIvl": 36500}, "mod": 1560476298, "maxTaken": 60, "new": {"ints": [1, 4, 7], "perDay": 20, "delays": [1, 10], "order": 1, "initialFactor": 2500, "bury": false, "separate": true, "LBGIMinBefore": 1, "LBGIMinAfter": 1, "LBEIMinBefore": 4, "LBEIMinAfter": 4}}, "error": null}

---------- The model exists:

{"result": ["Question", "Answer", "Card ID"], "error": null}

---------- But adding a card fails:

null

Solution

  • There's a few errors in your JSON.

    Add "tags": [] to each of your notes objects, as it seems to be mandatory from the tests I've done but can be empty.

    This will at least get you an output of {"result": [null, null, null, null, null], "error": null}

    But then you need to fix some of your fields

    • "addNotes" takes an array of "notes" objects, but you're assigning a single object to "notes" and not an array.
    • "modelName" cannot be "Auto-generated" change it to "Basic"
    • "Question" should be "Front"
    • "Answer" should be "Back"

    So this is what the object in your curl call should look like:

    {
    "action": "addNotes",
    "version": 6,
    "params": {
      "notes": [ 
        {
          "deckName": "Foo",
          "modelName": "Basic",
          "fields": {
            "Front": "why?",
            "Back": "because!",
            "Card ID": "foo"
          },
          "options": {
            "allowDuplicate": true
          },
          "tags": []
        } 
      ]
    }
    }