Skip to main content

Build a Complete Checkout

This tutorial walks through building a complete checkout flow from scratch. Copy-paste ready, with all gotchas inline.

Prerequisites

  • Khaime API key (pk_live_xxx or pk_sandbox_xxx)
  • Next.js/React project (or adapt to your framework)

Step 1: Environment Setup


Step 2: API Client

Create a reusable API client with proper error handling.

Step 3: Build Cart Items Correctly

This is where most errors happen. The fields differ based on has_variation.

Step 4: Price Formatting (Multicurrency Safe)


Step 5: Complete Checkout Page


Key Takeaways

Cart Items

Use 'none' for product_variant_data on non-variant products. Only include main_variant and least_sub_variant_id when has_variation: true.

Currency Conversion

shipping_fee, total_sum_with_shipping_fee, and amount_to_pay are ALREADY CONVERTED. Pass skipConversion: true to formatPrice.

Shipping Options

Check available_destinations.length > 1 after cart validation. Show selector before payment if multiple options.

Payment Timing

Use useEffect to call confirmPayment AFTER the container renders. Pass the DOM element, not an ID string.

Common Errors Reference