Create a new file called index.js and add the following code:
Copy
Ask AI
import express from "express";import ZbdPayments from '@zbdpay/payments-sdk';const ZBD_API_KEY = 'b7YW3s2JzZKGcXjIf5Dqof8wjKT2RuWr8'; // dummy keyconst client = new ZbdPayments({ apikey: ZBD_API_KEY,});// Create Express appconst app = express();// Creating a Bitcoin Lightning payment requestapp.get("/request", async (req, res) => { try { const data = await client.lightningCharges.create({ amount: '100000', callbackUrl: 'https://your-app.com/zbd-callback', description: 'Express + ZBD!', }); res.status(200).json({ data }); } catch (error) { res.status(500).json({ error }); }});// Send a payment to a Bitcoin Lightning Addressapp.get("/send", async (req, res) => { try { const data = await client.lightningAddress.sendPayment({ lnAddress: "andreneves@zbd.gg", // Who is the recipient? amount: "100000", // 100 satoshis (100,000 msats) comment: "Express + ZBD!", }); res.status(200).json({ data }); } catch (error) { res.status(500).json({ error }); }});app.listen(3000, () => { console.log("Express server w/ ZBD listening on https://example.com");});
Run the following command to start your Express.js server:
Copy
Ask AI
yarn start
Opening your browser to the following URL: https://example.com/request should return a JSON response with a payment request.You can also test this using curl command:
Copy
Ask AI
curl https://example.com/request
You’re looking for the data.invoice.request property in the JSON response. It starts with lnbc1 and is the payment request anyone in the Bitcoin Lightning Network can use to pay you.
Charges and payment requests are usually shown to users as QR codes that can be scanned by mobile apps (e.g. ZBD). Read Callbacks to understand how to receive updates about your payment asynchronously.
Opening your browser to the following URL: https://example.com/send should return a JSON response with the payment sent message.You can also test this using curl command:
Copy
Ask AI
curl https://example.com/send
You’re looking for the status of completed to know that the payment settled successfully.
Payments in the Lightning Network are asynchronous so you may see a response stating the payment is processing. This is expected — use the callbackUrl property to receive updates about your payments.