Quick API Integration Guide for Khalti ePayment Checkout
Posted on June 27th, 2025
Khalti E-Payment
Payments
Quick Guide

๐Ÿ”Œ Quick API Integration Guide for Khalti ePayment Checkout

Khalti provides a seamless ePayment platform that enables merchants to accept digital payments easily. This guide walks you through the quick integration steps for the latest Khalti ePayment Checkout API.


๐Ÿง  How It Works

  1. User visits your website to make a purchase.
  2. A unique purchase_order_id is generated on your backend.
  3. You make a payment initiation request to Khalti with required details.
  4. User is redirected to the Khalti ePayment portal.
  5. Upon successful payment, user is redirected back to your return_url.
  6. Optionally verify payment using the Payment Lookup API.
  7. Provide service to the user only after payment is confirmed.

โš™๏ธ Getting Started

  • โœ… No SDK required โ€” just HTTP requests.
  • โœ… Use any language or framework that supports HTTP POST.
  • ๐Ÿง‘โ€๐Ÿ’ผ You must have a Khalti Merchant Account.
  • ๐Ÿ” Use sandbox credentials for testing.

๐Ÿงช Sandbox Login OTP: 987654

๐Ÿงช Test Khalti IDs:

9800000000 to 9800000005

๐Ÿงช Test MPIN: 1111


๐Ÿ”‘ API Authorization

All requests require an authorization header:

Authorization: Key <LIVE_SECRET_KEY>
  • Use keys from test-admin.khalti.com for sandbox
  • Use keys from admin.khalti.com for production

๐Ÿš€ Step 1: Initiate Payment Request

๐Ÿ“ก Endpoint

  • Sandbox: https://dev.khalti.com/api/v2/epayment/initiate/
  • Production: https://khalti.com/api/v2/epayment/initiate/

๐Ÿ“ Request Headers

Authorization: Key <YOUR_SECRET_KEY>
Content-Type: application/json

๐Ÿงพ Sample Request Body

{
  "return_url": "https://yourwebsite.com/payment/",
  "website_url": "https://yourwebsite.com/",
  "amount": 1300,
  "purchase_order_id": "order001",
  "purchase_order_name": "Premium Subscription",
  "customer_info": {
    "name": "John Doe",
    "email": "john@example.com",
    "phone": "9800000001"
  },
  "amount_breakdown": [
    { "label": "Base Price", "amount": 1000 },
    { "label": "VAT", "amount": 300 }
  ],
  "product_details": [
    {
      "identity": "sku123",
      "name": "Subscription Plan",
      "total_price": 1300,
      "quantity": 1,
      "unit_price": 1300
    }
  ]
}

โœ… Sample Success Response

{
  "pidx": "bZQLD9wRVWo4CdESSfuSsB",
  "payment_url": "https://test-pay.khalti.com/?pidx=bZQLD9wRVWo4CdESSfuSsB",
  "expires_in": 1800
}

๐Ÿ‘‰ Redirect the user to the payment_url


๐Ÿ”„ Step 2: Handle Payment Callback

After user completes or cancels the payment, they will be redirected to your return_url with these query parameters:

โœ… Sample Callback URL (Success)

https://yourwebsite.com/payment/?pidx=bZQLD9wRVWo4CdESSfuSsB
&transaction_id=4H7AhoXDJWg5WjrcPT9ixW
&status=Completed
&amount=1300
&purchase_order_id=order001
&purchase_order_name=Premium Subscription

โŒ Sample Callback URL (Canceled)

https://yourwebsite.com/payment/?status=User canceled

โœ… Step 3: Verify Payment with Lookup API

๐Ÿ“ก Endpoint

  • POST /epayment/lookup/

๐Ÿ“ Request

{ "pidx": "bZQLD9wRVWo4CdESSfuSsB" }

๐Ÿ” Sample Response

{
  "status": "Completed",
  "transaction_id": "TXN123",
  "total_amount": 1300
}
StatusAction
Completedโœ… Provide the service
Pending๐Ÿ•“ Wait and contact Khalti
User canceledโŒ Do not provide service
Refunded/ExpiredโŒ Do not provide service

๐Ÿšจ Error Handling

๐Ÿ” Invalid Token

{ "detail": "Invalid token.", "status_code": 401 }

โ“ Not Found (Wrong PIDX)

{ "detail": "Not found.", "error_key": "validation_error" }

๐Ÿงช Test it with cURL

curl --location 'https://dev.khalti.com/api/v2/epayment/initiate/' \
--header 'Authorization: Key <your_test_secret_key>' \
--header 'Content-Type: application/json' \
--data-raw '{
  "return_url": "http://example.com/",
  "website_url": "http://example.com/",
  "amount": 1000,
  "purchase_order_id": "order001",
  "purchase_order_name": "Test Product",
  "customer_info": {
    "name": "Test Bahadur",
    "email": "test@khalti.com",
    "phone": "9800000001"
  }
}'

โœ… Final Tips

  • Always verify payments using Lookup API after redirection.
  • Do not rely on callback alone.
  • Payment links expire after 60 minutes.
  • Only process orders with status: Completed.

Happy integrating! ๐Ÿ”ง
Need help? Contact Khalti Support or refer to the official documentation.