{"id":25716329,"url":"https://github.com/interledgerjs/express-web-monetization","last_synced_at":"2025-05-05T20:49:17.319Z","repository":{"id":72490427,"uuid":"128424550","full_name":"interledgerjs/express-web-monetization","owner":"interledgerjs","description":null,"archived":false,"fork":false,"pushed_at":"2018-04-25T23:11:40.000Z","size":3865,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T23:31:39.802Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/interledgerjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-06T17:22:50.000Z","updated_at":"2021-01-17T20:52:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"399acf51-c28b-4e10-a068-95db42656984","html_url":"https://github.com/interledgerjs/express-web-monetization","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fexpress-web-monetization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fexpress-web-monetization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fexpress-web-monetization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fexpress-web-monetization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/interledgerjs","download_url":"https://codeload.github.com/interledgerjs/express-web-monetization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252575717,"owners_count":21770608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-25T14:53:00.353Z","updated_at":"2025-05-05T20:49:17.310Z","avatar_url":"https://github.com/interledgerjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express Web Monetization\n\u003e Charge for resources and API calls with web monetization\n\n- [Overview](#overview)\n- [Example Code](#example-code)\n- [Try it Out](#try-it-out)\n  - [Prerequisites](#prerequisites)\n  - [Install and Run](#install-and-run)\n- [API Docs](#api-docs)\n  - [Constructor](#constructor)\n  - [Receiver](#receiver)\n  - [Paid](#paid)\n\n## Overview\n\nUsing [Interledger](https://interledger.org) for payments, [Web\nMonetization](https://github.com/interledger/rfcs/blob/master/0028-web-monetization/0028-web-monetization.md#web-monetization)\nallows sites to monetize their traffic without being tied to an ad network. And\nbecause payments happen instantly, they can also be tracked on the server-side\nto unlock exclusive content or paywalls.\n\n`express-web-monetization` makes this easy by providing middleware for your\n[Express](https://expressjs.com) application. Charging your users is as easy as putting\n`request.spend(100)` in your route handler. No need to convince them to\nbuy a subscription or donate.\n\n## Example Code\n\nBelow is an example of some of the functions that you would use to create\npaywalled content. For a complete and working example, look at the next\nsection.\n\n```js\n'use strict'\n\nconst express = require('express')\nconst app = express()\nconst server = require('http').Server(app)\nconst router = express.Router()\nconst ExpressWebMonetization = require('express-web-monetization')\nconst monetizer = new ExpressWebMonetization()\nconst cookieParser = require('cookie-parser')\nconst path = require('path')\n\n\n// This is the SPSP endpoint that lets you receive ILP payments.  Money that\n// comes in is associated with the :id\nrouter.get(monetizer.receiverEndpointUrl, monetizer.receive())\n\n// This endpoint charges 100 units to the user with :id\n// If awaitBalance is set to true, the call will stay open until the balance is sufficient. This is convenient\n// for making sure that the call doesn't immediately fail when called on startup.\nrouter.get('/content/', async (req, res) =\u003e {\n\n  await req.awaitBalance(100)\n  req.spend(100)\n  // load content\n})\n\nrouter.get('/client.js', async (req, res) =\u003e {\n  res.send(monetizer.serverClient())\n})\n\nrouter.get('/', (req, res) =\u003e {\n  res.sendFile(path.join(__dirname + '/index.html'))\n})\n\n\napp.use(cookieParser())\napp.use(monetizer.mount())\napp.use('/', router)\nserver.listen(8080)\n\n```\n\nThe client side code to support this is very simple too:\n\n```html\n\u003c!doctype html\u003e\n\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n  \u003cmeta charset=\"utf-8\"\u003e\n\u003c/head\u003e\n\n\u003cbody\u003e\n  \u003cdiv id=\"container\"\u003e \u003c/div\u003e\n  \u003cimg src=\"/content\" width=\"600\"/\u003e\n\u003cscript src=\"/scripts/monetization-client.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n    var monetizerClient = new MonetizerClient();\n    monetizerClient.start()\n  \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Try it out\n\nThis repo comes with an example server that you can run. It serves a page that has a single paywalled image on it.\nThe server waits for money to come in and then shows the image.\n\n### Prerequisites\n\n- You should be running [Moneyd](https://github.com/interledgerjs/moneyd-xrp)\n  for Interledger payments. [Local\n  mode](https://github.com/interledgerjs/moneyd-xrp#local-test-network) will work\n  fine.\n\n- Build and install the [Minute](https://github.com/sharafian/minute)\n  extension. This adds Web Monetization support to your browser.\n\n### Install and Run\n\n```sh\ngit clone https://github.com/andywong418/express-web-monetization.git\ncd express-web-monetization\nnpm install\nDEBUG=* node example/index.js\n```\n\nNow go to [http://localhost:8080](http://localhost:8080), and watch the server\nlogs.\n\nIf you configured Minute and Moneyd correctly, you'll start to see that money\nis coming in. Once the user has paid 100 units, the example image will load on\nthe page.\n\n## API Docs\n\n### Constructor\n\n```ts\n\nnew ExpressWebMonetization(opts: Object | void): ExpressWebMonetization\n```\n\nCreate a new `ExpressWebMonetization` instance.\n\n- `opts.plugin` - Supply an ILP plugin. Defaults to using Moneyd.\n- `opts.maxBalance` - The maximum balance that can be associated with any user. Defaults to `Infinity`.\n- `opts.receiveEndpointUrl` - The endpoint in your Express 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).\n- `opts.cookieName` - The cookie key name for your server generated payer ID. Defaults to `__monetizer`.\n- `opts.cookieOptions` - Cookie configurations for Express. See [Express response setting cookies options](https://expressjs.com/en/api.html) for more details! Only defaults are `httpOnly: false`\n### Receiver\n\n```ts\ninstance.receiver(): Function\n```\n\nReturns a Express middleware for setting up Interledger payments with\n[SPSP](https://github.com/sharafian/ilp-protocol-spsp) (used in Web\nMonetization).\n\n### Client constructor options\n\n```ts\nnew MonetizerClient(opts: Object | void): MonetizerClient\n```\nCreates a new `MonetizerClient` instance.\n\n- `opts.url` - The url of the server that is registering the ExpressWebMonetization plugin. Defaults to `new URL(window.location).origin`\n- `opts.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.\n- `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.\n\n### Middleware for cookies\n\n```ts\nWebMonetizationMiddleware(monetizer: ExpressWebMonetization)\n```\nThis middleware allows cookies to be generated (or just sent if already set) from the server to the client. It also injects the `awaitBalance` and `spend` methods described below. Note that your app must require and use the `cookie-parser` middleware before it uses the `WebMonetizationMiddleware`.\n\n### Charging users\n\nThe methods `req.spend()` and `req.awaitBalance()` are available to use inside handlers.\n\n```ts\nreq.spend(amount): Function\n```\nSpecifies how many units to charge the user.\n\n```ts\nreq.awaitBalance(amount): Function\n```\nWaits until the user has sufficient balance to pay for specific content.\n`awaitBalance` can be useful for when a call is being done at page start.\nRather than immediately failing because the user hasn't paid, the server will\nwait until the user has paid the specified price.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finterledgerjs%2Fexpress-web-monetization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finterledgerjs%2Fexpress-web-monetization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finterledgerjs%2Fexpress-web-monetization/lists"}