@pybara/pybara-js
v0.16.0
Published
Take crypto donations with Pybara on any page — a callable checkout for your own donation form or Donate button, in the stripe-js / paypal-js convention.
Maintainers
Readme
@pybara/pybara-js
Take crypto donations with Pybara on any page — a callable checkout, in the same convention as @stripe/stripe-js and @paypal/paypal-js.
Donations only, for now.
checkout()runs the donation flow: thesiteUrlresolves a registered recipient, and the checkout collects the donor's details, optional anonymity and tax-receipt flag. It is not for selling goods or services yet — for a store, use Pybara for WooCommerce. Merchant payments will ride the same surface when they land: the site's registry record decides the flow, and the API won't change. Want the whole donation form instead — presets, amount selector, frequency picker, styled button? That's@pybara/donate-widget, built on the same machinery.
You keep your own donation form — its payment methods, amount selector, fields. Pybara.checkout() binds your site once; checkout.buttons() draws a "Pybara" button beside your other payment methods, or checkout.open() opens Pybara's checkout from your own Donate button. The checkout itself (wallet, token choice, settlement, success screen) is Pybara's.
Install
npm install @pybara/pybara-jsNo bundler? The IIFE build exposes window.Pybara — the same calls as the module:
<script src="https://cdn.jsdelivr.net/npm/@pybara/[email protected]/dist/pybara-js.iife.js"></script>
<script>
const pybara = Pybara.checkout({ siteUrl, siteName, recipientPrincipal, currency: 'EUR' });
pybara.buttons({ createPayment: () => ({ amount: '25' }) }).render('#pybara-button');
</script>Add Pybara next to PayPal
A donation form with PayPal Buttons gets a Pybara button beside them in a few lines. Pass the PayPal button's style as it is (color, shape, height, borderRadius) and the two match:
<div id="paypal-button-container"></div>
<div id="pybara-button"></div>import { Pybara } from '@pybara/pybara-js';
const pybara = Pybara.checkout({
siteUrl: 'https://example.com', // this site, as registered in Pybara Wallet
siteName: 'Example Org',
recipientPrincipal: 'aaaaa-bbbbb-…', // the site owner's wallet principal (see "Before it works")
currency: 'EUR',
});
pybara.buttons({
style: { color: 'black', shape: 'rect', height: 45 }, // the same style as the PayPal button
createPayment: () => ({ amount: amountInput.value }), // read on the click; add frequency: 'monthly' for a subscription
onStateChange: ({ message }) => (hint.textContent = message ?? ''), // e.g. "Minimum €0.70", in the donor's language
onSuccess: (result) => showThanks(result),
}).render('#pybara-button');
amountInput.addEventListener('input', () => pybara.precheck({ amount: amountInput.value }));- The button follows availability by itself: disabled below the minimum, and its slot left empty when Pybara can't be used on this page yet. It holds no text but "Pybara"; the page around it says what it's for.
- One-time and monthly side by side: call
buttons()twice on the same checkout, one withfrequency: 'monthly'. - Everything the donor sees is served by Pybara, so the button stays current without updating this package.
Full recipe: https://docs.pybara.com/pybara-js/next-to-paypal/
Before it works: the site owner's steps
Code can't do these; whoever adds Pybara to the site relays them to the site owner, once:
- Create a Pybara Wallet at https://pybara.io, then open Manage › Sites › Add a site without a plugin and enter this site's address.
- Publish the DNS
TXTrecord the wallet shows (_pybara-payto, holding the wallet's principal) with the domain's DNS provider, until the wallet shows Verified.- Accept the Pybara Terms of Service for the site there, and copy the wallet's principal into
recipientPrincipal.Until then the Pybara button stays hidden, and the browser console says which step is missing. It works on the site's registered domain, not on a preview URL.
Full guide: https://docs.pybara.com/pybara-js/setup/
Or your own Donate button: open()
import { Pybara } from '@pybara/pybara-js';
// Once, when the page loads — binds the site and preloads the checkout:
const pybara = Pybara.checkout({
siteUrl: 'https://example.com', // your site, as registered in Pybara Wallet
siteName: 'Example Org',
recipientPrincipal: 'aaaaa-bbbbb-…', // the wallet principal in your _pybara-payto DNS record
currency: 'EUR', // optional default for open(); one currency per site
});
// On your own button's click — opens the checkout over your page:
await pybara.open({
amount: '100', // in EUR, as a decimal string: '12.34', '1000' for JPY
donorEmail: '[email protected]', // optional — enables the receipt email + tax receipts
onSuccess: (result) => showThanks(result),
});That's the whole integration. open() resolves once the checkout is up; the result arrives on onSuccess.
Amounts are decimal strings in the currency's major units — '12.34' for €12.34, '1000' for ¥1,000 — as PayPal's API takes them; a number is accepted too (12.34). The checkout reads them with the currency's ISO 4217 minor units, so your page never needs to know how many decimals a currency has, and it refuses an amount the currency can't hold ('12.345' EUR, '1000.5' JPY). It converts to USD itself, with Pybara's own rates, and shows the minimum in the donor's currency if the amount is below it.
Monthly giving — same call, two extra params:
await pybara.open({ amount: '25', frequency: 'monthly', durationMonths: 12 });
// omit durationMonths for an open-ended subscriptionNo amount: the checkout asks
A tip jar, or a "Donate" button with no amount next to it: open the checkout without one, and it asks the donor how much, before the wallet.
const pybara = Pybara.checkout({ siteUrl, siteName, recipientPrincipal, currency: 'USD' });
donateButton.addEventListener('click', () => pybara.open({}));With suggested amounts, shown as buttons beside the amount field:
donateButton.addEventListener('click', () => pybara.open({ suggestedAmounts: ['5', '10', '25'] }));Built with AI? Ask the site owner first, rather than guessing:
- Which amounts to suggest, if any: up to four, in the site's currency. Without them, the donor types the amount; the checkout has no presets of its own.
- Which currency the donations are in, if the site doesn't say: it is required, set once in
Pybara.checkout(), or on eachopen()on a multi-currency site. Never guess it from the visitor. - Whether to offer monthly giving, and how: a second "Give monthly" button calling
open({ frequency: 'monthly' }), orfrequencyon the existing one. Withoutfrequency, donations are one-time.
Below the minimum, the checkout shows it in the donor's currency.
Full recipe: https://docs.pybara.com/pybara-js/donate-button/
Reference
Pybara.checkout(config) → PybaraCheckout
| Field | Type | Required | Description |
|---|---|---|---|
| recipientPrincipal | string | ✓ | The recipient's wallet principal — the one in the _pybara-payto DNS record |
| siteUrl | string | ✓ | Site registry key, e.g. 'https://example.com' |
| siteName | string | ✓ | Display name — shown in the checkout and stored on subscriptions |
| currency | string | | Default currency for open(), ISO 4217 uppercase ('EUR') — no default of its own |
| thankYouMessage | string | | Success-screen title override |
| locale | string | | The checkout's language, a BCP 47 tag ('fr', 'de-DE'), usually your site's; otherwise the donor's own |
| thankYouRedirectUrl | string | | Redirect after the donor closes the success screen |
| siteCountry | string | | ISO 3166-1 alpha-2 |
| platform | string | | Your integration's slug in the payment log, a-z 0-9 - (default 'pybara-js') |
| pluginVersion | string | | Set by plugins — skips the autonomous-embed terms gate |
| staging / debug | boolean | | Staging canisters + wallet / console logging |
| onAvailability(availability) | function | | Whether a donation would go through right now, before any click — see Availability |
checkout.open(payment) → Promise<void>
| Field | Type | Description |
|---|---|---|
| amount | string | number | Decimal string in the currency's major units: '12.34', '1000' (JPY). Omit it and the donor chooses (No amount: the checkout asks) |
| suggestedAmounts | (string \| number)[] | Without amount: up to four amounts offered as buttons beside the amount field |
| currency | string | ISO 4217 — required here unless set in Pybara.checkout(); a multi-currency site passes it on every call |
| frequency | 'weekly' \| 'monthly' \| 'yearly' | Omit for one-time |
| durationMonths | 3 \| 6 \| 12 \| 24 | Omit for unlimited |
| donorEmail donorFirstName donorLastName donorCompany donorCountry donorAddress* | string | Donor identity — pass what your form already collects |
| donorIsAnonymous | boolean | Hide identity on the public record |
| donorMessage | string | Stored with the payment log |
| wantsTaxReceipt | boolean | Prefills the receipt checkbox (eligible sites only) |
| onSuccess(result) | function | Donation confirmed — result carries block height, token, principals |
| onError(err) onClose() | function | Checkout refusals (EmbeddedError, err.code) / modal closed |
checkout.buttons(options) → { render(el), destroy() }
| Field | Type | Description |
|---|---|---|
| createPayment() | function | Required. Called on the click: the donation from the form, as open() takes it (amount, currency, frequency, donor fields), without callbacks. May return a promise |
| style | { color, shape, height, borderRadius } | color 'brand' | 'black' | 'white' (PayPal's names; its 'gold', 'blue', 'silver' read as 'brand'), shape 'pill' | 'rect' | 'sharp', height 25–55 px (default 45), borderRadius in px, overriding shape as PayPal's does. Passed to the button as given |
| hideWhenUnavailable | boolean | Collapse the button instead of leaving its slot empty when Pybara is unavailable |
| onStateChange(availability) | function | The checkout's availability answer, once per answer — see Availability |
| onSuccess(result) onError(err) onClose() | function | As open()'s |
render(el) takes an element or a selector, draws the button at the element's full width, and may be called again for another button; destroy() removes them all. Several buttons() calls share the one checkout. From the click until the checkout closes, the button shows "Processing…" in the checkout's locale, and takes no second click.
Availability
With onAvailability set, Pybara tells the page — before any click — whether a donation would go through, so you show the option only when it works:
const pybara = Pybara.checkout({
recipientPrincipal, siteUrl, siteName, currency: 'EUR',
onAvailability: ({ status, message }) => {
payButton.hidden = status === 'unavailable'; // the site isn't set up, or the currency isn't served
payButton.disabled = status === 'below-minimum'; // message: "Minimum €0.70", in the donor's language
hint.textContent = status === 'below-minimum' ? message : '';
},
});
amountInput.addEventListener('input', () => pybara.precheck({ amount: amountInput.value }));- Called once on its own when the checkout is ready (the site and its currency), then after each
checkout.precheck({ amount, currency }). Callprecheck()whenever the amount or currency changes; only the latest is answered. - Act on
status:available;unavailable(hide Pybara: nothing on the page can fix it);below-minimum(keep it, disable the button, showmessage;minimumis{ amount, currency });retry-later(keep it: the click tries again). messageis in the donor's language, ready to show.detail, in English, says what's missing and who fixes it — e.g. the site owner's DNS record or terms — and is printed to the console.reasonis for debugging only: never branch on it.- Without
onAvailability, nothing changes:open()checks everything at the click, as always.
Errors
open() rejects with a PybaraError (err.code) when the checkout can't open:
| Code | Meaning |
|---|---|
| GATE_REFUSED | Site hasn't accepted the Pybara Terms of Service, or a kill switch is on — err.message says which |
| RECIPIENT_SUSPENDED | Recipient can't accept donations right now |
| INVALID_CONFIG | Missing or malformed config or params — err.message names the field, e.g. open(): "currency" is required |
| CHECKOUT_FAILED | The checkout modal couldn't initialise |
Once open, the checkout reports a refusal to onError as an EmbeddedError, also with err.code. The ones an integration can cause: INVALID_CHECKOUT (the message names the field and what it must be, e.g. checkout.amount "12.345" has more decimals than EUR has (2)) and RATE_UNAVAILABLE (Pybara's rate for the currency couldn't be loaded — let the donor try again). An amount below the minimum isn't an error: the checkout shows the minimum itself.
If pybara.io can't be reached, the checkout doesn't spin forever: after 15 seconds without its first screen it says it couldn't load, with Try again and Close, and closing reports FRAME_UNAVAILABLE to onError, so the page can offer another way to give. A buttons() button that doesn't load in that time leaves its slot empty and reports unavailable to onStateChange.
Error reports
When the checkout can't open, or reports an error once open, pybara-js also tells Pybara, so a problem can be fixed before a site owner has to write in. Each report is one HTTPS request to Pybara's telemetry canister (POST /v1/reports on py6mw-3yaaa-aaaas-amwhq-cai.icp0.io, or its staging twin with staging: true), sent in the background: nothing waits for it, and a report that can't be sent is dropped.
A report carries the error's code and message, the site's address, the platform and plugin version you configured, and whether it was a donation. Nothing about the visitor: no principal, no amount, no donor details, and the canister stores no IP address. Configuration refusals and the checkout's refusals count as warnings, a checkout that couldn't load as an error; in production only errors are kept, and the same report repeated within a minute is sent once, with its count.
Internal entry points
@pybara/pybara-js/modal, @pybara/pybara-js/button and @pybara/pybara-js/admin are Pybara's own machinery for its plugins: not part of the public API, with no compatibility promise between versions. Integrate with Pybara.checkout().
Links
- Docs — https://docs.pybara.com
- Wallet — https://pybara.io
License
MIT. See LICENSE.
"Pybara" is a trademark of Pybara OÜ and is not licensed under the MIT License. Forks must remove Pybara branding.
