Skip to content

Commit

Permalink
add paywall to api
Browse files Browse the repository at this point in the history
  • Loading branch information
neb-b committed Apr 29, 2023
1 parent 2cf7463 commit d8856ab
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"next": "^12.3.0",
"node-twitter": "^0.5.3",
"number-to-words": "^1.2.4",
"paymeformyapi": "^0.0.5",
"react": "^18.2.0",
"react-charts": "^3.0.0-beta.48",
"react-dom": "^18.2.0",
Expand Down
28 changes: 26 additions & 2 deletions src/pages/api/data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { NextApiResponse, NextApiRequest } from "next"
import { getData } from "core/data"
import { NextApiResponse, NextApiRequest } from 'next'
import PayMeForMyAPI from 'paymeformyapi'
import { getData } from 'core/data'

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
})

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { api_token: api_token_from_request } = req.query

try {
const { success, api_token, invoice } = await paywall.deductBalance(api_token_from_request as string)

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

const { usd } = await getData()

res.status(200).json({ usd })
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3665,6 +3665,13 @@
"resolved" "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz"
"version" "4.0.0"

"paymeformyapi@file:../paymeformyapi":
"resolved" "file:../paymeformyapi"
"version" "0.0.4"
dependencies:
"axios" "^1.4.0"
"uuid" "^9.0.0"

"pend@~1.2.0":
"integrity" "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg=="
"resolved" "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz"
Expand Down

0 comments on commit d8856ab

Please sign in to comment.