Browser SDK

Hosted checkout for any website

Add a payment button, pass your payment account identifier, and let PayNexus handle the M-Pesa checkout flow in a secure hosted popup.

1

Add the CDN script

Load PayNexus from the CDN on your website.

2

Open hosted checkout

Pass accountId, amount, description, and redirect URLs.

3

Receive the result

Redirect the customer or unlock content after payment completes.

COPY AND ADAPT

Use your payment account identifier

The Browser SDK hosted checkout flow uses accountId, which is the identifier for the payment account receiving the money. This keeps API keys out of the page.

No server code required for basic checkout Hosted M-Pesa STK Push flow Supports return and cancel URLs
<script src="https://cdn.paynexus.co.ke/v1/paynexus.min.js"></script>
<script>
  const checkout = PayNexusHostedCheckout();

  document.getElementById('payBtn').addEventListener('click', async () => {
    try {
      const result = await checkout.open({
        accountId: 'ACC_ZVTDYYYL',
        amount: 100,
        description: 'Premium Article Access - AI Future',
        returnUrl: 'https://example.com/thanks.html',
        cancelUrl: 'https://example.com/cancelled.html',
      });

      console.log('Payment completed:', result.reference, result.amount);
      window.location.href = 'thanks.html';
    } catch (error) {
      if (error.code === 'cancelled') {
        window.location.href = 'cancelled.html';
      } else {
        alert('Payment initialization failed. Please try again.');
      }
    }
  });
</script>

Account identifier

Use the payment account identifier from your PayNexus dashboard, for example ACC_ZVTDYYYL.

Popup or redirect

Use checkout.open() for a popup or checkout.redirect() for full-page hosted checkout.

API keys stay private

The hosted checkout flow does not require publishing secret or public API keys in your HTML.

Build faster

Add checkout to your site today

Create a merchant account, copy your payment account identifier, and connect your first payment button.