ondc-node

Integrate ONDC in your JavaScript applications easily.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
ondc-node
1221.1.68 months ago9 months agoMinified + gzip package size for ondc-node in KB

Readme

ondc-node
This library can be used to integrate ONDC in JavaScript based applications.
Package is developed in TypeScript and will work with Node.Js & other JavaScript based stacks.

Introduction 👋

ONDC stands for Open Network for Digital Commernce.
ONDC is an Open Network which makes it easy for any buyer and seller to engage in a transactions.
LOGO

How to install? 👀

npm i ondc-node --save

☝️ Using to call APIs

Initialization

const ondc = require('ondc-node');
const instance = new ondc.ONDC({
    host: "http://localhost:5000",
    bapId: "bap.com",
    bapUri: "https://bap.com/beckn",
    bppId: "bpp.com",
    bppUri: "https://bpp.com/beckn",
    country: "IND",
    city: "std:080",
    ttl: "P1M"
});

Search an Item

const response = await instance.search({
        "item": {
            "descriptor": {
                "name": "ABC Aata"
            }
        },
        "fulfillment": {
            "end": {
                "location": {
                    "gps": "12.4535445,77.9283792"
                }
            }
        }
    })

✌️ Using as Express Middleware

const ONDC = require('ondc-node');
const express = require("express");

const app = express();
app.use(express.json());

app.use(ONDC.Middleware({"on_search": onSearchHandler, "init": initHandler}));

🤩 Effortless Authorization 🔐 with ONDC

You can use ``ondc.createAuthorizationHeader` function to create `Authorization`` header signature.
const ondc = require('ondc-node');
// Private key & Public keys are must for Auth to work
const instance = new ondc.ONDC({
    host: "http://localhost:5000",
    bapId: "bap.com",
    bapUri: "https://bap.com/beckn",
    bppId: "bpp.com",
    bppUri: "https://bpp.com/beckn",
    country: "IND",
    city: "std:080",
    ttl: "P1M",
    publicKey: "<Public Key>",
    privateKey: "<Private Key>",
    uniqueKey: "dev.test.ondc-node.com",
    subscriberId: "<Gateway Address>",
});

let body = {
    "item": {
        "descriptor": {
            "name": "ABC Aata"
        }
    },
    "fulfillment": {
        "end": {
            "location": {
                "gps": "12.4535445,77.9283792"
            }
        }
    }
};
// apiKey is sent in Authorization Header
instance.apiKey = await instance.createAuthorizationHeader(body);
const response = await instance.search(body)

Next function is ``ondc.verifyHeader` which is used to verify the `Authorization`` header signature with senders Public Keys.
You can fetch the public key from Gateway using ``/lookup`` call.
const publicKey = "<Public key of Sender>";

// verifyHeader return true/false
let output = await ondc.verifyHeader(req.header.Authorization, {
    signing_public_key: publicKey
}, req.body);

console.log(`output: ${output}`);

Reference 📚

Functions Covered 🚀

| Async Function | Callback Function | Other Function | |:----------------:|:-------------------------:|:-------------------------:| | search | onsearch | getcancellationreasons |
| select | on
select | cancellationreasons |
| init | on
init | getreturnreasons |
| confirm | onconfirm | returnreasons | | status | onstatus | getratingcategories |
| track | on
track | ratingcategories |
| cancel | on
cancel | getfeedbackcategories |
| update | onupdate | feedbackcategories |
| rating | onrating | getfeedbackform |
| support | on
support | feedbackform |

Content