@bamotf/node
This library provides convenient access to the bamotf API from applications written in server-side JavaScript.
Requirements
Node 12 or higher.
Installation
Install the package with:
npm install @bamotf/node
Usage
The package needs to be configured with your API Key, which is defined as a env var during start up (see Running the server).
Using it with CJS:
const bamotf = require('@bamotf/node')('secret-key...')
bamotf.paymentIntents
.retrieve(pi.id)
.then(pi => console.log(pi))
.catch(error => console.error(error))
Or using ES modules and async
/await
:
import Bamotf from '@bamotf/node'
const bamotf = new Bamotf('my-fancy-token', {
// baseURL: 'http://localhost:21000',
})
const pi = await bamotf.paymentIntents.create({
amount: 6.25,
address: 'bc1q00hf9mlaxzej30cx5q6200c636ufdc4gjmqyuh',
})
console.log(pi.id)
Configuration
Initialize with config object
The package can be initialized with several options:
const bamotf = Bamotf('my-fancy-token...', {
baseURL: 'https://my-core:21000',
})
Option | Default | Description |
---|---|---|
baseURL | 'http://localhost:21000' | Base URL that requests are made to. |
Webhook signing
bamotf can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party.
Please note that you must pass the raw request body, exactly as received from
bamotf, to the constructEvent()
function; this will not work with a parsed
(i.e., JSON) request body.
You can find an example of how to use this with various JavaScript frameworks in
[examples/webhook-signing
][examples/webhook-signing] folder, but here's what
it looks like:
const {success, parsed} = bamotf.webhooks.constructEvent(
webhookRawBody,
webhookSignatureHeader,
webhookSecret,
)
Testing Webhook signing
You can use bamotf.webhooks.generateTestHeaderString
to mock webhook events
that come from bamotf:
const payload = {
id: 'evt_test_webhook',
object: 'event',
}
const payloadString = JSON.stringify(payload, null, 2)
const secret = 'test_secret'
const header = bamotf.webhooks.generateTestHeaderString({
payload: payloadString,
secret,
})
const event = bamotf.webhooks.constructEvent(payloadString, header, secret)
if (!event.success) {
throw new Error('Not Authorized')
}
// Do something with mocked signed event
expect(event.parsed.id).to.equal(payload.id)
Managing Payment Intents
When you want to receive payments, you need to create a Payment Intent on your
unique bitcoin address. You can do this by calling the create
method on the
paymentIntents
object:
const paymentIntent = await bamotf.paymentIntents.create({
amount: 6.25,
address: 'bc1q00hf9mlaxzej30cx5q6200c636ufdc4gjmqyuh',
tolerance: 1 / 100,
confirmations: 6,
currency: 'BTC',
description: 'Payment for order #1234',
})
The paymentIntents
object has a retrieve
method that allows you to retrieve,
update, list, and cancel a Payment Intent:
const paymentIntent = await bamotf.paymentIntents.retrieve(
'1Hh0Xz2eZvKYlo2Ck4YJQ',
)
await bamotf.paymentIntent.update('1Hh0Xz2eZvKYlo2Ck4YJQ', {
description: 'Payment for order #1234',
})
await bamotf.paymentIntent.cancel('1Hh0Xz2eZvKYlo2Ck4YJQ', {
cancellationReason: 'User requested',
})
const allPis = await bamotf.paymentIntent.list()
Addresses
There's a addresses
object that allows you derive addresses from your xpubs:
const index = ...
const address = await bamotf.address.derive('xpub.....', index)