DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on

15 8

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

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!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay