How to easily make a bitcoin lightning app
A payment layer like bitcoin can be used for more than traditional payments. It can be used to do fun things like interacting with a website by doing transactions.
You can make paywalls, set bounties, create tip-bots which can tip small amounts of bitcoin.
The fact you can send money to anyone in this world instantly with a reddit comment or a tweet is what makes this technology truly powerful.
First thing - you need a bitcoin node
We cannot simply use a bitcoin wallet. We need something that's a full package with APIs to all aspects of a merchant.
There are 3 major options you can use:
1)BTCPay Server: Self-hosted bitcoin merchant node (non-custodial Highly recommended)
2) lnpay.co: This is a service that can help create us lightning apps. It's free but custodial. We'll be using this for now.
3) lnbits: self-hosted open-source. Inspired from lnpay.
There's also opennode.com though I stopped using them ever since they started requiring KYC.
Setting up the node
In this tutorial we'll use lnpay. Go to https://lnpay.co and create an accont.
Once done, go to: https://lnpay.co/wallet/dashboard and create a wallet. You'll be using this wallet to accept bitcoin on your website.
After that you'll see a screen with your wallet. Take a look at "Wallet Access Keys" this is what our web server use to communicate with this walet. Everything else is pretty self-explanatory.
Make sure to NOT disclose access keys!
Great! Now we have our node setup. It's custodial so take it with a grain of salt.
At this point I recommend reading the lnpay docs: https://docs.lnpay.co/
Though there are some aspects which I have made easier so keep reading.
Setting up your web server
Our web server will be an express js server running on node.js. Anything from here requires some experience with web development.
Assuming you have setup your express server. We can use the lnpay API to access our wallet.
First, go to https://lnpay.co/developers/dashboard and get your API keys. It's best to keep all communication with bitcoin node server side.
I'll discuss actions you can perform with the API:
1) Request Invoice: When the user wants to pay you (to spin a wheel, deposit money, or anything your website does), you'll have to provide the user with an invoice to your wallet.
To do that you can use a function like below:
var request = require('request');
function request_invoice(amt, memo="send to my website"){
var headers = {
'Content-Type': 'application/json'
};
// num_satoshies is the amount requested
// memo is the memo that comes embedded into the payment request
var dataString = '{"num_satoshis":'+amt+', "memo":"'+memo+'"}';
// pointing url to your wallet
var options = {
url: 'https://lnpay.co/v1/user/wallet/waki_YkKDYw5aOfO7mAPBQHXyQB/invoice',
method: 'POST',
headers: headers,
body: dataString,
auth: {
'user': 'sak_.....', // your full API key
'pass': ''
}
};
// what to do when we get the response
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
// execute request
request(options, callback);
}
Complete documenation: https://docs.lnpay.co/wallet/generate-invoice
Here you have to fill up 2 values. One, url inside options. Here "waki_YkKDYw5aOfO7mAPBQHXyQB" can be found in your wallet page. Replace it with whatever you have.
2nd, you need to fill your API key in options.auth.user, it can be found at https://lnpay.co/developers/dashboard
Now when you call request_invoice() from your server, your web server will get an invoice to your wallet from lnpay. You can pass in your express request object and use req.send(body) in the callback function to return the lightning payment request to your user.
2) Pay invoice: There may be cases where you'd pay the user. For example, rewarding the user, winning lottery, etc.
The input to this function is the payment request sent by the user.
var request = require('request');
function pay_invoice(pay_req){
var headers = {
'Content-Type': 'application/json'
};
var dataString = '{"payment_request":"'+pay_req+'"}';
var options = {
url: 'https://lnpay.co/v1/wallet/waka_KXTGXR6T4lm1a3leeZnP4/withdraw',
method: 'POST',
headers: headers,
body: dataString,
auth: {
'user': 'sak_...', // Your API key here
'pass': ''
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
}
Just like before you need replace wallet access key with yours.
Full documentation: https://docs.lnpay.co/wallet/pay-invoice
3) lnurl-withdraw: lnurl is a recent standard that makes withdrawing lightning bitcoin much more easier. It's a UX improvement from traditional withdrawal. Traditionally, the user has to generate an invoice from their wallet (which'll probably be on their phone) that they'll have paste onto your website (which may not be on their phone).
There's also a concern that the client can be compromised and the invoices can get swapped to that of the hacker when pasting.
lnurl-withdraw is more secure and user friendly.
You can read a complete overview on lnurl improvements here: https://degreesofzero.com/article/beyond-coffee-bitcoin-for-every-day-use-with-lnurl.html
To implement lnurl-withdraw in your server, you can use the function:
var request = require('request');
function lnurl_withdraw(amt, memo='payment from your website'){
var options = {
url: 'https://lnpay.co/v1/wallet/waka_KXTGXR6T4lm1a3leeZnP4/lnurl/withdraw?num_satoshis='+amt+'&memo='+memo,
auth: {
'user': 'sak_...', // Your API key here
'pass': ''
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
}
Again, make sure to swap out access keys with yours.
Full documentation: https://docs.lnpay.co/wallet/lnurl-withdraw
Wrapping up
Everything else other than this is traditional web dev and not specifically related to bitcoin. With these 3 functions in your toolbox, you can create any lighting app.
Wanna create a tipbot for your discord server?
Wanna create a website where people can spin lottery?
Wanna create a website where users can pay you for your service?
You can do all this with these 3 functions in your hand. Now go make some beautiful Lapps! Good luck!