Universal JavaScript client for Swell's Frontend API, providing client-safe access to store and customer data. You can use it in JAMstack or SSR apps to:
- Fetch products, categories, store settings, nav menus, and custom content
- Create, recover, and update shopping carts
- Build custom checkout and subscription flows
- Authenticate customers and allow them to edit account details, orders, and subscriptions
- Resolve linked content to dynamically generate page URLs
- Format prices in the store's currency
This SDK implements a subset of operations available in Swell's Backend API and is authorized with a public key + session token, making it safe to use in any context. You should only use the Backend API server-side, and keep your secret keys stored as environment variables.
About Swell
Swell is an ecommerce platform with subscriptions and B2B pricing built in. Run your store on a Swell theme, or go headless with its APIs.
Besides createElements, tokenize and handleRedirect (see the documentation), swell.payment offers helpers a checkout page needs:
tokenize({ card: { createIntent: false } })stores a Stripe card without authorizing it now.tokenize({ card: { paymentMethod: 'pm_…' } })authorizes a Stripe card the shopper already has (a saved card, a wallet's payment method) with an intent and 3-D Secure, like a new card, before the order is submitted. Both reuse the cart's intent while it still matches the card and amount.- Saferpay card and TWINT (
twint) redirect to the Saferpay payment page fromtokenizeand come back throughhandleRedirect; iDEAL returns throughhandleRedirect({ ideal })too. - Stripe iDEAL needs no element:
tokenize({ ideal: {} })withoutcreateElementsredirects to the iDEAL page, where the shopper picks the bank, and returns throughhandleRedirect({ ideal }). handleAsyncRedirect(order)sends the shopper to the gateway when a submitted order waits for an asynchronous payment.setupPaymentMethod({ card })saves a card without a cart, e.g. for an account page. On Stripe it mounts the Payment Element in setup mode with the store currency, andconfirm()calls the host's requiredgetSetupIntent()for{ client_secret }and confirms that SetupIntent with 3-D Secure. Any other card gateway mounts nothing:confirm(fields)tokenizes the host's own card fields throughcard.createToken. Both resolve{ gateway, token, brand, last4, exp_month, exp_year }for the host to save (Stripe addstest: trueoutside live mode); a Stripe error keeps itscode,typeanddecline_code.destroy()removes the mounted fields, e.g. when the host's effect re-runs; it does nothing for the host's own fields. Quickpay, Saferpay and ConvesioPay reject withcode: 'unsupported_gateway'.- Stripe card and iDEAL can render through the Stripe Payment Element instead of the split elements: pass
paymentElement: trueto bothcreateElementsandtokenize. Stripe draws the whole form (fields, labels, errors) and takes its look fromappearance(Appearance API). The element mounts into#payment-elementunlesselementIdis set;optionsgo toelements.create('payment')over the defaultslayout: 'tabs',fields.billingDetails: 'never'(billing details come from the cart) and no Apple Pay / Google Pay.tokenizere-reads the cart and updates the amount beforeelements.submit(), so a changed cart total needs no extra call.
const card = {
paymentElement: true,
appearance: {
theme: 'stripe',
labels: 'floating',
variables: { colorPrimary: '#111' },
},
elementId: 'card-payment-element',
onChange: (event) => setComplete(event.complete),
onSuccess: () => submitOrder(),
onError: (error) => showError(error.message),
};
await swell.payment.createElements({ card });
// on "Pay": validates, creates the PaymentMethod and the intent, stores them on the cart
await swell.payment.tokenize({ card });
// iDEAL: the same flag; tokenize redirects to the bank and returns through handleRedirect({ ideal })
await swell.payment.createElements({
ideal: {
paymentElement: true,
appearance,
elementId: 'ideal-payment-element',
},
});swell.app(app components rendered by swell-js) is deprecated and will be removed in the next major version. Apps render their components through@swell/apps-sdk/componentsinstead; hosted checkout mounts payment components at the placecheckout.payment.methods.