lightning-mpp-extension-sdk
v0.1.2
Published
Browser-side Lightning MPP client that routes 402 (Payment Required) approvals through a compatible browser extension event bridge.
Maintainers
Readme
lightning-mpp-extension-sdk
Automatically routes 402 Payment Required approvals through a compatible browser extension via a window event bridge.
When a server responds with a 402 Lightning payment challenge, the SDK:
- Probes the page for a compatible extension using the
mpp:extensionevent (type: 'request',paymentMethods: ['lightning'],intents: ['charge']). - Forwards the Lightning
Paymentchallenge to the extension viampp:challenge. - Waits for the extension's
mpp:credentialresponse and transparently retries the original request with the returned credential.
Installation
npm install lightning-mpp-extension-sdkUsage
import { createLightningMppExtensionClient } from 'lightning-mpp-extension-sdk';
const client = createLightningMppExtensionClient({
polyfill: false,
});
const response = await client.fetch('https://api.example.com/paid-endpoint');
const data = await response.json();Options
interface CreateLightningMppExtensionClientOptions {
/** Custom fetch implementation. Defaults to the global fetch. */
fetch?: typeof globalThis.fetch;
/** Patch the global fetch (Mppx.create polyfill). Defaults to true. */
polyfill?: boolean;
/** Timeout for the extension payment approval, in ms. Defaults to 90000. */
paymentTimeoutMs?: number;
/** Probe for the extension before requesting credentials. Defaults to true. */
probeExtension?: boolean;
/** Timeout for the extension probe, in ms. Defaults to 1500. */
extensionProbeTimeoutMs?: number;
}Restoring global fetch
If you used the global fetch polyfill, restore the original implementation with:
import { restoreLightningMppExtensionFetch } from 'lightning-mpp-extension-sdk';
restoreLightningMppExtensionFetch();Requirements
- A browser
windowcontext (the SDK throws ifwindowis unavailable). - A compatible extension installed on the page that responds to the
mpp:extension,mpp:challenge, andmpp:credentialevents.
Development
npm install # install dependencies + dev dependencies
npm run build # bundle ESM + CJS + type declarations into dist/