See any bugs? Let us know in the #opennode channel in the BEN Slack!
In this activity, you will use OpenNode to create Bitcoin and Lightning Network invoices. OpenNode offers a REST API that supports the bitcoin lightning network and standard on-chain operations with action events associated allowing you to receive and send microtransactions instantly.
OpenNode uses separate development and production environments. This means you can build your app in the in the development enivroment connected to Bitcoin's testnet network as well as Lightning infrastructure connected to testnet full nodes. After testing, you can move over to production environment and launch on the mainnet.
Languages used - Javascript
Register an account on dev.opennode.com. OpenNode provides a developer focused network for the developer network and a main website for the main network (oppennode).
Get API credentials from the dashboard (Developers -> Integrations)
You will have to set up 2 Factor Authentication to get your keys.
Create a new api key with a label and set the permissions to Invoices
Copy the key and save it in a secure location for now.
First, create a new folder to store all of your working files.
NPM (Node package manager) is a package manager for online projects.
Install NPM - https://nodejs.org/en/download/
Open a terminal and go to your newly created folder.
npm init --yes
This createas a package.json file in your folder.
We will be using the OpeNNode module which allows us to interact with the OpenNode API.
Open the folder and run npm install opennode.
You will see now that the file package.json has a new "dependency" called opennode.
We're ready to use OpenNode. Imagine you are a merchant and want to accept a Bitcoin payment. You just handed over and now you need to send them an invoice.
You need to create a 'charge' to receive payments through OpenNode.
An OpenNode "charge" contains two parts:
In order to receive notifications when a 'charge' changes its status, you need to subscribe to the 'charge' events through webhooks. You can subscribe by passing a 'callback_url' parameter on the charge payload.
Lightning Network limit: The API does not return a lightning invoice when creating a charge with an amount bigger than 4294967 sats (~ 0.0429 BTC).
make a new file called index.js
At the top of the file, add your import statement.
const opennode = require('opennode');
Next, set your credentials
opennode.setCredentials('MY_API_KEY', 'dev');
Replace 'MY_API_KEY' with your actual api key.
For now, it is ok to paste your API directyl. For a real application, you'll want to separate the API key into a separate file so that it isn't exposed.
Next, Now make a const called "charge" will all the properties of your transaction.
const charge = { description: 'Test charge', amount: 10.5, // required currency: 'USD', order_id: '823320', customer_name: 'Satoshi Nakamoto', customer_email: 'satoshi@nakamoto.com', notif_email: 'satoshi@nakamoto.com', callback_url: "https://example.com/webhook/opennode", success_url: 'https://example.com/order/abc123', auto_settle: false };
Next, create the method for creating a charge
opennode.createCharge(charge) .then(charge => { console.log(charge); }) .catch(error => { console.error(`${error.status} | ${error.message}`); });
Full index.js:
const opennode = require('opennode'); opennode.setCredentials('MY_API_KEY', 'dev'); const charge = { description: 'Test charge', amount: 10.5, // required currency: 'USD', order_id: '823320', customer_name: 'Satoshi Nakamoto', customer_email: 'satoshi@nakamoto.com', notif_email: 'satoshi@nakamoto.com', callback_url: "https://example.com/webhook/opennode", success_url: 'https://example.com/order/abc123', auto_settle: false }; opennode.createCharge(charge) .then(charge => { console.log(charge); }) .catch(error => { console.error(`${error.status} | ${error.message}`); });
Now run node index.js from the command line. If the request was correctly sent, the API will reply with the following JSON:
{ id: '3c2dc69f-e1e9-42bc-924c-a3d0c7b48a60', name: 'Satoshi Nakamoto', description: 'Test charge', created_at: 1575441171, status: 'unpaid', callback_url: 'https://example.com/webhook/opennode', success_url: 'https://example.com/order/abc123', order_id: '823320', notes: 'Order: 823320\nCustomer Email: satoshi@nakamoto.com', currency: 'USD', source_fiat_value: 10, fiat_value: 10, auto_settle: false, notif_email: 'satoshi@nakamoto.com', lightning_invoice: { expires_at: 1575444771, payreq: 'LNTB1413760N1PW7W5CNPP5HMRHJTCSG03763HDQ89EHDP0LHMKWYC0580Q08L78TNKG4QCFMQQDQJ23JHXAPQVD5XZUN8V5CQZPGH73REKT0XF3ZP76HJMH6ZYEUZALW7J233WYD5MRNWVEXFWXL9JUN94LRGX3JQYZFUFTAJ3RTHSA7HNUWTS0LNNQUD80JD32F36CH55GPCELDAA' }, chain_invoice: { address: '2Mwizr7Yo2EpZsHKFtinRYANinWxj2a8c8Z' }, address: '2Mwizr7Yo2EpZsHKFtinRYANinWxj2a8c8Z', amount: 141376, uri: 'bitcoin:2Mwizr7Yo2EpZsHKFtinRYANinWxj2a8c8Z?amount=0.00141376&label=Test charge&lightning=LNTB1413760N1PW7W5CNPP5HMRHJTCSG03763HDQ89EHDP0LHMKWYC0580Q08L78TNKG4QCFMQQDQJ23JHXAPQVD5XZUN8V5CQZPGH73REKT0XF3ZP76HJMH6ZYEUZALW7J233WYD5MRNWVEXFWXL9JUN94LRGX3JQYZFUFTAJ3RTHSA7HNUWTS0LNNQUD80JD32F36CH55GPCELDAA' }
You can craft your invoice by going to https://dev-checkout.opennode.com/{id} with the id at the end ex:3c2dc69f-e1e9-42bc-924c-a3d0c7b48a60
You can pay this with any lightning enabled wallet on the test net.
download electrum run the electrum.exe as --test set up a new wallet get testnet bitcoin from a bitcoin faucet (https://bitcoinfaucet.uo1.net/)
copy and paste btc address into wallet and send payment. You can see the page automatically updates once payment is received. If the sender underpays, the checkout page will update with the remaining balance.