# Programmatically Adding New Notes

**URL:** <https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326>\
**Category:** Development\
**Created:** [January 26, 2024, 6:10pm UTC](https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326 "2024-01-26T18:10:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjmh](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kjmh](https://forums.ankiweb.net/u/kjmh)\
**Post date:** [January 26, 2024, 6:10pm UTC](https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326/1 "2024-01-26T18:10:43Z")

</div>

I’m trying to write an addon that automates my note making process. Here’s my method for adding a new note to my collection.

```auto
def add_to_anki(word, definition, path):
    col = mw.col
    vocab_model = col.models.by_name('vocab-import')
    deck = col.decks.by_name('master')
    col.decks.select(deck['id'])
    col.decks.current()['mid'] = vocab_model['id']
    note = col.new_note(vocab_model)
    note.fields[0] = word
    note.fields[1] = definition
    w = mw.col.media.add_file(path)
    note.fields[2] = f'<img src="{w}">'
    col.add_note(note, deck['id'])

```

However, when I run these lines (strings for each of the three arguments), I get this error with my last line, `add_note`:

```auto
  File "anki.collection", line 528, in add_note
  File "anki.notes", line 71, in _to_backend_note
TypeError: bad argument type for built-in operation

```

It looks like `_to_backend_note` needs me to make my new note in a different way. Is there a better way to make a new note?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [January 26, 2024, 7:21pm UTC](https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326/2 "2024-01-26T19:21:31Z")

</div>

You’re probably providing add\_to\_anki() with non-string arguments.

---

<div class="post-metadata">

**Author:** ![kjmh](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kjmh](https://forums.ankiweb.net/u/kjmh)\
**Post date:** [January 29, 2024, 10:28am UTC](https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326/3 "2024-01-29T10:28:59Z")

</div>

Thank you. That was it. I only thought I was passing strings—I was not.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [February 28, 2024, 10:29am UTC](https://forums.ankiweb.net/t/programmatically-adding-new-notes/40326/4 "2024-02-28T10:29:02Z")

</div>

Closed on February 28, 2024, 10:29am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
