DEV Community

Cover image for Verify User Identity with a Face Comparison API
AI Engine
AI Engine

Posted on • Originally published at ai-engine.net

Verify User Identity with a Face Comparison API

How do you know the person signing up for your service is who they claim to be? Email verification confirms someone controls an email address, but it says nothing about their real identity.

A face comparison API closes this gap by comparing a live selfie against a reference photo — whether that's a government-issued ID, an existing profile picture, or a previously enrolled face.

In this guide, I'll show how to implement identity verification for KYC onboarding, profile verification, and fraud prevention using a face comparison endpoint.

How It Works

The /compare-faces endpoint takes two images — a source (reference photo) and a target (photo to verify) — and returns whether they contain the same person:

// Match found
{
  "statusCode": 200,
  "body": {
    "matchedFaces": [
      { "boundingBox": { ... }, "landmarks": { ... } }
    ],
    "unmatchedFaces": []
  }
}

// No match
{
  "statusCode": 200,
  "body": {
    "matchedFaces": [],
    "unmatchedFaces": [
      { "boundingBox": { ... }, "landmarks": { ... } }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

When matchedFaces contains results → same person. When only unmatchedFaces → faces don't match.

Use Case 1: KYC Identity Verification

The user uploads their ID photo, then takes a selfie. The system compares both to confirm identity.

import requests

BASE = "https://faceanalyzer-ai.p.rapidapi.com"
HEADERS = {
    "x-rapidapi-host": "faceanalyzer-ai.p.rapidapi.com",
    "x-rapidapi-key": "YOUR_API_KEY",
}

def verify_identity(id_photo_path: str, selfie_path: str) -> dict:
    with open(id_photo_path, "rb") as id_file, open(selfie_path, "rb") as selfie_file:
        response = requests.post(
            f"{BASE}/compare-faces",
            headers=HEADERS,
            files={
                "source_image": ("id.jpg", id_file, "image/jpeg"),
                "target_image": ("selfie.jpg", selfie_file, "image/jpeg"),
            },
        )
    result = response.json()
    matched = result["body"]["matchedFaces"]
    return {
        "verified": len(matched) > 0,
        "matched_count": len(matched),
    }

result = verify_identity("passport_photo.jpg", "user_selfie.jpg")
if result["verified"]:
    print("Identity verified — faces match")
else:
    print("Verification failed — faces do not match")
Enter fullscreen mode Exit fullscreen mode

Use Case 2: Social Media Profile Verification

Combat catfishing and fake accounts by comparing a live selfie against the user's profile picture:

def verify_profile(profile_image_url: str, selfie_path: str) -> dict:
    with open(selfie_path, "rb") as selfie_file:
        response = requests.post(
            f"{BASE}/compare-faces",
            headers=HEADERS,
            files={"target_image": ("selfie.jpg", selfie_file, "image/jpeg")},
            data={"source_image_url": profile_image_url},
        )
    result = response.json()
    matched = result["body"]["matchedFaces"]
    if matched:
        return {"verified": True, "badge": "verified_profile"}
    else:
        return {"verified": False, "reason": "selfie_does_not_match_profile"}
Enter fullscreen mode Exit fullscreen mode

This works for dating apps, freelance platforms, and any marketplace where user trust matters.

Use Case 3: Fraud Prevention at Checkout

Add a face verification step for high-value transactions:

def verify_transaction(user_id: str, selfie_path: str) -> bool:
    reference_url = get_user_reference_photo(user_id)
    with open(selfie_path, "rb") as f:
        response = requests.post(
            f"{BASE}/compare-faces",
            headers=HEADERS,
            files={"target_image": ("selfie.jpg", f, "image/jpeg")},
            data={"source_image_url": reference_url},
        )
    result = response.json()
    return len(result["body"]["matchedFaces"]) > 0

if is_high_risk_transaction(order):
    if verify_transaction(order.user_id, selfie):
        process_payment(order)
    else:
        flag_for_review(order)
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Photo quality: Ensure good lighting, no glare on IDs, face centered in selfies
  • Liveness detection: Add a blink/head turn check before comparison to prevent spoofing with printed photos
  • Clear feedback: When verification fails, guide the user to retake with better conditions (3 retry limit)
  • Privacy: Facial data is biometric under GDPR/CCPA — get explicit consent, delete after verification

Try It Out

The Face Analyzer API is available on RapidAPI with a free tier. The /compare-faces endpoint gives you a single building block for instant identity verification — upload two images, check the result, done.

👉 Read the full tutorial with more use cases

Top comments (0)