Plugin API: how to get total note count?

I want to get the total number of notes currently in Joplin from within my plugin.

I have not found how to do it in docs; the joplin.data API doesn't seem to have anything straightforward, and I didn't find any way to get it from sync status, or similar.

What is the correct way of doing this?

One option is GET /notes. You just need to make sure to "unpaginate" the results.

Yes I think fetching all the notes and counting at the moment is only way. You can use the parameter fields=id to limit the load on the database.

But probably we should just have a /count end point to give that information in a more efficient way. @zblesk what is the use case?

Thank you both.

The use case for me is - personal stats. I already have a plugin that keeps a record of how many notes I'd edited in a given day (purely based on edit date).
I want to include the total number of notes in my base, so I can see how that evolves over time.

This data is currently sent to a service outside Joplin, and it's exported after every Joplin sync. That's often enough for me not to want to loop over all the notes every time it happens.

A /count endpoint would be appreciated.
It just occurred to me that in the meantime, I can hardcode the loop to start at, say, page 80, so the loop would be shorter :thinking:

Good idea - if you keep track of the last page, you can resume from there next time. You might want to handle the case where notes have been deleted though, which would mean checking the previous pages if the current one is empty.

Nah, I'll just hard-code it to start at 80, I probably won't fall below that. And if I get too far above, I'll change the const to 90. Much simpler to implement. :grin:

1 Like