> Charge for resources and API calls with web monetization
npm install koa-web-monetization- Overview
- Example Code
- Try it Out
- Prerequisites
- Install and Run
- API Docs
- Constructor
- Receiver
- Paid
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.
koa-web-monetization makes this easy by providing middleware for your
Koa application. Charging your users is as easy as puttingctx.spend(100) in your route handler. No need to convince them to
buy a subscription or donate.
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.
``js
const Koa = require('koa')
const app = new Koa()
const router = require('koa-router')()
const KoaWebMonetization = require('koa-web-monetization')
const monetizer = new KoaWebMonetization()
// This endpoint charges 100 units to the user with :id
// If awaitBalance is set to true, the call will stay open until the balance is sufficient. This is convenient
// for making sure that the call doesn't immediately fail when called on startup.
router.get('/content/', async ctx => {
await ctx.awaitBalance(100)
ctx.spend(100)
// load content
})
router.get('/', async ctx => {
// load index page
})
app
.use(monetizer.mount())
.use(router.routes())
.use(router.allowedMethods())
.listen(8080)
`
The client side code to support this is very simple too:
` Underneath this is a paid image. It will load if you have web monetization on.html
Hello World
`
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.
- 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.
`sh`
git clone https://github.com/sharafian/koa-web-monetization.git
cd koa-web-monetization
npm install
DEBUG=koa* 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.
`ts
new KoaWebMonetization(opts: Object | void): KoaWebMonetization
`
Create a new KoaWebMonetization instance.
- opts.plugin - Supply an ILP plugin. Defaults to using Moneyd.opts.maxBalance
- - The maximum balance that can be associated with any user. Defaults to Infinity.opts.receiveEndpointUrl
- - The endpoint in your Koa 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).opts.clientFilePath
- - This sets the endpoint which specifies where you want to serve the MonetizerClient file to your frontend. Your html script source must match this.opts.cookieName
- - The cookie key name for your server generated payer ID. Defaults to __monetizer.opts.cookieOptions
- - Cookie configurations for Koa. See Koa ctx setting cookies options for more details! httpOnly has to be false for the monetizer to work!$3
`ts`
instance.mount(): FunctionawaitBalance
This middleware allows cookies to be generated (or just sent if already set) from the server to the client. It also injects the and spend methods described below. It also serves the MonetizerClient below to the client side.
`ts`
new MonetizerClient(opts: Object | void): MonetizerClientMonetizerClient
Creates a new instance.
- opts.url - The url of the server that is registering the KoaWebMonetization plugin. Defaults to new URL(window.location).originopts.cookieName
- - The cookie key name that will be saved in your browser. Defaults to __monetizer. This MUST be the same has opts.cookieName in the server configuration.opts.receiveEndpointUrl
- - The endpoint where users of the site can start streaming packets via their browser extension or through the browser API. Defaults to opts.url + '__monetizer/:id' where id is the server generated payer ID. This MUST be the same has opts.receiverEndpointUrl in the server configuration.
The methods ctx.spend() and ctx.awaitBalance() are available to use inside handlers.
`ts`
ctx.spend(amount): Function
Specifies how many units to charge the user.
`ts`
ctx.awaitBalance(amount): FunctionawaitBalance` can be useful for when a call is being done at page start.
Waits until the user has sufficient balance to pay for specific content.
Rather than immediately failing because the user hasn't paid, the server will
wait until the user has paid the specified price.