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.
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.
npm i ondc-node --save
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"
});
const response = await instance.search({
"item": {
"descriptor": {
"name": "ABC Aata"
}
},
"fulfillment": {
"end": {
"location": {
"gps": "12.4535445,77.9283792"
}
}
}
})
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}));
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
ondc.apiKey = await ondc.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}`);
Async Function | Callback Function | Other Function |
---|---|---|
search | on_search | get_cancellation_reasons |
select | on_select | cancellation_reasons |
init | on_init | get_return_reasons |
confirm | on_confirm | return_reasons |
status | on_status | get_rating_categories |
track | on_track | rating_categories |
cancel | on_cancel | get_feedback_categories |
update | on_update | feedback_categories |
rating | on_rating | get_feedback_form |
support | on_support | feedback_form |