App
React
Add the SDK
- 1
Set up a React application using Vite
Run the following commands to create a new React application using Vite:
terminalnpm create vite@latest monocle-react -- --template react-tscd monocle-reactnpm install - 2
Install @spur.us/monocle-react
Run the following command to install the SDK:
terminalnpm install @spur.us/monocle-react - 3
Set your Monocle API keys
Add your Monocle Publishable Key to your
.envfile or create it if it doesn't exist..envVITE_MONOCLE_PUBLISHABLE_KEY=<MONOCLE PUBLISHABLE KEY> - 4
Add MonocleProvider to your app
Wrap the section from which you wish to access Monocle with the
MonocleProvidercomponent.tsximport { StrictMode } from "react";import { createRoot } from "react-dom/client";import "./index.css";import App from "./App.tsx";import { MonocleProvider } from "@spur.us/monocle-react";const PUBLISHABLE_KEY = import.meta.env.VITE_MONOCLE_PUBLISHABLE_KEY;if (!PUBLISHABLE_KEY) {throw new Error("Add your Monocle Publishable Key to the .env file");}createRoot(document.getElementById("root")!).render(<StrictMode><MonocleProvider publishableKey={PUBLISHABLE_KEY}><App /></MonocleProvider></StrictMode>,);
Session-Level Analysis (Optional)
- 5
Add useMonocle to your form component
Use the
useMonoclehook to get the assessment and pass it with your form submission. Monocle appends the encrypted assessment when you include it in the request body.tsximport { useMonocle } from "@spur.us/monocle-react";export default function Form() {const { assessment, isLoading } = useMonocle();const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {e.preventDefault();const formData = new FormData(e.target as HTMLFormElement);await fetch("/api/submit", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({email: formData.get("email"),password: formData.get("password"),monocle: assessment,}),});};if (isLoading) return <div>Loading...</div>;return (<form onSubmit={handleSubmit}><input type="email" name="email" /><input type="password" name="password" /><button type="submit">Submit</button></form>);} - 6
Install the backend package
Use the
@spur.us/monocle-backendpackage to decrypt the assessment in your Node.js backend.terminalnpm install @spur.us/monocle-backend - 7
Add your secret key
Set your Monocle secret key as an environment variable for server-side decryption.
.envVITE_MONOCLE_PUBLISHABLE_KEY=<MONOCLE PUBLISHABLE KEY>MONOCLE_SECRET_KEY=<your-secret-key> - 8
Decrypt the assessment
Decrypt the assessment in your backend using the monocle client.
javascriptconst { createMonocleClient } = require("@spur.us/monocle-backend");const express = require("express");const app = express();app.post("/api/submit", express.json(), async (req, res) => {const { monocle } = req.body;try {const monocleClient = await createMonocleClient({secretKey: process.env.MONOCLE_SECRET_KEY,});const decryptedAssessment = await monocleClient.decryptAssessment(monocle);// Handle decrypted assessment} catch (error) {// Handle error}});
Related
- Monocle Assessment — Understand the decrypted assessment payload
- Assessment Decryption — Decryption API and backend decryption options