0.0.5 • Published 1 year ago

paymeformyapi v0.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Pay Me For My API

Add lightning paywalls to your api

How to use

You can view this commit to see paymeformyapi being added to a next.js project

npm i --save paymeformyapi
  • Generate an LNBits instance and install the User Manager extension
  • Create a new user & wallet, this will be the user that receives payments for every api request
  • Save the inkey from this user's wallet
// Initialize the package

import PayMeForMyAPI from 'paymeformyapi'

const paywall = new PayMeForMyAPI({
  lnBitsAdminId: process.env.LN_BITS_ADMIN_ID,
  lnBitsApiKey: process.env.LN_BITS_API_KEY,
  lnBitsURL: process.env.LN_BITS_URL,
  lnBitsAdminInvoiceKey: process.env.LN_BITS_ADMIN_INVOICE_KEY,
  refillAmount: 50, // number of satoshis to refill
  requestCost: 1, // number of satoshis per API call
})
// Add a paywall to an API route

const { api_token } = req.query // Get the api_token from the request somehow

const {
    success,           // User successfully paid for this request
    updated_api_token, // User has never paid for api access, or is out of sats, generate a new api token for them
    invoice            // Lightning invoice to refill their API access
} = await paywall.deductBalance(api_token)

if (!success) {
    res.status(402).json({
        message: 'Payment required. Please pay the invoice and update your api token.',
        updated_api_token,
        invoice,
    })

    return
}
0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago