Home / GitHub Page

Joplin API - Python

Its because, before the api was released i wrapped the terminal command line which allow to rename folder, so i didnt remove it :nerd_face:

Hello!
I have a problem with API with sample from docs:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from joplin_api import JoplinApi
>>> joplin = JoplinApi(token='MY_TOKEN')
>>> joplin.ping()
<coroutine object JoplinApi.ping at 0x0373A810>
>>> folder = joplin.create_folder('folder_title')
>>> folder_id = folder.json()['id']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'coroutine' object has no attribute 'json'
>>>

After that got error and in Jolpin not exist new folder.
Version: Joplin 1.0.161, Win10 x64, Python 3.7.4. In Jolpin enabled clipper.

What I am doing wrong ?

You made a mistake.
Doc says

>>> from joplin_api import JoplinApi
>>> joplin = JoplinApi(token='the token provided by Joplin in the WebClipper menu:P'))
>>> ping = await joplin.ping()  # to check if the service is up
>>> print(ping.text)

You will notice the await keyword

If I add await, i have another error:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
>>> from joplin_api import JoplinApi
>>> joplin = JoplinApi(token=MY_TOKEN)
>>> ping = await joplin.ping()
  File "<stdin>", line 1
SyntaxError: 'await' outside function

Search in internet: when use ‘async’, ‘await’ needed ‘event loop’.
I am confused, what i need to do

This should answer your questions https://github.com/foxmask/joplin-api/issues/4
I will improve the doc with the event loop

@foxmask I somehow doubt that using an event loop for a request API makes sense. while this could be great for your web project, it’s rather awkward for using the API in a simple python script or on the command line. Maybe I’m wrong, but this seems excessive to me.

@tessus this is how asyncio works

@mrkingmidas with python 3.7+ you can use

asyncio.run(main())

instead of using the event loop by hand.
run is like a shortcut for it.

1 Like

I underatand that, but what I’m saying is that it is not necessary to use asyncio for this API. At least not when using it in a script or when I quickly want to fire off a quick python code on the command line.

In that case it’s better to wrap the Joplin command line

Hello!
Please, make example how to use create_resource method ?
I am put full filename as described, file or base64 => anyway error:

{"error":"Resource cannot be created without a file"}

And in get_resource error:

path = f'/resource_id/{resource_id}'

not work and work

path = f'/resources/{resource_id}'

Also I am try access by requests module:

import requests as requests
host = 'http://127.0.0.1:41184'
res = requests.post(host + '/resources/', files={'data':data}, params={'token':JoplinToken})

OR

res = requests.post(host + '/resources/', files={'data':(data)}, params={'token':JoplinToken})

where data is:

  • data=open(‘image.png’, ‘rb’)
  • data=string “123”
  • data=base64 encoded

And anyway error:

{"error":"Internal Server Error: Unexpected end of JSON input"}

What I am doing wrong ?
Global task: make script that import Markdown file with embeding images (by resources in Joplin termins)
Environment: Windows 10 x64, Joplin 1.0.165, Python 3.7

You can try with joplin import command line. I will make a try with your example

i could deal with resources with curl but not with requests
I’m digging that issue, as I use httpx

i’ve update the lib of the api, to update

pip install -U joplin-api 

or clone the github project if you prefer