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
-
Open your terminal.
-
Create a new folder for the project:
mkdir hyperswitch
-
Navigate to the folder:
cd hyperswitch
-
Initialize a Node.js project by running:
npm init -y
This will generate a package.json file that manages your project’s dependencies and configurations.
-
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}`); });
-
Save and exit the editor (in Vim, this would be :wq).
-
Install Express by running:
npm i express
-
Run the app and open your browser at http://localhost:3000/. You should see "Hello World!" displayed.
Step 2: Connecting to Hyperswitch
Now that you have a basic app running, let’s connect it to Hyperswitch.
Sign up for Hyperswitch.
For a smoother experience, it’s best to complete the sign-up process on a desktop or laptop.-
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.
-
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}`); });
-
Insert your API key by replacing with the actual key from Hyperswitch:
headers: { 'api-key': 'snd_xxxxxxxxxxxxx', 'Content-Type': 'application/json' }
-
Save the changes to app.js and run the server:
node app.js
-
Refresh your browser and check the terminal. You should see your first customer entry logged!
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)