Install the SDK
npm install @defchat-ltd/voicemark-sdk
The SDK is published to GitHub Packages. Add this to your
.npmrc before installing:@defchat-ltd:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GH_TOKEN}
GH_TOKEN must have read:packages scope. See SDK installation for details.Enrol a voice
import { VoiceMark } from "@defchat-ltd/voicemark-sdk";
const vm = new VoiceMark({ apiKey: process.env.VOICEMARK_API_KEY });
// Record audio (browser)
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const recorder = new MediaRecorder(stream);
const chunks: BlobPart[] = [];
recorder.ondataavailable = e => chunks.push(e.data);
recorder.onstop = async () => {
const blob = new Blob(chunks, { type: "audio/webm" });
const enroll = await vm.enroll(blob, userId);
console.log(enroll.voiceprint_id); // store this
console.log(enroll.quality_score); // 0–1; re-record if below 0.4
// enroll.glyph_params → render Glyph VoiceMark immediately
};
recorder.start();
setTimeout(() => recorder.stop(), 5000);
Verify (challenge + record + verify)
// 1. Get a challenge phrase
const { phrase, challenge_id } = await vm.challenge();
showToUser(phrase); // "Wild roses grew beside the wooden fence."
// 2. Record the user speaking the phrase (same approach as above)
// ...
// 3. Verify
const verify = await vm.verify(blob, {
voiceprintId: storedVoiceprintId,
challengeId: challenge_id,
});
if (!verify.authorized) {
throw new Error("Voice not recognised");
}
// 4. Use the token on your backend
await fetch("/api/secure-action", {
headers: { "X-VoiceMark-Token": verify.token },
});
Validate on your backend
// Your Express/Fastify/Next.js route handler
const check = await vm.token(tokenId, tokenJwt);
if (!check.valid) return res.status(401).json({ error: "Token expired" });