cyclestrand.js
A 3 KB vanilla snippet that opens hosted checkout or the customer portal over your own page. It only ever uses your publishable key.
1. Allow your origin
Add your site (for example https://app.example.com) under Billing settings, Hosted pages. Requests from other origins are refused with origin_not_allowed.
2. New customers: checkout
<script src="https://billing.example.com/cyclestrand.js"></script>
<script>
Cyclestrand.init({ publishableKey: 'cs_pk_...', baseUrl: 'https://billing.example.com' });
document.querySelector('#buy').onclick = function () {
Cyclestrand.openCheckout({ priceId: 'PRICE_ID', quantity: 5, successUrl: location.origin + '/thanks' });
};
</script>3. Existing customers: portal
A publishable key is public, so it cannot choose a customer. Your server mints a short-lived token for the logged-in customer with a secret key and hands it to the page:
# your server
curl -X POST https://billing.example.com/api/v1/customers/CUSTOMER_ID/token \
-H "Authorization: Bearer cs_sk_..."
# -> { "customer_token": "ctk_...", "expires_at": "..." }Cyclestrand.openPortal({ customerId: 'CUSTOMER_ID', customerToken: tokenFromYourServer });Tokens last one hour, work for one customer only and stop working when the customer's secret is rotated. The same customerId + customerToken pair works for openCheckout when an existing customer subscribes to another plan.