✨Works out of the box guarantee. If you face any issue at all, hit us up on Telegram and we will write the integration for you.

JS
Backend Example (Node.js)

Backend Example: Integrating Reclaim SDK in a Node.js Application

This guide demonstrates how to integrate the Reclaim Protocol JavaScript SDK into a Node.js backend application using Express. We'll create a simple server that generates json configuration for sdk for frontend to use and handles proof verification.

This example uses Express.js, but the core concepts can be applied to other Node.js frameworks or vanilla Node.js.

Step-by-Step Implementation

1. Set Up Your Project

First, make sure you have Node.js installed and create a new project:

mkdir reclaim-backend-demo
cd reclaim-backend-demo
npm init -y
npm install express @reclaimprotocol/js-sdk

2. Import Dependencies

Create a new file named server.js and import the necessary dependencies:

const express = require('express')
const { ReclaimProofRequest } = require('@reclaimprotocol/js-sdk')

3. Initialize Express App

Set up your Express application:

const app = express()
const port = 3000
 
app.use(express.json()) // Middleware to parse JSON bodies

4. Create Route to Generate Request Configuration

Implement a route that initializes the SDK and generates a JSON configuration string:

app.get('/reclaim/generate-config', async (req, res) => {
  const APP_ID = 'YOUR_APPLICATION_ID' // Replace with your application ID
  const APP_SECRET = 'YOUR_APPLICATION_SECRET' // Replace with your application secret
  const PROVIDER_ID = 'YOUR_PROVIDER_ID' // Replace with your provider ID
 
  try {
    const reclaimProofRequest = await ReclaimProofRequest.init(APP_ID, APP_SECRET, PROVIDER_ID)
    
    reclaimProofRequest.setAppCallbackUrl('https://your-backend.com/receive-proofs')
    
    const reclaimProofRequestConfig = reclaimProofRequest.toJsonString()
 
    return res.json({ reclaimProofRequestConfig })
  } catch (error) {
    console.error('Error generating request config:', error)
    return res.status(500).json({ error: 'Failed to generate request config' })
  }
})
⚠️

In a production environment, it's crucial to store sensitive information like APP_SECRET securely. Use environment variables or a secure secret management system to handle these credentials. Never hardcode sensitive information directly in your source code.

Client-Side Usage

On the client side, you can use the fromJsonString() method to reconstruct the ReclaimProofRequest object and generate the request URL and status URL. Here's an example of how you might use this in a frontend application:

import { ReclaimProofRequest } from '@reclaimprotocol/js-sdk'
 
async function initializeReclaim() {
  // Fetch the configuration from your backend
  const response = await fetch('https://your-backend.com/reclaim/generate-config')
  const { reclaimProofRequestConfig } = await response.json()
 
  // Reconstruct the ReclaimProofRequest object
  const reclaimProofRequest = ReclaimProofRequest.fromJsonString(reclaimProofRequestConfig)
 
  // Generate request URL and status URL
  const requestUrl = await reclaimProofRequest.getRequestUrl()
  const statusUrl = reclaimProofRequest.getStatusUrl()
 
  // Start the session for better UX
  await reclaimProofRequest.startSession({
    onSuccessCallback: (proofs) => {
      console.log('Verification success', proofs)
      // Handle successful verification
    },
    onFailureCallback: (error) => {
      console.error('Verification failed', error)
      // Handle verification failure
    }
  })
 
  // Use requestUrl and statusUrl as needed in your application
  console.log('Request URL:', requestUrl)
  console.log('Status URL:', statusUrl)
}
 
initializeReclaim()

This approach allows for a more flexible and secure implementation. The sensitive APP_SECRET remains on the server, while the client can still utilize all the SDK features.

5. Create Route to Receive Proofs

Implement a route to receive and process the proofs:

app.post('/receive-proofs', (req, res) => {
  const proofs = req.body
  console.log('Received proofs:', proofs)
  // Process the proofs here
  return res.sendStatus(200)
})

In a production environment, you'd want to implement more robust proof verification and error handling in this route.

6. Start the Server

Finally, start your Express server:

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`)
})

Complete Server Code

Here's the complete server.js file:

const express = require('express')
const { ReclaimProofRequest } = require('@reclaimprotocol/js-sdk')
 
const app = express()
const port = 3000
 
app.use(express.json())
 
app.get('/reclaim/generate-config', async (req, res) => {
  const APP_ID = 'YOUR_APPLICATION_ID'
  const APP_SECRET = 'YOUR_APPLICATION_SECRET'
  const PROVIDER_ID = 'YOUR_PROVIDER_ID'
 
  try {
    const reclaimProofRequest = await ReclaimProofRequest.init(APP_ID, APP_SECRET, PROVIDER_ID)
    
    reclaimProofRequest.setAppCallbackUrl('https://your-backend.com/receive-proofs')
    
    const reclaimProofRequestConfig = reclaimProofRequest.toJsonString()
 
    return res.json({ reclaimProofRequestConfig })
  } catch (error) {
    console.error('Error generating request config:', error)
    return res.status(500).json({ error: 'Failed to generate request config' })
  }
})
 
app.post('/receive-proofs', (req, res) => {
  const proofs = req.body
  console.log('Received proofs:', proofs)
  // Process the proofs here
  return res.sendStatus(200)
})
 
app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`)
})

Usage and Next Steps

To use this server:

  1. Save the code in server.js.
  2. Run the server using node server.js.
  3. Access the /reclaim/generate-config endpoint to get a request reclaim sdk config.
  4. The server will receive proofs at the /receive-proofs endpoint.

This example provides a basic implementation. In a production environment, you'd want to add more robust error handling, implement proper security measures, and potentially use a database to store and manage proofs.

Enhancing the Implementation

Consider these improvements for a production-ready implementation:

  1. Implement proper authentication and authorization for your endpoints.
  2. Use environment variables to manage sensitive information like APP_SECRET.
  3. Add more comprehensive logging and error handling.
  4. Implement a database to store and manage received proofs.
  5. Consider using HTTPS for secure communication.

By following this example and considering these enhancements, you'll be well on your way to integrating the Reclaim Protocol into your Node.js backend. Happy coding!