DEV Community

Brix Mavu
Brix Mavu

Posted on

Getting Started with Hyperswitch, Nodejs, and Express for Payment Processing

Introduction

Welcome to this step-by-step tutorial on using the Hyperswitch API with Express and Node.js! In this first part, we'll walk you through creating a basic app using Node.js and Express. Later, in part two, we’ll integrate multiple payment gateways with the help of Hyperswitch.

Why Hyperswitch?

Hyperswitch allows you to connect to various payment processors and payment methods, making it easier to manage transactions. Meanwhile, Node.js enables you to run JavaScript on the server side, and Express simplifies the process of building web applications and APIs.


Requirements

Before we start, make sure you have the following:

  • Node.js installed on your system
  • Hyperswitch API keys
  • A text editor (I’ll be using Vim on Termux, but feel free to use your favorite editor like VS Code)

Step 1: Create a "Hello World" Application

  1. Open your terminal.

    new terminal

  2. Create a new folder for the project:

    mkdir hyperswitch
    
  3. Navigate to the folder:

    cd hyperswitch
    
  4. Initialize a Node.js project by running:

    npm init -y
    

    package json

    This will generate a package.json file that manages your project’s dependencies and configurations.

  5. Create a server file.
    You can create it using your text editor’s UI or in the terminal:

    touch app.js
    

    Since I’m using Vim, I’ll run:

    vim app.js
    

    and paste the "Hello World" code from the Express documentation:

    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
    res.send('Hello World!');
    });
    
    app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
    });
    
  6. Save and exit the editor (in Vim, this would be :wq).

    vim in termux

  7. Install Express by running:

    npm i express
    
  8. Run the app and open your browser at http://localhost:3000/. You should see "Hello World!" displayed.

    app in browser


Step 2: Connecting to Hyperswitch

Now that you have a basic app running, let’s connect it to Hyperswitch.

  1. Sign up for Hyperswitch.
    For a smoother experience, it’s best to complete the sign-up process on a desktop or laptop.

  2. Get your API key from the settings. This key will allow your app to interact with the Hyperswitch API.

    Note: For this tutorial, we’ll paste the API key directly into our code. However, this is not recommended for production environments. We’ll cover a more secure method later.

  3. Update your code to create a customer through Hyperswitch. Replace the previous code in app.js with the following:

    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => 
    {
    /* Fetching the API key from 
    Hyperswitch to create a 
    customer */
      const options = {
      method: 'POST',
      headers: {
      'api-key': '<api-key>', 
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      email: 'guest@example.com',
      name: 'John Doe'
    })
    }; 
    fetch('https://sandbox.hyperswitch.io/customers', options)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));
    res.send('Customer creation initiated.');
    });
    
    app.listen(port, () => {
    console.log(`App listening on port ${port}`);
    });
    
  4. Insert your API key by replacing with the actual key from Hyperswitch:

    headers: { 'api-key': 'snd_xxxxxxxxxxxxx', 'Content-Type': 'application/json' }
    
  5. Save the changes to app.js and run the server:

    node app.js
    
  6. Refresh your browser and check the terminal. You should see your first customer entry logged!

    first entry


That’s it for part one! Now you’ve created a basic Express app and connected it to Hyperswitch to create customers. In the next part, we’ll dive deeper into handling payments and managing multiple gateways.

Stay tuned!

Top comments (0)