Skip to main content

Initialization

  1. Create index.js next to your index.html
  2. Import it to your index.html <script src="index.js" type="module"></script>
  3. Import SDK to your index.js import VeryfiLens from "./veryfi-lens-wasm/veryfi-lens.js"
  4. Set process url and your credentials from Authentification
const PROCESS_DOCUMENT_URL = "https://lens.veryfi.com/rest/process" or "https://lens-dev.veryfi.com/rest/process";
const CLIENT_ID = "YOUR_CLIENT_ID";
const API_KEY = "YOUR_API_KEY";
const USERNAME = "your.username";
  1. Add function to validate client and receive session token (it is necessary to initialize Lens)
async function validatePartner(clientId) {
const validateUrl = "https://lens.veryfi.com/rest/validate_partner";
try {
const requestOptions = {
method: "POST",
headers: { "CLIENT-ID": clientId },
};
const response = await fetch(validateUrl, requestOptions);
const data = await response.json();
return data.session;
} catch (error) {
console.error("Error validating partner:", error);
return null;
}
}
  1. Call it const sessionToken = validatePartner(CLIENT_ID)

  2. Call const deviceData = await VeryfiLens.getDeviceData() (Is reqiured to send an image to our backend)

  1. Add Drag and Drop area to your index.html
 <div id="upload-area"
class="upload-area"
ondrop="drop(event)"
ondragover="dragOver(event)"
ondragleave="dragLeave(event)"
onclick="triggerInput()">
Drag and Drop or Click here to upload receipt
<input
id="file-input"
type="file"
accept="image/*"
onchange="handleImageChange(event)"
class="hidden"
/>
</div>
  1. Add functions to take care of user's actions

let imageFile
let base64Image

window.dragOver = (event) => {
event.preventDefault();
event.stopPropagation();
};

window.dragLeave = (event) => {
event.preventDefault();
event.stopPropagation();
};

window.drop = (event) => {
event.preventDefault();
event.stopPropagation();
const files = event.dataTransfer.files;
if (files.length > 0) {
processFile(files[0]);
}
};

window.triggerInput = () => {
document.getElementById("file-input").click();
};

window.handleImageChange = (event) => {
const file = event.target.files[0];
imageFile = file;
if (file) {
processFile(file);
}
};

function processFile(file) {
const reader = new FileReader();
reader.onload = (e) => {
const imgData = e.target.result;
base64Image = imgData;
document.getElementById("upload-image").src = base64Image; //Add <img id="upload-image" /> to your html to show users document
};
reader.onerror = (error) => {
console.error("Error reading file:", error);
};
reader.readAsDataURL(file);
}

  1. Add crop function const data = await VeryfiLens.captureUploaded(imageFile) (Pass it a blob with users image, it will return cropped image in base64)

  2. Call await VeryfiLens.initUploadWasm(sessionToken, CLIENT_ID)