bitpay-api v0.0.2
bitpay-api
The bitpay api wrapped for NodeJS/JavaScript.
Why we created it?
We created this API wrapper so you could access the equivalent REST API through simple asynchronous functions with all the power provided by Bitpay's direct API. This API uses simple method calls without all of the hassle of writing API request URLs, body and query strings.
Pull requests are welcomed!
API documentation
Instantiating a Client
var Bitpay = require('bitpay-api');
var bitpay = new BitPay(options);This generates a new API client. It accepts options arguments.
options:
-apiKey string Optional. Your Bitpay api key for write operations, like creating an invoice.
Bitpay client
- bitpay.createInvoice(order, callback)
- bitpay.getInvoice(invoiceId, callback)
- bitpay.getBTCBestBidRates, callback)
- bitpay.getBTCTxLedger(params, callback)
- bitpay.invoiceListener()
createInvoice
Create a Bitpay invoice from order.
Arguments
orderobject Customer order information to generate the invoice:pricestring Required. This is the amount that is required to be collected from the buyer. Note, if this is specified in a currency other than BTC, the price will be converted into BTC at market exchange rates to determine the amount collected from the buyer.currencystring Required. This is the currency code set for the price setting. The pricing currencies currently supported are USD, EUR, BTC, and all of the codes listed on this page: https://bitpay.com/bitcoinexchangeratesposDatastring Optional. A passthru variable provided by the merchant and designed to be used by the merchant to correlate the invoice with an order or other object in their system. Maximum string length is 100 characters. This passthru variable can be a JSON-encoded string, for exampleposData: { "ref" : 711454, "affiliate" : "spring112" }notificationURLstring Optional. A URL to send status update messages to your server (this must be an https URL, unencrypted http URLs or any other type of URL is not supported). Bitpay.com will send a POST request with a JSON encoding of the invoice to this URL when the invoice status changes.transactionSpeedstring Optional. default value: set in your https://bitpay.com/ordersettings, the default value set in your merchant dashboard is "medium". NOTE: Orders are posted to your Account Summary after 6 block confirmations regardless of this setting.- "high": An invoice is considered to be "confirmed" immediately upon receipt of payment.
- "medium": An invoice is considered to be "confirmed" after 1 block
- "low": An invoice is considered to be "confirmed" after 6 block confirmations (~1 hour).
fullNotificationsstring Optional. default value: false- true: Notifications will be sent on every status change.
- false: Notifications are only sent when an invoice is confirmed (according to the "transactionSpeed" setting).
notificationEmailstring Optional. Bitpay.com will send an email to this email address when the invoice status changes.redirectURLstring Optional. This is the URL for a return link that is displayed on the receipt, to return the shopper back to your website after a successful purchase. This could be a page specific to the order, or to their account.orderIDstring Optional. Used to display your public order number to the buyer on the BitPay invoice. In the merchant Account Summary page, this value is used to identify the ledger entry. Maximum string length is 100 characters.itemDescstring Optional. Used to display an item description to the buyer. Maximum string length is 100 characters.itemCodestring Optional. Used to display an item SKU code or part number to the buyer. Maximum string length is 100 characters.physicalstring Optional. default value: false - true: Indicates a physical item will be shipped (or picked up) - false: Indicates that nothing is to be shipped for this order Maximum string length of each field is 100 characters.buyerAddress1string 100 Optional.buyerAddress2string 100 Optional.buyerCitystring 100 Optional.buyerStatestring 100 Optional.buyerZipstring 100 Optional.buyerCountrystring 100 Optional.buyerEmailstring 100 Optional.buyerPhonestring 100 Optional.
callbackfunction Returns error or theinvoiceobject:idstring The unique id of the invoice assigned by bitpay.comurlstring An https URL where the invoice can be viewed.posDatastring The passthru variable provided by the merchant on the original invoice creation.statusstring The current invoice status.- "paid" As soon as full payment (or over payment) is received, an invoice goes into the paid status.
- "confirmed" The transaction speed preference of an invoice determines when an invoice is confirmed. For the high speed setting, it will be confirmed as soon as full payment is received on the bitcoin network (note, the invoice will go from a statusof new to confirmed, bypassing the paid status). For the medium speed setting, the invoice is confirmed after the payment transaction(s) have been confirmed by 1 block on the bitcoin network. For the low speed setting, 6 blocks on the bitcoin network are required. Invoices are considered complete after 6 blocks on the bitcoin network, therefore an invoice will go from a paid status directly to a complete status if the transaction speed is set to low.
- "complete" When an invoice is complete, it means that BitPay.com has credited the merchant’s account for the invoice. Currently, 6 confirmation blocks on the bitcoin network are required for an invoice to be complete. Note, in the future (for qualified payers), invoices may move to a complete status immediately upon payment, in which case the invoice will move directly from a new status to a complete status.
- "expired" An expired invoice is one where payment was not received and the 15 minute payment window has elapsed.
- "invalid" An invoice is considered invalid when it was paid, but payment was not confirmed within 1 hour after receipt.It is possible that some transactions on the bitcoin network can take longer than 1 hour to be included in a block. In such circumstances, once payment is confirmed, BitPay.com will make arrangements with the merchant regarding the funds (which can either be credited to the merchant account on another invoice, or returned to the buyer).
pricestring The price set by the merchant (in terms of the provided currency).currencystring The 3 letter currency code in which the invoice was priced.btcPricestring The amount of bitcoins being requested for payment of this invoice (same as the price if the merchant set the price in BTC).invoiceTimestring The time the invoice was created in milliseconds since midnight January 1, 1970. Time format is "20140101T19:01:01.123Z".expirationTimestring The time at which the invoice expires and no further payment will be accepted (in milliseconds since midnight January 1, 1970). Currently, all invoices are valid for 15 minutes. Time format is "20140101T19:01:01.123Z".currentTimestring The current time on the BitPay.com system (by subtracting the current time from the expiration time, the amount of time remaining for payment can be determined). Time format is "20140101T19:01:01.123Z".
bitpay.createInvoice(order, function(err, invoice) {
// .. utilize invoice
});getInvoice
Get the status of an invoice.
Arguments
invoiceIdstring The Bitpay invoice ID.callbackfunction Returns error or theinvoiceobject (see above).
bitpay.getInvoice(invoiceId, function(err, invoice) {
// .. utilize invoice
});getBTCBestBidRates
Get Bitcoin best bid rates.
Arguments
callbackfunction Returns error or theratesobject:namestring The full display name of the currency.codestring The three letter code for the currency, in all caps.ratenumber The numeric exchange rate of this currency provided by the BitPay server.
bitpay.getBTCBestBidRates(function(err, rates) {
// .. utilize rates
});getBTCTxLedger
Get the Bitcoin transaction ledger.
Arguments
paramsobject Ledger request object:cstring This is the three letter currency code set for the ledger. The payout currencies currently supported are:- BTC Bitcoin
- AUD Australian Dollar
- CAD Canadian Dollar
- EUR Eurozone Euro
- GBP Pound Sterling
- MXN Mexican Peso
- NZD New Zealand Dollar
- USD US Dollar
- ZAR South African Rand
startDatestring The start date for the ledger query. Ledger entries are retrieved from this date (inclusive) forward. The format for this parameter is "yyyymmdd"; example "20140101".endDatestring The end date for the ledger query. Ledger entries are retrieved up to and including this date. The format for this parameter is "yyyymmdd"; example "20140131".
callbackfunction Returns error or theledgerobject:codestring The transaction code in the BitPay ledger. The code is associated with a transaction type, "txType".- 1000 = "sale"
- 1001 = "fee"
- 1002 = "payout"
- 1003 = "ACH/other"
- 1004 = "charity fee refund"
- 1005 = "deposit"
- 1006 = "exchange"
- 1007 = "exchange fee"
- 1008 = "plan charge"
- 1009 = "plan change credit"
- 1010 = "plan underuse credit"
- 1011 = "plan charge transfer"
amountstring The amount of the credit or debit from the account. The amount is expressed in the currency unit specified in the request (see GET parameter "c"). The amount is a positive number for a ledger credit. The amount is a negative number for ledger debit.timestampstring The date and time the ledger entry was made. Time format is "20140101T19:01:01.123Z".descriptionstring The item description specified when the invoice was created. See invoice field "itemDesc".orderIdstring The merchant order identifier specified when the invoice was created. See invoice field "orderID". If the invoice did not specify an "orderID" then this field will not be present in the result set.txTypestring Identifies the type of transaction for the ledger entry. One of the following:- "sale"
- "fee"
- "payout"
- "ACH/other"
- "charity fee refund"
- "deposit"
- "exchange"
- "exchange fee"
- "plan charge"
- "plan change credit"
- "plan underuse credit"
- "plan charge transfer"
exRatesstring This is a JSON block containing the exchange rate used in the transaction.- currency code the three letter currency code specifying the units of the associated exchange rate.
- exchange rate the exchange rate price expressed in the associated currency code.
buyerFieldsstring This is a JSON block containing the buyer details for the transaction. These values were provided when the invoice was created. All values are strings. Maximum string length is 100 characters. If no buyer information was specified when the invoice was created then this field will be present but the JSON block will be empty; e.g. {}. If only several of the buyer information fields were specified when the invoice was created then those that were not specified will not be present in the result set.invoiceIdstring The unique id of the invoice assigned by bitpay.comsourceTypestring An identifier that specifies the type of source used to initiate the ledger entry. One of the following:<blank>- "invoice"
- "bitcoinTx"
bitpay.getBTCTxLedger(params, function(err, ledger) {
// .. utilize ledger
});invoiceListener (Middleware)
Create a Bitpay invoice from order. This is Express/Connect middleware. Place this on your POST receive invoice update route. Your request object will be populated with an invoice object (req.invoice). This is the same invoice type described earlier.
// middleware
app.post('receive/invoice/:internalInvoiceId', bitpay.invoiceListener(), function(req, res) {
// .. utilize req.invoice
// ... send status 200 once you have successfully processed the invoice, all other responses will cause bitpay to retry until 200 is received
res.json(200, {});
});Reference
https://github.com/bitpay/nodejs-client
https://bitpay.com/downloads/bitpayApi.pdf
Test
npm testNote: to test against the actual bitpay api do:
apiKey=<Your bitpay api key> npm test