How to use Notebook REST API from Jupyterhub REST API

Description

Am not sure if this is a bug or this feature does not exist in JupyterHub.
I have my website and I am among to connect it to JupyterHub, when the user clicks on a button called “create notebook”, this will direct the user to his Jupyterhub home directory and creates his new notebook with name like “new_notebook.ipynb”.

As far as I understood the JupyterHub API is running on 8081 by default also in my case it does run on 8081, I can do different commands POST, GET, this works fine, however, to create new Notebook I have to use the Notebook API, not the JupyterHub, and I don’t know which is the right port for the Notebook! I understand that each user has his own port for the API and I find it a difficult or bad idea to try to fetch t users API port to create his own notebook if I just can have on a global port to use to create different notebooks for different users as an admin.

Also, I understood from the documentation that if I am using the Jupyterhub API port to access the Notebook API this will be done automatically from the JupyterHub, it will redirect me to the Notebook API correct?

So for testing, I am just running this code:

import requests
r = requests.get('http://localhost:8081/user/jupyterhub/api' + '/sessions',
headers={
'Authorization': 'token %s' % token,
}
)

r.raise_for_status()
users = r.json()

Am getting this output:

HTTPError: 500 Server Error: Internal Server Error for url: http://localhost:8081/hub/user/jupyterhub/api/sessions?redirects=4

Expected behavior

This should redirect me to the Notebook API and fetch the sessions and return a result.

Actual behaviour

HTTPError: 500 Server Error: Internal Server Error for url: http://localhost:8081/hub/user/jupyterhub/api/sessions?redirects=4

How to reproduce

import requests
r = requests.get('http://localhost:8081/user/jupyterhub/api' + '/sessions',
headers={
'Authorization': 'token %s' % token,
}
)

r.raise_for_status()
users = r.json()

Your personal set up

  • OS: Debian GNU/Linux 10 (buster)
  • Version: 10
  • Configuration:
    c.Authenticator.admin_users = {'jupyterhub'}
    c.JupyterHub.api_tokens = { 'token': 'jupyterhub', }

So, this idea as I said I just want to create a new notebook for the logged-in user inside my system inside Jupyterhub by using the JupyterHub, and I don’t know-how.

hey, did you find a solution for this? I am facing the same problem.

You shouldn’t need to use the API port for jupyterhub, you can go via the proxy instead, i.e. the same address that you use to log in to JupyterHub through your web browser. JupyterHub is only responsible for starting your singleuser/notebook server, after that it just proxies everything through, so you can do anything you want using the notebook API providing of course you include the required tokens or cookies.

1 Like