Integration Guide
Learn how to integrate Hyperswitch embeddable components into your React application using the SDK with JWT-based authentication for secure connector configuration.
Last updated
Was this helpful?
Was this helpful?
"dependencies": {
"hyperswitch-control-center-embedded": "github:juspay/hyperswitch-control-center-embedded"
}npm installconst express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
const port = 4000;
app.use(cors());
app.use(express.json());
const HYPERSWITCH_BASE_URL = 'https://app.hyperswitch.io/api';
app.get('/embedded/hyperswitch', async (req, res) => {
try {
// Call Hyperswitch to generate a temporary JWT for the frontend
const response = await axios.get(`${HYPERSWITCH_BASE_URL}/api/embedded/token`, {
headers: {
'api-key': 'YOUR_ACTUAL_API_KEY_HERE', // STORE IN ENV VARIABLES
'X-profile-id': 'YOUR_PROFILE_ID_HERE',
'Content-Type': 'application/json'
}
});
console.log('Hyperswitch Token Generated:', response.data);
// Return the token to your frontend
res.json({
success: true,
message: 'Token fetched successfully',
data: response.data
});
} catch (error) {
console.error('Error fetching token:', error.message);
res.status(500).json({
error: 'Failed to fetch token from Hyperswitch API',
details: error.message
});
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});import React, { useState } from 'react';
import './App.css';
import 'tailwindcss/tailwind.css';
import {
loadHyperswitch,
HyperswitchProvider,
ConnectorConfiguration,
} from 'hyperswitch-control-center-embedded';function App() {
const [errorMessage, setErrorMessage] = useState(null);
// Initialize the SDK instance once
const [hyperswitchInstance] = useState(() => {
// Define the token fetching logic
const fetchToken = async () => {
try {
// 1. Request token from YOUR backend (created in Step 2)
const response = await fetch('http://localhost:4000/embedded/hyperswitch', {
method: "GET",
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorData = await response.json();
const errorMsg = errorData.error || 'Network error fetching token';
console.error('Token fetch failed:', errorMsg);
setErrorMessage(errorMsg);
return undefined; // Signals the SDK that auth failed
}
const responseData = await response.json();
// 2. Extract the actual JWT string
// Check both data.token (standard) or root token property depending on your backend response structure
const token = responseData.data?.token || responseData.token;
console.log('Token received');
return token;
} catch (err) {
console.error('Exception during token fetch:', err);
setErrorMessage(err.message);
return undefined;
}
};
// Return the initialized loader
return loadHyperswitch({
fetchToken: fetchToken,
});
});
return (
<div className="h-screen bg-gray-100 p-10 text-gray-700">
{/* Error State Handling */}
{errorMessage ? (
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
<strong className="font-bold">Error: </strong>
<span className="block sm:inline">{errorMessage}</span>
</div>
) : (
/* Provider wraps the configuration component.
hyperswitchInstance is passed down here.
*/
<HyperswitchProvider hyperswitchInstance={hyperswitchInstance}>
{/* Render the actual UI.
'url' prop points to the Hyperswitch Dashboard API.
Use "https://app.hyperswitch.io/api" for Sandbox
*/}
<ConnectorConfiguration url="https://app.hyperswitch.io/api" />
</HyperswitchProvider>
)}
</div>
);
}
export default App;