Monobank Node.js Library
The Monobank Node library provides convenient access to the Monobank API from applications written in server-side JavaScript.
Please keep in mind that this package is for use with server-side Node that uses Monobank secret keys. To maintain PCI compliance, tokenization of credit card information should always be done with Monobank.js on the client side. This package should not be used for that purpose.
Documentation
See the Node API docs.
Installation
Install the package with:
npm install stripe --save
Usage
The package needs to be configured with your account's secret key which is available in your Monobank Dashboard. Require it with the key's value:
const stripe = 'sk_test_...'; const customer = await stripecustomers;
Or using ES modules, this looks more like:
;const stripe = ;//…
On older versions of Node, you can use promises
or callbacks instead of async
/await
.
Usage with TypeScript
Monobank does not currently maintain typings for this package, but there are community typings available from DefinitelyTyped.
To install:
npm install --dev @types/stripe
To use:
// Note `* as` and `new Monobank` for TypeScript:;; Monobank.customers.ICustomer> = stripe.customers.create/* ... */;
Using Promises
Every method returns a chainable promise which can be used instead of a regular callback:
// Create a new customer and then a new charge for that customer:stripecustomers ;
Using callbacks
On versions of Node.js prior to v7.9:
var stripe = 'sk_test_...'; stripecustomers;
Configuring Timeout
Request timeout is configurable (the default is Node's default of 120 seconds):
stripe; // in ms (this is 20 seconds)
Configuring For Connect
A per-request Monobank-Account
header for use with Monobank Connect
can be added to any method:
// Retrieve the balance for a connected account:stripebalance ;
Configuring a Proxy
An https-proxy-agent can be configured with
setHttpAgent
.
To use stripe behind a proxy you can pass to sdk:
if processenvhttp_proxy const ProxyAgent = ; stripe;
Network retries
Automatic network retries can be enabled with setMaxNetworkRetries
.
This will retry requests n
times with exponential backoff if they fail due to an intermittent network problem.
Idempotency keys are added where appropriate to prevent duplication.
// Retry a request twice before giving upstripe;
Examining Responses
Some information about the response which generated a resource is available
with the lastResponse
property:
chargelastResponserequestId; // see: https://stripe.com/docs/api/node#request_idschargelastResponsestatusCode;
request
and response
events
The Monobank object emits request
and response
events. You can use them like this:
const stripe = 'sk_test_...'; const onRequest = { // Do something.}; // Add the event handler function:stripe; // Remove the event handler function:stripe;
request
object
api_version: 'latest' account: 'acct_TEST' // Only present if provided idempotency_key: 'abc123' // Only present if provided method: 'POST' path: '/v1/charges'
response
object
api_version: 'latest' account: 'acct_TEST' // Only present if provided idempotency_key: 'abc123' // Only present if provided method: 'POST' path: '/v1/charges' status: 402 request_id: 'req_Ghc9r26ts73DRf' elapsed: 445 // Elapsed time in milliseconds
Webhook signing
Monobank can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it here.
Please note that you must pass the raw request body, exactly as received from Monobank, 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 Express in the examples/webhook-signing
folder, but here's what it looks like:
const event = stripewebhooks;
Testing Webhook signing
You can use stripe.webhooks.generateTestHeaderString
to mock webhook events that come from Monobank:
const payload = id: 'evt_test_webhook' object: 'event'; const payloadString = JSON;const secret = 'whsec_test_secret'; const header = stripewebhooks; const event = stripewebhooks; // Do something with mocked signed eventto;
Writing a Plugin
If you're writing a plugin that uses the library, we'd appreciate it if you identified using stripe.setAppInfo()
:
stripe;
This information is passed along when the library makes calls to the Monobank API.
Auto-pagination
As of stripe-node 6.11.0, you may auto-paginate list methods. We provide a few different APIs for this to aid with a variety of node versions and styles.
for-await-of
)
Async iterators (If you are in a Node environment that has support for async iteration, such as Node 10+ or babel, the following will auto-paginate:
for await const customer of stripecustomers ; if break;
autoPagingEach
If you are in a Node environment that has support for await
, such as Node 7.9 and greater,
you may pass an async function to .autoPagingEach
:
await stripecustomers;console;
Equivalently, without await
, you may return a Promise, which can resolve to false
to break:
stripecustomers ;
If you prefer callbacks to promises, you may also use a next
callback and a second onDone
callback:
stripecustomers;
If your onItem
function does not accept a next
callback parameter or return a Promise,
the return value is used to decide whether to continue (false
breaks, anything else continues).
autoPagingToArray
This is a convenience for cases where you expect the number of items
to be relatively small; accordingly, you must pass a limit
option
to prevent runaway list growth from consuming too much memory.
Returns a promise of an array of all items across pages for a list request.
const allNewCustomers = await stripecustomers ;
Request latency telemetry
By default, the library sends request latency telemetry to Monobank. These numbers help Monobank improve the overall latency of its API for all users.
You can disable this behavior if you prefer:
stripe;
More Information
Development
Run all tests:
$ yarn install$ yarn test
If you do not have yarn
installed, you can get it with npm install --global yarn
.
Run a single test suite:
$ yarn mocha test/Error.spec.js
Run a single test (case sensitive):
$ yarn mocha test/Error.spec.js --grep 'Populates with type'
If you wish, you may run tests using your Monobank Test API key by setting the
environment variable STRIPE_TEST_API_KEY
before running the tests:
$ export STRIPE_TEST_API_KEY='sk_test....'$ yarn test
Run prettier:
Add an editor integration or:
$ yarn fix