Flutter SDK
Biometry
Biometry's Flutter package that allows developers to integrate biometric authentication and verification into their Flutter applications using the Biometry API. This package simplifies the process of handling biometric data, making it easy to authenticate users securely.
Features
- Biometric Authentication: Easily authenticate users with biometric data such as voice and face recognition.
- Video Processing: Upload and process video files for biometric verification.
- API Integration: Seamlessly integrate with the Biometry API using a clean and straightforward Dart interface.
Getting Started
Prerequisites
Before you can start using the Biometry package, you need to:
- Obtain an API token from the Biometry service, and keep it on your backend (see proxy mode below).
- Ensure your Flutter environment is set up. You can check the official Flutter installation guide for help.
Installation
Add biometry to your pubspec.yaml:
dependencies: biometry: path: ../biometry # Replace with the path or pub.dev version if publishedThen, run:
flutter pub getUsage
Initialize the Biometry SDK
Use proxy mode (SDK 2.1.0+): point the SDK at your own backend with
baseUrl and leave the token out. The app then holds no Biometry token; your
backend authenticates your user, adds the token and forwards the request.
import 'package:biometry/biometry.dart';
Future<void> start(String appSessionToken) async { final biometry = await Biometry.initialize( baseUrl: 'https://app.example.com/biometry', userId: 'user-123', // Your own app's credentials, checked by your backend. headers: {'Authorization': 'Bearer $appSessionToken'}, );
// Now you can use the biometry instance to perform operations}Each request goes to baseUrl plus its own short path: the API v2 path
without Biometry’s /api-gateway/v2 prefix. So the session start calls
https://app.example.com/biometry/sessions?warmup=true, liveness calls
.../biometry/liveness, and so on (the defaults are in
Biometry.defaultProxyPaths). Your backend owns these routes and forwards each
one to the matching Biometry path, for example POST /liveness to
https://api.biometrysolutions.com/api-gateway/v2/liveness. Without a token
the SDK sends no Authorization header of its own.
To use your own route names, pass paths with the operations you want to move;
the others keep their defaults:
final biometry = await Biometry.initialize( baseUrl: 'https://app.example.com', userId: 'user-123', paths: { ProxyOperation.endSession: '/biometry-sessions/{session_id}/finish', },);Each path must start with /, must not contain ? or #, and must contain
exactly the placeholders of its default path once each ({session_id},
{consent_id}, {user_id}); otherwise initialize() throws an
ArgumentError before any request. Placeholder values are URI-encoded. Serve
the same new path on your backend and keep forwarding it to the default
Biometry path. In the reference proxy, change only the first path of the
route’s ROUTES entry and keep its upstream, for example
route('POST', '/biometry-sessions/:session_id/finish', { upstream: '/sessions/:session_id/end', ... }).
ROUTES writes a path parameter as :session_id; the SDK’s paths writes the
same parameter as {session_id}.
Your backend must accept only the SDK’s paths, rebuild each request with user_id set to the
signed-in user and only that user’s session_ids, and add
Authorization: Bearer <your API token>. Use headersProvider instead of
headers if your app token can be refreshed while the instance is alive.
headers, headersProvider and paths only apply with baseUrl; without it
they are ignored (with a debugPrint warning). Direct mode is unchanged.
Your backend also records the user’s consent (once, before enrollment, with
PUT /api-gateway/v2/users/{user_id}/consents) and owns the
liveness challenge: it issues the digits
for the session and writes its own copy into phrase on liveness and
voice-verify (face-verify takes no phrase, so the example proxy sends it
without one). In the app, fetch the challenge from
your backend and pass it to Biometry.setChallenge before showing the scanner,
so the user reads your backend’s digits, and give the recording (scanTime)
about 10 seconds for 8 digits:
final res = await http.post( Uri.parse('https://app.example.com/biometry/challenge'), headers: {'Authorization': 'Bearer $appSessionToken', 'content-type': 'application/json'}, body: jsonEncode({'session_id': biometry.sessionId}),);Biometry.setChallenge(jsonDecode(res.body)['challenge'] as String);Call Biometry.clearChallenge() if the user abandons the attempt. See
Use a challenge from your backend below.
See Backend proxy for client SDKs for the full table
of default paths, both jobs, and a minimal proxy. User ids must be 3 to 128
characters of A-Za-z0-9._:@-.
fullName is deprecated since 2.1.0 and is not sent to the API; drop it from
your initialize call.
Use a challenge from your backend
The digits the user reads should be a challenge your backend issues: we recommend 8 distinct digits from 1–9, from a secure random source, stored against the user’s login attempt, valid for at most 5 minutes and used once, with about 10 seconds of recording. Pass it to the SDK before showing the scanner (2.1.0+):
// challenge came from your backend, e.g. "48719352"Biometry.setChallenge(challenge);
BiometryScannerWidget( phrase: biometry.phraseWords, // "Four Eight Seven One Nine Three Five Two" onCapture: (video) => uploadVideoToYourBackend(video),);
// Without an instance, e.g. before initialize() has finished:Biometry.challengeWords(challenge); // same words, validatedsetChallenge accepts only the digits 0–9, 7 to 12 of them, and throws an
ArgumentError otherwise; it never swaps in other digits. biometry.phrase
returns the challenge unchanged. The challenge stays set across initialize
and across calls (one recording can go to livenessCheck and then
voiceVerify) until you call setChallenge again or Biometry.clearChallenge().
Call setChallenge for every login attempt, and clearChallenge when the user
abandons one. After clearChallenge there is no phrase, so phraseWords,
livenessCheck and voiceVerify throw a StateError until a challenge is set
again; faceVerify needs no challenge and keeps working.
| Challenge | What happens |
|---|---|
7 to 12 digits 0–9 passed to setChallenge | Used exactly as given |
Empty or invalid, passed to setChallenge | Throws an ArgumentError. It is never swapped for other digits. |
| Never set (omitted) | Deprecated: initialize generates digits on the device and prints a one-time warning per process (debugPrint). Existing apps keep working, with no replay protection. |
Your backend then sends its own stored copy as phrase to /v2/liveness.
A phrase chosen or returned by the device doesn’t stop replays: someone who
controls the device, or calls your backend directly, can pair a pre-recorded
video of the user with whatever digits that video happens to say, and every
lip-reading and voice check agrees. Only digits your backend chose, kept and compares against
prove the video was recorded just now. See
Liveness challenge for the rules and Node and Go
examples.
Record consent before enrolling
enrolFace() and enrolVoice() need the user’s authorization and
storage consent, or the API answers 403. Ask the user in your app first.
Since 2.1.0 the SDK records it with
PUT /v2/users/{user_id}/consents:
final status = await biometry.giveConsent(); // both consents; repeat calls are no-opsif (status.canEnrol) { await biometry.enrolFace();}
final current = await biometry.getConsentStatus();await biometry.revokeConsent(); // enrollment is refused again until giveConsent()Consent is stored per userId, so give it once per user, not per session. In
proxy mode your backend records consent instead (POST /onboarding/consent in
the reference proxy), so giveConsent(), getConsentStatus() and
revokeConsent() throw a StateError. See
Onboarding.
approveConsent() / getConsentApprovals() are for your own custom consent
templates (/v2/consents). They do not unlock enrollment. Before 2.1.0 the
SDK had no way to record the consent enrollment needs.
Run a liveness check
API v2 splits the old processVideo call into livenessCheck, faceVerify, and voiceVerify. Each call sends the userId you passed to initialize and the current sessionId, and returns the raw http.Response (non-2xx responses throw an Exception):
import 'dart:convert';import 'dart:io';
import 'package:biometry/biometry.dart';
Future<void> runLiveness(Biometry biometry, File video) async { // The user records `video` while saying the phrase shown by the app. final liveness = await biometry.livenessCheck(video: video); final body = jsonDecode(liveness.body) as Map<String, dynamic>; print('Decision: ${body['decision']?['status']}'); print('Request ID: ${body['meta']?['request_id']}');
// For a returning user, also match the face against their enrollment. final verify = await biometry.faceVerify(video: video); print('Face verify: ${verify.body}');
await biometry.endSession();}Run faceVerify and voiceVerify on the same video as livenessCheck, as
above: in a session, face and voice results count only from the liveness
media. Ending the session returns the session’s decision for the liveness,
face and voice checks together, which is the verification result. See Sessions.
More Information
For more information visit the api reference documentation.