All posts

How to Fix Payment Request Api Error in TypeScript

Learn how to diagnose and fix the payment request api error in TypeScript. Includes code examples and prevention tips.

Stumbled on a payment request api error in your TypeScript application? This common issue has a well-known fix that you can apply in minutes.

Understanding the Error

The Payment Request API error in TypeScript typically occurs when the browser doesn't support the API or when the payment method configuration is invalid. This surfaces as a runtime exception that can crash your entire checkout flow, leading to lost revenue.

Root Cause

Common triggers include:

  • Calling new PaymentRequest() in an unsupported browser or environment
  • Passing malformed methodData or details objects with missing fields
  • Omitting required fields like total in the payment details configuration
  • Testing on HTTP instead of HTTPS (the API requires a secure context)

The Fix

Always feature-detect before using the Payment Request API:

if (window.PaymentRequest) {
  const methods = [{ supportedMethods: 'basic-card' }];
  const details = {
    total: { label: 'Order Total', amount: { currency: 'USD', value: '10.00' } },
    displayItems: [
      { label: 'Item price', amount: { currency: 'USD', value: '10.00' } }
    ]
  };
  try {
    const request = new PaymentRequest(methods, details);
    const canMake = await request.canMakePayment();
    if (canMake) {
      const response = await request.show();
      await processPayment(response);
      response.complete('success');
    } else {
      showManualCheckoutForm();
    }
  } catch (err) {
    console.error('Payment Request failed:', err);
    showManualCheckoutForm();
  }
} else {
  showManualCheckoutForm();
}

Best Practices

  • Always provide a fallback checkout experience for unsupported browsers
  • Call canMakePayment() before show() to verify the user has a valid payment method
  • Validate payment detail objects before passing them to the API
  • Ensure your page is served over HTTPS

Use [Bugsly](https://bugsly.dev) to monitor payment flow errors in real time and catch checkout failures the moment they happen.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free