Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First, let us initialise the NodeJs server. Open your terminal and type the following command:

$ npm init

Fill the the required information according to your specifications.

Next, let us install Express. In the same terminal, type the following:

...

  1. Cross Origin Resource Sharing (CORS), allows the api’s resources to be accessed from a different domain. In this case, our front-end will be able to access the api’s resources.

  2. Body parser converts incoming body request to a javascript object.

  3. Helmet adds a layer of security to our api. It protectes protects applications from attacks such as XSS, Content Security Policy to mention but a few. It ensures the right response HTTP headers are set.

  4. Nodemon - Automatically restart the server after every save.

...

The explanation will come after the code.

Code Block
languagejs
const express = require("express");
const bodyParser = require('body-parser');
const cors = require("cors");
const helmet = require("helmet");

// initialise the express app
const app = express();

/**
 * Fake database of our products. Replace with your database query.
 */
const products = [
    {
        id: 1,
        name: "iPhone 11",
        price: 1500000,
        quantityAvailable: 6
    },
    {
        id: 2,
        name: "JBL speaker",
        price: 300000,
        quantityAvailable: 6
    },
    {
        id: 3,
        name: "New King James Bible",
        price: 6000000
    }
];

// use helmet
app.use(helmet());

// use the body parser
app.use(bodyParser.json());

// next enable cors for all requests
app.use(cors());

app.get('/api/product', (request, response) => {
    response.send(products).status(200);
})

app.listen(3000, () => {
 console.log("Server running on port 3000");
});

...

Congratulations! Your api is functional, we will build the front-end view in the coming sections and render the products in a nice view. Obviously in a production environment or an MVP, the route would be protected by authentication. Since our focus is integrating with Monnify, this will suffice. Next let us handle the integration.

...

  1. One Time Payments - A payment can be initiated using the APIs, web and mobile (android/ios) sdkSDKs. A dynamic account number is generated with each initialisation initialization such that a bank transfer to the account, credits your wallet with Monnify. Card payments are also supported.

  2. Customer Reserved Accounts - Generate a permanent virtual account for your dedicated customer. A bank transfer to this account credits your wallet with Monnify.

  3. Invoice Generation - Generate invoices for your customers while they pay for your goods and services.

...

You might need to add some logic in your code to determine if your environment is dev, staging or production and use the appropriate base url.⚠️ Caution:

Note

.env file may contain sensitive information, such as API keys or secrets. Thus, add it to a project's .gitignore file to prevent it from being committed to version control

Replace the asterisks with the actual values from your dashboard and add the following code to the top of your entry file (ours is index.js).

...