Frontend Example: Integrating Reclaim SDK in a React Application
This guide will walk you through the fundamental steps to integrate Reclaim's proof verification system into your React application.
Prerequisites
Before you begin, ensure you have:
- Installed the SDK (see Installation Guide)
- Your Application ID, Secret, and Provider ID from the Reclaim Developer Portal (opens in a new tab)
Implementation
Here's a simple React component that implements Reclaim verification with QR code support:
import { useState } from 'react';
import QRCode from 'react-qr-code';
import { ReclaimProofRequest } from '@reclaimprotocol/js-sdk';
function ReclaimDemo() {
// State to store the verification request URL
const [requestUrl, setRequestUrl] = useState('');
const [proofs, setProofs] = useState([]);
const getVerificationReq = async () => {
// Your credentials from the Reclaim Developer Portal
// Replace these with your actual credentials
const APP_ID = 'YOUR_APPLICATION_ID';
const APP_SECRET = 'YOUR_APPLICATION_SECRET';
const PROVIDER_ID = 'YOUR_PROVIDER_ID';
// Initialize the Reclaim SDK with your credentials
const reclaimProofRequest = await ReclaimProofRequest.init(APP_ID, APP_SECRET, PROVIDER_ID);
// Generate the verification request URL
const requestUrl = await reclaimProofRequest.getRequestUrl();
console.log('Request URL:', requestUrl);
setRequestUrl(requestUrl);
// Start listening for proof submissions
await reclaimProofRequest.startSession({
// Called when the user successfully completes the verification
onSuccess: (proofs) => {
if (proofs) {
if (typeof proofs === 'string') {
// When using a custom callback url, the proof is returned to the callback url and we get a message instead of a proof
console.log('SDK Message:', proofs);
setProofs([proofs]);
} else if (typeof proofs !== 'string') {
// When using the default callback url, we get a proof object in the response
console.log('Verification success', proofs?.claimData.context);
setProofs(proofs);
}
}
// Add your success logic here, such as:
// - Updating UI to show verification success
// - Storing verification status
// - Redirecting to another page
},
// Called if there's an error during verification
onError: (error) => {
console.error('Verification failed', error);
// Add your error handling logic here, such as:
// - Showing error message to user
// - Resetting verification state
// - Offering retry options
},
});
};
return (
<>
<button onClick={getVerificationReq}>Get Verification Request</button>
{/* Display QR code when URL is available */}
{requestUrl && (
<div style={{ margin: '20px 0' }}>
<QRCode value={requestUrl} />
</div>
)}
{proofs && (
<div>
<h2>Verification Successful!</h2>
<pre>{JSON.stringify(proofs, null, 2)}</pre>
</div>
)}
</>
);
}
export default ReclaimDemo;
How it Works
-
Component Setup: The component uses a single state variable
requestUrl
to store the verification URL. -
Verification Flow:
- When the button is clicked,
getVerificationReq
initializes the Reclaim SDK with your credentials - It generates a request URL and displays it both as text and as a QR code
- The component starts listening for proof submissions via
startSession
- Success and error callbacks handle the verification result
- When the button is clicked,
-
Display: The component shows:
- A button to start verification
- A QR code (when URL is available)
- The request URL in text form
Complete Example Code
For a complete working example of Reclaim Protocol integration in Next.js, check out our demo repository (opens in a new tab).
For better user experience, consider conditionally rendering the QR code based on the user's device. On desktop, showing a QR code allows users to easily scan with their mobile devices. On mobile devices, displaying the direct link is more useful since users can't scan QR codes from the same device.
🚨 Security Note 🚨 Never expose your APP_SECRET in frontend code. For production: 1. Keep credentials on your backend
2. Generate the request configuration server-side 3. Use fromJsonString()
on the frontend See the Fullstack
Example for secure implementation.
Usage and Next Steps
To use this component in your React application:
- Import it into your main App or desired parent component.
- Render it within your application's JSX.
This example provides a basic implementation. In a production environment, you'd want to add more robust error handling, loading states, and potentially move the SDK initialization to a more appropriate place in your application architecture.
Now that you've learned the basics, you can:
- Learn about Advanced Configuration options to customize the SDK for your specific needs.
- Review Best Practices for optimizing your Reclaim integration.
If you have any questions, don't hesitate to join our Telegram community (opens in a new tab) for support.
Happy coding with Reclaim Protocol in React!