Every app that checks identity documents meets the same fraud sooner or later: an expired passport with a new expiry year, an ID card with a different birth date, a photo edited before upload. This tutorial shows how to detect a fake or tampered ID document in Python: one API call returns every field, every check the document can prove on its own, and a verdict your code can act on.

Everything was tested on official specimen documents published by governments (a Dutch passport and a Croatian ID card, fictional data, free to reuse). We edited copies of them the way a quick forgery is made, then sent each version to the API.

Want to verify ID documents in your app? Get a free key for the ID Document OCR API.

How tampering shows up in an ID document

  • Check digits: the MRZ has one digit after the document number, the birth date and the expiry date. Change a date and its digit no longer matches.
  • The composite digit: one last digit covers all of those fields and their digits together, so fixing one digit is not enough.
  • Printed text against the MRZ: the name, dates and sex are printed in the visual zone and encoded in the MRZ. Edit one and the two disagree.

The API reports each check in a verdict object: status (ok, review or rejected), mrz_checks, mismatched_fields, national_checks, missing_critical_fields, consistency_issues, and a separate is_expired flag. Each field also has a status: mrz_confirmed when the printed text and the MRZ agree, mismatch when they disagree, with the printed value in visual_value.

The script: turn the verdict into a decision

You need Python 3, requests and a RapidAPI key in RAPIDAPI_KEY. Save this as check_id.py:

import os import sys import requests API_URL = "https://id-document-ocr-verification.p.rapidapi.com/parse-id" HEADERS = { "x-rapidapi-key": os.environ["RAPIDAPI_KEY"], "x-rapidapi-host": "id-document-ocr-verification.p.rapidapi.com", } def parse_id(*paths): """Send 1 to 6 photos of the same document, get fields and a verdict.""" files = [("images", open(p, "rb")) for p in paths] r = requests.post(API_URL, headers=HEADERS, files=files, timeout=60) r.raise_for_status() return r.json()["result"] def tamper_signals(result): """List every reason the document cannot be trusted as is.""" verdict, fields = result["verdict"], result["fields"] signals = [] if not verdict["mrz_present"]: signals.append("no MRZ that passes its checks") for name, ok in verdict["mrz_checks"].items(): if not ok: signals.append(f"check digit failed: {name}") for name, ok in verdict.get("national_checks", {}).items(): if not ok: signals.append(f"national number invalid: {name}") for name in verdict["mismatched_fields"]: f = fields[name] signals.append(f"{name}: MRZ {f['value']}, printed {f['visual_value']}") for name in verdict["missing_critical_fields"]: signals.append(f"missing: {name}") signals += [str(issue) for issue in verdict["consistency_issues"]] return signals def decide(result): verdict = result["verdict"] if verdict["status"] == "ok": return "reject: expired" if verdict["is_expired"] else "accept" return "manual review: " + "; ".join(tamper_signals(result)) if __name__ == "__main__": for path in sys.argv[1:]: result = parse_id(path) expiry = result["fields"].get("expiry_date", {}).get("value") print(f"{os.path.basename(path)} expiry={expiry}") print(f" -> {decide(result)}") 

Output on two original specimens and six edited copies:

$ python check_id.py ids/*.jpg nl_original.jpg expiry=2024-03-09 -> reject: expired nl_expiry_2029.jpg expiry=2029-03-09 -> manual review: check digit failed: expiry_date; check digit failed: composite nl_expiry_2031_digit_fixed.jpg expiry=2031-03-09 -> manual review: check digit failed: composite nl_two_fields.jpg expiry=2029-03-09 -> manual review: check digit failed: document_number; check digit failed: expiry_date; check digit failed: composite nl_heavy_edit.jpg expiry=None -> manual review: no MRZ that passes its checks; missing: birth_date; missing: document_number; missing: expiry_date hr_original.jpg expiry=2002-12-12 -> reject: expired hr_printed_expiry_2027.jpg expiry=2002-12-12 -> manual review: expiry_date: MRZ 2002-12-12, printed 2027-12-12 hr_mrz_expiry_2028.jpg expiry=2028-12-12 -> manual review: expiry_date: MRZ 2028-12-12, printed 2002-12-12 

Both originals are authentic and expired, so they pass every check and are refused only for the date. None of the six edited copies is accepted.

Edit 1: change a date in the MRZ

The most common edit turns an expired document into a valid one. On the Dutch passport, which expired in March 2024, we changed the expiry year in the MRZ from 24 to 29.

Four versions of the MRZ line of the specimen passport: the original ok and expired, three edited versions in review with the failing check digits boxed

The expiry check digit still matches 2024, so expiry_date fails, and the composite fails with it. The API marks the new date mrz_unchecked and returns review.

Edit 2: recalculate the check digit

The algorithm is public, so a forger can recompute it. We changed the year to 2031 and fixed the expiry digit. The expiry check passes, but the composite digit was computed over the old date (check_digit comes from the passport MRZ tutorial):

from mrz import check_digit line2 = "SPECI20142NLD6503101F3103094999999990<<<<<84" print(check_digit(line2[21:27]) == line2[27]) # expiry: True composite = line2[0:10] + line2[13:20] + line2[21:43] print(check_digit(composite) == line2[43]) # composite: False 

The API agrees: composite false, status review.

Edit 3: every digit right, but the printed date disagrees

On the Croatian ID card, the API reads the printed expiry date as well as the MRZ. We changed only the printed date to 2027 in one copy. In another, we changed the MRZ to 2028 and recalculated every check digit, so every MRZ check passes.

Two edited copies of the Croatian specimen ID card, each reported as review with a mismatch on expiry_date

Both come back as review with expiry_date in mismatched_fields, and the field shows both values:

"expiry_date": { "value": "2028-12-12", "status": "mismatch", "visual_value": "2002-12-12" } 

This is why the photo matters: ask for the whole document, both sides of an ID card, so the printed zone can be compared.

Edit 4: too many changes

With the document number, birth year and expiry year changed together, the MRZ failed so many checks that the API stopped trusting it: mrz_present false, the three fields listed as missing, status review. The edited values are never returned as if they were valid.

Production tips

  • Do not tell the user which check failed: a neutral "we could not verify this document" stops anyone from using your app to test forgeries.
  • Limit retries: several review results for one account in a short time are a signal in themselves.
  • Send several photos: up to 6 images per call, such as front, back and a second shot of the data page.
  • Match the face too: compare a selfie with the document photo, as in the selfie to ID verification tutorial.
  • Keep the signals with the decision, so a reviewer always knows what was flagged.

Full tutorial with images: Detect a Fake or Tampered ID Document on ai-engine.net.

Next step

Get a free key on the ID Document OCR API page, save check_id.py and run it on a specimen. Then edit one digit of its MRZ and watch the verdict change.