How to pass in authorization token to API and return contents?

Apologies for the noob question. I’m creating a website on a static server (Github Pages) and am trying to call the Spotify API to obtain my most recently played track. I think the issue I’m having is that I’m having trouble entering the authorization token

This is the code I tried - look at the ‘response = await’ line:

      from pyodide.http import pyfetch
      import json
      import asyncio
      import re
      response = await pyfetch(url="", headers = {'authorization': --insert auth token here--} , method="GET")
      cal= str('C')
      output = f'{await response.json()}'
      def jj(output):

        str1 = output.replace("\'", "\"")
        st= json.loads(str1)

        return st


I end up getting a ‘JsException(PythonError: Traceback (most recent call last): File “/lib/python3.10/asyncio/”, line 201, in result raise self._exception File “/lib/python3.10/asyncio/”, line 232, in __step result = coro.send(None) File “/lib/python3.10/site-packages/_pyodide/”, line 500, in eval_code_async await CodeRunner( File “/lib/python3.10/site-packages/_pyodide/”, line 353, in run_async await coroutine File “”, line 7, in File “/lib/python3.10/site-packages/pyodide/”, line 139, in json self._raise_if_failed() File “/lib/python3.10/site-packages/pyodide/”, line 107, in _raise_if_failed raise OSError( OSError: Request for failed with status 400: )’

How can I fix this

Apologies for the duplicate answer - I just responded to you on Stack Overflow as well.

What version of PyScript are you using? Using top-level await like that was removed in Version 2022.12.1. The answer to your question will depend on which version of PyScript (and therefore which version of Pyodide) you are using.

My first impression is that you’ll want to wrap your headers object in to_js, as follows:

from pyodide.ffi import to_js
from js import Object

response = await pyfetch(
    headers = to_js({'authorization': --insert auth token here--}, dict_converter=Object.fromEntries), 

This will cause the headers object to be passed as JavaScript object, as opposed to a Mapping or PyProxy as is likely currently happening.

Hi thank you for response. I am on version 0.20.0.

When I try to run your code, I get the following error:

ModuleNotFoundError: No module named ‘pyodide.ffi’ )

Ah gotcha, so probably PyScript Alpha then? Is your link pointing at That’s a somewhat outdated version, the latest link is

If you do want to use the alpha version, try replacing that line that doesn’t work with from pyodide import to_js.