BtcPayServer’s ruby integration

Pablo Fernandez
Nov 6, 2019 · 2 min read

I’ve been banging my head against the wall trying to get ruby to talk to my btcpayserver instance. The ruby integration uses Bitpay’s gem, which is outdated and the documentation is quite out of sync with the actual implementation.

I’ll look into releasing a gem for btcpayserver specifically that handles things properly when I have time, but in the meantime, here’s how I got it to work.

BtcPaysServer ruby integration

  1. Install bitpay-sdk:
gem install bitpay-sdk

2. Generate PEM certificate locally

$ pry
> require 'bitpay_sdk'
> pem = BitPay::KeyUtils.generate_pem
=> "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICR9KvFrAg7q8RHJUzCck7eFGD0qTDTz83n2IbHevF8/oAcGBSuBBAAK\noUQDQgAEttn/qOjm9jD7Q/UrBJlvEo+JwZPK+Xn/1td69DiJBPDZKh0igNyWTiQ8\nymJVCTsvaXGxajIPtCF+puwXoj+Ghg==\n-----END EC PRIVATE KEY-----\n"

You’ll wanna save that certificate somewhere to reuse the client.

3. Generate the client

> client = BitPay::SDK::Client.new(api_uri: 'https://pay.littleq.net', pem: pem)

4. Start a SIN pairing on your BtcPayServer and grab the pairing code.

5. Pair the client locally.

> client.pair_client(pairingCode: 'YS5cwdU')
[{"policies"=>[], "pairingCode"=>"YS5cwdU", "pairingExpiration"=>1573031853255, "dateCreated"=>1573030953255, "facade"=>"merchant", "token"=>"CQGVisa1ELtHqz4cJhmr5bZ8STkApNab7982vMJ3teAn", "label"=>""}]

Keep a note of the token (CQGVisa1ELtHqz4cJhmr5bZ8STkApNab7982vMJ3teAn), you’ll need it to recreate the client.

That’s it, your client is authenticated. Let’s recreate the client now, this time without having to create a new pairing so it could be done programmatically.

# Load the pem from permanent storage
pem = "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICR9KvFrAg7q8RHJUzCck7eFGD0qTDTz83n2IbHevF8/oAcGBSuBBAAK\noUQDQgAEttn/qOjm9jD7Q/UrBJlvEo+JwZPK+Xn/1td69DiJBPDZKh0igNyWTiQ8\nymJVCTsvaXGxajIPtCF+puwXoj+Ghg==\n-----END EC PRIVATE KEY-----\n"
# Load the token from permanent storage
token = "CQGVisa1ELtHqz4cJhmr5bZ8STkApNab7982vMJ3teAn"
# Create the client
client = BitPay::SDK::Client.new(api_uri: 'https://pay.littleq.net', pem: pem, tokens: {"merchant" => token})

This client is using the stored certificate and token and is now able to communicate programmatically with your btcpayserver.

Create a test invoice

When creating an invoice, it’s important to add the facade: ‘merchant’, otherwise it’ll default to the unauthenticated facade ‘pos’.

> client.create_invoice(price: 1, currency: 'BTC', facade: 'merchant')

Hope this was useful. If you have questions feel free to hit me up on twitter @pablof7z https://twitter.com/pablof7z

Pablo Fernandez

Written by

Climber. Wrote a Linux email client when I was 14. Bootstrapped two profitable startups. I create because I care.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade