npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-web-monetization

v1.0.0

Published

> Charge for resources and API calls with web monetization

Downloads

10

Readme

Hapi Web Monetization

Charge for resources and API calls with web monetization

Overview

Using Interledger for payments, Web Monetization allows sites to monetize their traffic without being tied to an ad network. And because payments happen instantly, they can also be tracked on the server-side to unlock exclusive content or paywalls.

hapi-web-monetization makes this easy by providing middleware for your Hapi application. Charging your users is as easy as putting request.spend(100) in your route handler. No need to convince them to buy a subscription or donate.

Example Code

Below is an example of some of the functions that you would use to create paywalled content. For a complete and working example, look at the next section.

const Hapi = require('hapi')

const server = Hapi.server({
  port: 8080,
  host: 'localhost'
})

const options = {
  cookieOptions: {
    isSecure: true
  }
}

const start = async () => {
  await server.register(require('inert'))
  await server.register({
    plugin: require('hapi-web-monetization'),
    options
  })

  server.route([
    {
      method: 'GET',
      path: '/',
      handler: function (request, reply) {
        // Load index page
      }
    },
    {
      method: 'GET',
      path: '/content/',
      handler: async function (request, reply) {
        await request.awaitBalance(100)
        request.spend(100)
        // Send paid content
      }
    },
  ])

  await server.start()

  console.log('Server running at:', server.info.uri)
}

start()

The client side code to support this is very simple too:

<script src="node_modules/hapi-web-monetization/client.js"></script>
<script>
  var monetizerClient = new MonetizerClient();
  monetizerClient.start()
    .then(function() {
      var img = document.createElement('img')
      var container = document.getElementById('container')
      img.src = '/content/'
      img.width = '600'
      container.appendChild(img)
    })
    .catch(function(error){
      console.log("Error", error);
    })
</script>

Try it out

This repo comes with an example server that you can run. It serves a page that has a single paywalled image on it. The server waits for money to come in and then shows the image.

Prerequisites

  • You should be running Moneyd for Interledger payments. Local mode will work fine.

  • Build and install the Minute extension. This adds Web Monetization support to your browser.

Install and Run

git clone https://github.com/andywong418/hapi-web-monetization.git
cd hapi-web-monetization
npm install
DEBUG=* node example/index.js

Now go to http://localhost:8080, and watch the server logs.

If you configured Minute and Moneyd correctly, you'll start to see that money is coming in. Once the user has paid 100 units, the example image will load on the page.

API Docs

Server register options

  server.register({
    plugin: require('hapi-web-monetization'),
    options : Object | void
  })

Registers a new HapiWebMonetization plugin which creates and sets cookie for the payer ID in the browser.

  • options.plugin - Supply an ILP plugin. Defaults to using Moneyd.
  • options.maxBalance - The maximum balance that can be associated with any user. Defaults to Infinity.
  • options.receiveEndpointUrl - The endpoint in your Hapi route configuration that specifies where a user pays streams PSK packets to your site. Defaults to /__monetizer/{id} where {id} is the server generated ID (stored in the browser as a cookie).
  • options.cookieName - The cookie key name for your server generated payer ID. Defaults to __monetizer.
  • options.cookieOptions - Cookie configurations for Hapi. See Hapi server state options for more details!

Client constructor options

new MonetizerClient(options: Object | void): MonetizerClient

Creates a new MonetizerClient instance.

  • options.url - The url of the server that is registering the HapiWebMonetization plugin. Defaults to new URL(window.location).origin
  • options.cookieName - The cookie key name that will be saved in your browser. Defaults to __monetizer. This MUST be the same has options.cookieName in the server configuration.
  • options.receiverUrl - The endpoint where users of the site can start streaming packets via their browser extension or through the browser API. Defaults to options.url + '__monetizer/:id' where id is the server generated payer ID. This MUST be the same has options.receiverEndpointUrl in the server configuration.

Charging users

The methods request.spend() and request.awaitBalance() are available to use inside handlers.

request.spend(amount): Function

Specifies how many units to charge the user.

request.awaitBalance(amount): Function

Waits until the user has sufficient balance to pay for specific content. awaitBalance can be useful for when a call is being done at page start. Rather than immediately failing because the user hasn't paid, the server will wait until the user has paid the specified price.