DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on

A Python binding for the web monetization Javascript API

For this hackathon, I'm thinking of building a python binding for the web monetization API. Here's what I have in mind:

According to the docs, if you want to check if a user is web monetized, you check the state using:

document.monetization.state
Enter fullscreen mode Exit fullscreen mode

You would then proceed to implement your logic based on the state.

As a backend python developer, it would be cool if I could do that in python. If I'm using Flask for example, and I have a monetized route, I would want to write something like:

from monetize import check_state

@app.route('/monetized')
def monetized_page():
    state = check_state()
    if state == 'started':
        # implement logic to offer monetized content
    else:
        # do something else
Enter fullscreen mode Exit fullscreen mode

This is a simple example but that's the general idea - to have all the methods exposed by the js API in python. Javascript would of course still be working behind the scenes once this gets to the browser.

I believe something like this would make it easier to implement the monetization API for python developers working with web frameworks like flask and django.

Top comments (4)

Collapse
 
bengreenberg profile image
Ben Greenberg

When I saw this hackathon my first thought was how could this be made relevant to backend devs, as a backend dev myself. This is a great idea! Excited to see the progress on it.

Collapse
 
bengreenberg profile image
Ben Greenberg • Edited

You inspired me actually, and I'm putting together some Rack middleware to handle the web monetization API for the Ruby backend developer :)

update, it's posted!

Collapse
 
wangonya profile image
Kelvin Wangonya

Wow, that was fast πŸ˜€

Collapse
 
ben profile image
Ben Halpern

Neat!