CredflareDevelopersOpen dashboard
Preview

JavaScript SDK

A server helper that creates cases and sessions, a typed session client, and browser screens for questions, the liveness challenge, the location step, and email and phone codes.

Not published yet. Build it from source on GitHub at credflare/javascript-sdk with npm ci and npm run build. The package name will be @credflare/sdk-javascript.

On your server

Keep this code and your client credentials on the server. The constructor refuses to run in a browser.

import {CredflareServer} from '@credflare/sdk-javascript/server';

const credflare = new CredflareServer(process.env.CREDFLARE_API_URL, {
  clientId: process.env.CREDFLARE_CLIENT_ID,
  clientSecret: process.env.CREDFLARE_CLIENT_SECRET,
});

const verification = await credflare.createVerification({
  email: '[email protected]',
  profileId: 123,
});
const session = await credflare.createSession(verification.id, [
  'https://app.example.test',
]);
// Store verification.id with your user. Return only session to the browser.

The second argument lists the browser origins allowed to use the session. Use exact HTTPS origins without a path. Native apps can omit it. Authenticate your own user before returning a session, and keep each user bound to their own case.

MethodCalls
createVerification({email, profileId, callbackUrl})POST /client/verifications
createSession(id, allowedOrigins)POST /client/verifications/{id}/sessions
revokeSession(sessionId)DELETE /client/verification-sessions/{session}
getVerification(id)GET /client/verifications/{id}

In the browser

Load the requirements the case's profile set, then build one step per side and source with captureSteps. It reads the capture method from the requirement, so a document set to snapshot_and_upload in the profile produces a camera step and an upload step for each side. Your code never chooses the method.

import {CredflareSession, captureSteps, methodSummary} from '@credflare/sdk-javascript';

const client = new CredflareSession(session);
const info = await client.get();
const requirement = info.profile.document_requirements[0];

methodSummary(requirement); // "Camera photos", "Saved files", ...
const steps = captureSteps(requirement);
// [{id: 'camera:front', side: 'front', source: 'camera', label: 'Front', sourceLabel: 'Camera photo'}, ...]

Upload each file, then submit the document with every file in evidence.

const evidence = [];
for (const step of steps) {
  const {file, source} = selected[step.id]; // source is 'camera' or 'upload'
  const body = new FormData();
  body.append('file', file);
  body.append('side', step.side);
  body.append('capture_source', source);
  body.append('document_type_id', String(requirement.document_type.id));
  const uploaded = await client.upload('documents', body, crypto.randomUUID());
  evidence.push({file_id: uploaded.id, side: step.side, source});
}
await client.submit({
  type: 'document_verification',
  document_type_id: requirement.document_type.id,
  evidence,
}, crypto.randomUUID());

Keep each request key with its payload until it succeeds, so a retry after a dropped connection does not create a duplicate. After a submission, call client.get() again: received documents drop out of document_requirements.

Use isCaptureSupported(requirement) before offering a document. An unsupported method should tell the applicant to contact you rather than open a capture screen.

Liveness and location

After the documents, mountRemainingSteps runs the case's outstanding facial and location requirements in profile order and ends with a receipt. It requests the liveness challenge, records the gesture frames, the video and the selfie, and submits them with the challenge ID.

import {mountRemainingSteps} from '@credflare/sdk-javascript/browser';
import {createMediaPipeProvider} from '@credflare/sdk-javascript/mediapipe';

const dispose = mountRemainingSteps(element, client, {
  provider: createMediaPipeProvider({
    wasmBaseUrl: '/mediapipe/wasm',
    modelAssetPath: '/models/face_landmarker.task',
  }),
  onDone: () => showFinished(),
});

To build your own screens, the client has createLivenessChallenge(key), uploadBiometric({type, challengeId, gesture, file}, key), submitFacial(submission, key) and submitLocation(submission, key). facialSettings and locationSettings read info.profile.verification_requirements and fill in the spec defaults. The package also exports the shared gesture engine and the generated spec constants and copy.

Questions, codes and rejections

Not yet on production: questions, email and phone codes, fetching the session after each submit, rejection screens with a help contact and continuing on a phone. They need API and SDK versions that are not released yet.

mountRemainingSteps also asks the profile's questions and confirms email addresses and phone numbers. The order is questions, documents, then the verification requirements in profile order.

const dispose = mountRemainingSteps(element, client, {
  provider: createMediaPipeProvider(),
  handoff: true,
  onLeave: code => host.needsAction(code),
  onDone: () => showFinished(),
});

Continue on a phone

With handoff: true, mountRemainingSteps offers "Continue on your phone". It creates a link with client.createHandoff(), shows it as a QR code (qrSvg) with the minutes left, offers a new code when it expires, and polls the session until the phone has finished, then shows the receipt. mountHandoff shows the QR step on its own. The link carries a single use token in its fragment: never log or store it. See how the handoff works.

Errors

Failures throw CredflareError with status, type (network, session_unavailable, verification_closed, validation or api), validation errors and the rejection code for a capture rule. See how to handle each status.

The API requires HTTPS, except on localhost. Applicant requests omit cookies.