{"id":25716323,"url":"https://github.com/interledgerjs/koa-web-monetization","last_synced_at":"2025-05-05T20:45:35.070Z","repository":{"id":67177270,"uuid":"125569831","full_name":"interledgerjs/koa-web-monetization","owner":"interledgerjs","description":"Charge for content and endpoints server-side with web monetization","archived":false,"fork":false,"pushed_at":"2018-11-20T19:34:48.000Z","size":3851,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T23:31:34.673Z","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-03-16T21:06:18.000Z","updated_at":"2022-06-12T08:44:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2d3a700-3521-4164-848b-40f94604b2c4","html_url":"https://github.com/interledgerjs/koa-web-monetization","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fkoa-web-monetization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fkoa-web-monetization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fkoa-web-monetization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/interledgerjs%2Fkoa-web-monetization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/interledgerjs","download_url":"https://codeload.github.com/interledgerjs/koa-web-monetization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252574434,"owners_count":21770400,"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:52:59.548Z","updated_at":"2025-05-05T20:45:35.063Z","avatar_url":"https://github.com/interledgerjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DEPRECATED\nThis module has been deprecated in favor of [web-monetization-receiver](https://github.com/interledgerjs/web-monetization-receiver).\n\n# Koa 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`koa-web-monetization` makes this easy by providing middleware for your\n[Koa](http://koajs.com/) application. Charging your users is as easy as putting\n`ctx.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\nconst Koa = require('koa')\nconst app = new Koa()\nconst router = require('koa-router')()\nconst KoaWebMonetization = require('koa-web-monetization')\nconst monetizer = new KoaWebMonetization()\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 ctx =\u003e {\n\n  await ctx.awaitBalance(100)\n  ctx.spend(100)\n  // load content\n})\n\nrouter.get('/', async ctx =\u003e {\n  // load index page\n})\n\napp\n  .use(monetizer.mount())\n  .use(router.routes())\n  .use(router.allowedMethods())\n  .listen(8080)\n```\n\nThe client side code to support this is very simple too:\n\n```html\n\u003cbody\u003e\n  \u003ch1\u003eHello World\u003c/h1\u003e\n  \u003cp\u003eUnderneath this is a paid image. It will load if you have web monetization on.\u003c/p\u003e\n  \u003cdiv id=\"container\"\u003e\n    \u003c!-- Paid image will be appended here --\u003e\n  \u003cimg src=\"/content\" width=\"600\"/\u003e\n  \u003c/div\u003e\n  \u003cscript src=\"/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\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/sharafian/koa-web-monetization.git\ncd koa-web-monetization\nnpm install\nDEBUG=koa* 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 KoaWebMonetization(opts: Object | void): KoaWebMonetization\n```\n\nCreate a new `KoaWebMonetization` 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 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).\n- `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.\n- `opts.cookieName` - The cookie key name for your server generated payer ID. Defaults to `__monetizer`.\n- `opts.cookieOptions` - Cookie configurations for Koa. See [Koa ctx setting cookies options](http://koajs.com/) for more details! httpOnly has to be false for the monetizer to work!\n### Receiver\n\n```ts\ninstance.mount(): Function\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. It also serves the MonetizerClient below to the client side.\n\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 KoaWebMonetization 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\n\n### Charging users\n\nThe methods `ctx.spend()` and `ctx.awaitBalance()` are available to use inside handlers.\n\n```ts\nctx.spend(amount): Function\n```\nSpecifies how many units to charge the user.\n\n```ts\nctx.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%2Fkoa-web-monetization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finterledgerjs%2Fkoa-web-monetization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finterledgerjs%2Fkoa-web-monetization/lists"}