Call Us NowRequest a Quote
Back to Blog
ONDC
October 27, 2023
15 min read

The Definitive Guide: Architecting a Scalable ONDC Seller App with a Headless ERPNext Backend

Induji Technical Team

Induji Technical Team

Content Strategy

The Definitive Guide: Architecting a Scalable ONDC Seller App with a Headless ERPNext Backend

Key Takeaways

  • Monolithic ERPs Fail ONDC: Directly exposing a traditional ERP like ERPNext to the ONDC network is a recipe for performance bottlenecks, security risks, and protocol inflexibility. The real-time, high-concurrency demands of ONDC require a decoupled architecture.
  • Headless is the Solution: A headless approach, where ERPNext serves as the backend "system of record" via APIs and a separate middleware layer handles ONDC protocol logic, is the optimal design pattern. This provides scalability, security, and agility.
  • The 5-Layer Blueprint: A robust architecture consists of five distinct layers: Core ERP (ERPNext), an ONDC Adaptation Middleware, a Caching/State Management Layer (Redis), an API Gateway, and an Observability Stack.
  • Middleware is a Must: The ONDC Adaptation Layer (middleware) is not optional. It's responsible for protocol translation, request signing, asynchronous webhook handling, and implementing ONDC-specific business logic that doesn't belong in the core ERP.
  • Asynchronicity is Key: ONDC is an asynchronous, state-driven protocol. Your architecture must rely on message queues and webhook handlers to process order updates (on_status, on_track, etc.) without blocking the main API threads or overloading the ERP.

The ONDC Paradigm Shift: Why Your Old Architecture Won't Work

The Open Network for Digital Commerce (ONDC) is not just another marketplace; it's a fundamental re-architecting of digital commerce in India. It replaces siloed, platform-centric models with a decentralized, open-protocol network. For businesses, this is a monumental opportunity to reach millions of customers across countless buyer apps. However, this opportunity comes with a significant technical challenge: your backend systems must be able to "speak" the ONDC protocol fluently, in real-time, and at scale.

Many businesses running on robust ERPs like ERPNext assume they can simply expose a few API endpoints and join the network. This is a critical miscalculation. The ONDC protocol, with its series of synchronous callbacks (search, select, init, confirm) and asynchronous webhooks (on_status, on_track), places unique demands on system architecture. A monolithic ERP, designed for internal workflows, will quickly crumble under the pressure of thousands of concurrent, sub-second API calls from the network.

This is where a modern architectural pattern becomes essential. In this definitive guide, we will lay out the technical blueprint for building a highly scalable, resilient, and future-proof ONDC Seller App by leveraging a Headless ERPNext architecture.

Diagram comparing a risky monolithic ERP integration with ONDC vs. a secure and scalable Headless ERPNext architecture with a dedicated middleware layer.

Understanding the ONDC Protocol's Architectural Demands

Before designing the solution, we must deeply understand the problem. The ONDC protocol's design implies several non-negotiable architectural requirements:

1. Real-Time, Low-Latency API Responses

The initial discovery and ordering flow (/search to /confirm) is a synchronous chain of requests. Buyer Apps and users expect near-instant responses. If your /search endpoint takes 5 seconds to query your ERP's entire item master, you've already lost the sale. The network mandates swift acknowledgements (ACK/NACK), meaning your system must be able to quickly validate a request and confirm its acceptance before processing it fully.

2. Decoupled Catalog and Inventory Management

Your master inventory resides in ERPNext's Stock Ledger. However, exposing this directly to every /search request from the entire ONDC network is inefficient and dangerous. A successful ONDC architecture must allow for a highly-optimized, often cached, public-facing catalog that is hydrated from the ERP but can be served independently. Inventory checks, especially for high-frequency items, need a more sophisticated, multi-tiered approach than a simple SELECT query on the ERP database.

3. Asynchronous State Management

Once an order is confirmed, the ONDC journey becomes asynchronous. Updates like "packed," "shipped," or "out_for_delivery" are pushed from your Seller App to the network via on_status calls. Your architecture cannot rely on ERPNext's UI-driven state changes. It needs a robust system of webhooks, message queues, and event handlers to reliably capture state changes in the ERP and translate them into the correct ONDC API calls, complete with retry logic.

4. Protocol Isolation and Security

The ONDC protocol will evolve. Tying your core ERP logic directly to the current version of the protocol creates technical debt. Furthermore, directly exposing your ERP to the public internet is a significant security risk. A proper architecture requires a "demilitarized zone" (DMZ) or an adaptation layer that handles all ONDC-specific communication, including the critical task of signing requests with your private key, insulating the core system of record.

The Solution: A 5-Layer Headless ERPNext Architecture

A headless architecture decouples the backend content and logic management system (ERPNext) from the frontend presentation and communication layer (the ONDC Seller App). This is the gold standard for building modern, scalable digital experiences, and it's perfectly suited for ONDC.

Our proposed blueprint consists of five logical layers, each with a distinct responsibility.

Detailed architectural diagram illustrating the 5 layers: ERPNext Core, Middleware, Caching Layer (Redis)
, API Gateway, and Observability Stack. It should show data flow for both a real-time 'search' request and an asynchronous 'on_status' update.

Layer 1: The Core System of Record - ERPNext

This is the foundation. ERPNext holds the ground truth for your business operations.

  • Responsibilities: Master data management (Items, Customers, Addresses), inventory levels (Stock Ledger), pricing logic (Pricing Rules), order processing (Sales Orders, Delivery Notes, Invoices), and GST compliance.
  • Key Configurations:
    • Item Master: Ensure all ONDC-required attributes are present, potentially using custom fields.
    • Warehouses: Accurately reflect fulfillment locations.
    • Server Scripts / Webhooks: Configure webhooks on Doctypes like Delivery Note and Sales Invoice to trigger asynchronous updates in the middleware layer.
  • Interface: ERPNext's powerful Frappe REST API is the primary interface for this layer.

Layer 2: The Brains - ONDC Adaptation Middleware

This is the heart of the headless architecture and the most critical custom-built component. It acts as an intelligent translator and traffic controller between ONDC and ERPNext.

  • Technology Stack: Choose a high-performance, asynchronous framework. Node.js (with Express/Fastify) or Python (with FastAPI) are excellent choices due to their non-blocking I/O, making them ideal for handling many concurrent network requests.
  • Core Responsibilities:
    • Protocol Translation: Convert incoming ONDC JSON schemas into API calls that ERPNext understands, and vice-versa.
    • Request Signing & Verification: Securely store your private key and sign all outgoing requests. Verify the signatures of all incoming requests from the ONDC Gateway.
    • Business Logic Orchestration: Implement ONDC-specific logic. For example, calculating estimated delivery times or checking serviceability for a given pincode before even touching the ERP.
    • Asynchronous Handler: Receive webhook events from ERPNext (e.g., "Delivery Note Created") and orchestrate the corresponding ONDC on_status API call.

Layer 3: The Accelerator - Caching & State Management

This layer prevents your ERP from becoming the bottleneck. It provides the speed and statefulness required for a smooth user experience.

  • Technology: An in-memory data store like Redis is indispensable here.
  • Use Cases:
    • Catalog Caching: Periodically fetch catalog data from ERPNext and store it in Redis. Serve all /search requests directly from this cache for millisecond response times. Implement a cache invalidation strategy (e.g., TTL, webhook-based updates) to ensure freshness.
    • Transaction State: When a transaction begins (/select), store its state (the "context") in Redis. This allows you to track an order's journey across multiple API calls without querying the database every time.
    • Message Queue: Use Redis Streams or a dedicated queue like RabbitMQ to handle asynchronous tasks. When ERPNext sends a webhook, the middleware pushes a job to the queue. A separate pool of workers processes this queue, making calls to the ONDC network. This makes your system resilient to network failures.

Layer 4: The Gatekeeper - API Gateway & Security

This layer is your public-facing entry point, protecting your entire infrastructure.

  • Technology: NGINX, Kong, Tyk, or cloud-native solutions like AWS API Gateway or Azure API Management.
  • Responsibilities:
    • SSL Termination: Handle all HTTPS traffic.
    • Rate Limiting: Protect your services from abuse and DoS attacks.
    • Authentication: Perform initial validation of ONDC requests (e.g., checking the Authorization header).
    • Request Routing: Direct incoming traffic (e.g., /search, /confirm) to the correct service in your middleware layer.
    • Load Balancing: Distribute traffic across multiple instances of your middleware for high availability.

Layer 5: The Watchtower - Observability Stack

You cannot manage what you cannot measure. For a mission-critical system like an ONDC integration, observability is not an afterthought.

  • Technology: The "PLG" stack (Prometheus for metrics, Loki for logs, Grafana for visualization) is a powerful open-source choice. OpenTelemetry provides a standardized way to generate traces.
  • Key Metrics to Monitor:
    • API Latency: Track the p95 and p99 latencies for all ONDC endpoints.
    • Error Rates: Monitor 4xx and 5xx HTTP status codes to quickly identify issues.
    • ERPNext API Health: Measure the response times and success rates of calls from your middleware to ERPNext.
    • Queue Depth: Keep an eye on your asynchronous message queue to ensure jobs are being processed in a timely manner.

Implementation Deep Dive: Tracing an ONDC /confirm Request

Let's walk through how these layers work together to handle a critical order confirmation flow:

  1. Request Ingress: The ONDC Gateway sends a signed /confirm request to your Seller App's public endpoint.
  2. Layer 4 (API Gateway): The API Gateway receives the request, terminates SSL, validates the Authorization header, and routes it to an available instance of the ONDC Adaptation Middleware.
  3. Layer 2 (Middleware):
    • The middleware first verifies the digital signature on the request using the ONDC registry's public key to ensure authenticity.
    • It checks the transaction_id against the Caching Layer (Layer 3) to prevent duplicate order processing.
    • It transforms the complex ONDC order JSON into a simplified structure required to create a Sales Order doctype in ERPNext. This includes mapping items, customer details, and pricing.
  4. Layer 1 (ERPNext):
    • The middleware makes a POST request to /api/resource/Sales Order in ERPNext.
    • This is a critical validation step. ERPNext's internal logic runs. It performs a final, atomic stock check. If the item is out of stock, the API call will fail. It validates pricing rules and customer credit limits.
  5. The Response Path:
    • Success: ERPNext successfully creates the Sales Order and returns a 201 Created with the new Order ID. The middleware immediately sends an ACK (on_confirm with a success payload) back through the Gateway to the ONDC network. It then stores the mapping (ondc_transaction_id -> erpnext_sales_order_id) in Redis for future reference.
    • Failure: ERPNext returns a 4xx or 5xx error (e.g., "Insufficient stock"). The middleware catches this and immediately sends a NACK (on_confirm with an error payload) to the ONDC network, providing a clear reason for the failure.
  6. Asynchronous Follow-up: Later, a warehouse employee in ERPNext creates a Delivery Note against this Sales Order. This action triggers a Frappe webhook. The webhook hits a specific endpoint in the middleware. The middleware creates a "shipment status update" job and places it on the Redis queue. A worker picks up the job, retrieves the order details using the ID mapping, constructs a signed /on_status request (with state: packed), and sends it to the ONDC network.

This entire flow is fast, secure, and resilient, ensuring that your core ERP is never the bottleneck while maintaining perfect data integrity.


Frequently Asked Questions (FAQ)

Q1: Why can't I just use a tool like Zapier or a simple proxy to connect ERPNext's API directly to the ONDC Gateway? A: This approach is extremely risky and will not scale. You would lack critical functionalities like request signing, caching for performance, proper error handling (ACK/NACK logic), state management for asynchronous updates, and the ability to isolate ONDC protocol logic from your core system. A simple proxy creates a brittle, unmanageable integration that will fail under real-world load.

Q2: How do we handle ONDC-specific pricing or promotions that aren't in our standard ERPNext Price Lists? A: This is a perfect use case for the middleware layer. The middleware can fetch the base price from ERPNext and then apply ONDC-specific rules on top of it before sending the response in the /on_select call. This logic lives in the middleware, keeping your core ERP pricing model clean and separated from channel-specific promotions.

Q3: What's the best practice for managing the private keys required for signing ONDC requests? A: Never hardcode private keys in your application code or commit them to a git repository. The best practice is to use a dedicated secret management service like HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault. Your middleware application should be granted IAM permissions to fetch the key at runtime. This ensures keys are encrypted at rest, rotated regularly, and access is tightly controlled and audited.

Q4: Can this architecture support multiple ONDC domains (e.g., retail and logistics) from a single ERPNext instance? A: Absolutely. The API Gateway can route requests based on the path (e.g., /ondc/retail/search, /ondc/logistics/search) to different modules or services within your middleware layer. The middleware can then apply the correct business logic and interact with the appropriate doctypes in the single ERPNext instance, making it a highly flexible, multi-domain solution.


Your Partner for a Scalable ONDC Integration

Building a robust, enterprise-grade ONDC Seller App is not just about protocol compliance; it's about strategic architecture. The headless ERPNext approach outlined here provides the performance, scalability, and agility required to succeed on the open network. Getting the architecture right from day one prevents costly re-platforming and ensures your business can handle the immense scale of ONDC.

Ready to build your high-performance ONDC integration? The architectural choices you make today will define your success tomorrow.

Contact the experts at Induji Technologies for a comprehensive architectural audit and implementation quote. Let's build your future on the Open Network together.

Related Articles

Ready to Transform Your Business?

Partner with Induji Technologies to leverage cutting-edge solutions tailored to your unique challenges. Let's build something extraordinary together.

The Definitive Guide: Architecting a Scalable ONDC Seller App with a Headless ERPNext Backend | Induji Technologies Blog