This tutorial uses the id-document-ocr API. See the docs, live demo, and pricing.
A rental counter, a car sharing app, a peer-to-peer rental platform: before the keys change hands, someone has to read the driver's license and decide. Is the driver old enough, does the class allow a car, and will the license still be valid on the day the car comes back? This tutorial reads US driver's licenses with an API in Python and turns those questions into one function that returns ok, review or deny.
It uses the ID Document OCR API. Every output below comes from live calls made on 29 November 2026 on three state sample licenses: California (2025 design), Massachusetts (2016) and Washington D.C. (2018).
One Numbering for Every State
Each state designs its own card, but licenses that follow the AAMVA card design standard number their fields the same way, and the numbers are the ones used on European licenses. A parser that knows where field 4b sits reads the expiration date on a California card and on a Polish one:

The license number is field 4d on a US card and field 5 on a European one, the only common field that moves. The API returns it as document_number in both cases. If you rent cars on both sides of the Atlantic, our EU driving licence tutorial covers categories, pre-2013 cards and the personal number checks.
The Script
Save this as us_license_check.py. It reads one license, applies the rental rules and prints the decision with its reasons. Set RAPIDAPI_KEY in your environment first.
import os
from datetime import date
import requests
KEY = os.environ["RAPIDAPI_KEY"]
HOST = "id-document-ocr-verification.p.rapidapi.com"
def parse_license(path):
"""Send the front of a US driver's license, return the API result."""
with open(path, "rb") as f:
r = requests.post(f"https://{HOST}/parse-id",
headers={"x-rapidapi-key": KEY, "x-rapidapi-host": HOST},
files={"images": f}, timeout=60)
r.raise_for_status()
return r.json()["result"]
def value(result, name):
v = result["fields"].get(name, {}).get("value")
return date.fromisoformat(v) if v and name.endswith("_date") else v
def age_on(birth, day):
return day.year - birth.year - ((day.month, day.day) < (birth.month, birth.day))
def classes(raw):
"""'C', 'D', 'DM' or 'A, M' as a set of single-letter classes."""
return {c for c in (raw or "").upper() if c.isalpha()}
def check_rental(result, return_day, today=None, min_age=21, young_until=25):
"""Return (decision, notes) for one car rental: ok, review or deny."""
today = today or date.today()
if result["document_type"] != "driving_license":
return "review", ["not a driver's license"]
if result.get("country") != "USA":
return "review", [f"issued in {result.get('country')}, use the EU rules"]
birth, issued = value(result, "birth_date"), value(result, "issue_date")
expiry = value(result, "expiry_date")
held = classes(value(result, "license_categories"))
checks = result["verdict"].get("national_checks", {})
deny, review, notes = [], [], []
if birth is None:
review.append("no date of birth")
else:
age = age_on(birth, today)
if age < min_age:
deny.append(f"driver is {age}, minimum {min_age}")
elif age < young_until:
notes.append(f"driver is {age}: young renter fee")
if not held:
review.append("license class not read")
elif held == {"M"}:
deny.append("motorcycle-only license (class M)")
if expiry is None:
review.append("no expiration date")
elif expiry < today:
deny.append(f"expired {expiry}")
elif expiry < return_day:
deny.append(f"expires {expiry}, before the car is back")
if checks.get("document_number_format") is False:
review.append("license number does not match the state format")
if birth and issued and age_on(birth, issued) < 16:
review.append(f"issued at age {age_on(birth, issued)}")
if issued and expiry and (expiry - issued).days > 12 * 366:
review.append("valid for more than 12 years")
if deny:
return "deny", deny
if review:
return "review", review
return "ok", notes or [f"class {', '.join(sorted(held))}, valid to {expiry}"]
if __name__ == "__main__":
import sys
return_day = date.fromisoformat(sys.argv[1])
for path in sys.argv[2:]:
decision, notes = check_rental(parse_license(path), return_day)
print(f"{os.path.basename(path):18} {decision:7}", "; ".join(notes))Step 1: Read the License
One call with the photo of the front returns every printed field:
result = parse_license("california.jpg")
for name, field in result["fields"].items():
if field["value"] is not None:
print(f"{name:20} {field['value']}")
print(result["verdict"]["national_checks"])On the 2025 California sample, in 1.7 seconds:
birth_date 1957-11-26
document_number I1234567
expiry_date 2030-11-26
first_name JANICE
issue_date 2025-11-09
last_name SAMPLE
license_categories A
sex F
{'document_number_format': True}document_number_format: True means the number matches the format California uses, one letter then seven digits. It is the check that catches an I read as a 1, the most common confusion on these cards. The Massachusetts sample passes the same check with its own format, S99988801.
Step 2: The Rental Rules
check_rental turns the fields into a decision. The thresholds are parameters, because every rental company sets its own:
- Age: under
min_age(21 by default) is a deny. Underyoung_until(25) the rental is allowed with a note, since most companies add a young renter fee. - Class: class letters change from state to state, so the script only denies a motorcycle-only license (
Malone). A combinedDMstill allows a car. - Expiration: the license must be valid until the return day, not only today.
- Review cases: a number that fails the state format check, a missing field, an issue date before age 16 or a validity longer than 12 years go to a person.
verdict.status from the API tells you the document was read cleanly. Expiration is a separate fact (verdict.is_expired and expiry_date), so the script compares the date with the return day itself.
Step 3: Run It on Three States
A rental from 29 November to 3 December 2026:
$ python us_license_check.py 2026-12-03 california.jpg massachusetts.jpg dc.png
california.jpg ok class A, valid to 2030-11-26
massachusetts.jpg deny expired 2021-01-12
dc.png deny expired 2018-02-21The Massachusetts and D.C. samples were printed years ago and have expired, which is exactly what a counter needs to catch. The same California license on a long rental that ends after its expiration date is refused too:
$ python us_license_check.py 2030-12-01 california.jpg
california.jpg deny expires 2030-11-26, before the car is backFor Rental Platforms: Ask Before the Pickup
On a peer-to-peer or app-based rental, run the check when the driver uploads the license, days before the pickup. A deny for an expired license gives the driver time to send a renewed one; a review goes to your team while there is still time to answer. To confirm that the person holding the license is the person in the photo, pair it with a selfie check as in our selfie to ID verification guide.
Production Tips
- Ask for a flat photo without glare. Laminated cards reflect light. A second photo fixes most missing fields, and several photos of the same side can go in one call so the API votes field by field.
- Keep the rules in config. Minimum age, young renter threshold and accepted countries change by state and by vehicle. Pass them to
check_rentalinstead of editing the code. - Route foreign licenses. The script sends any license whose country is not USA to review. Plug in the EU rules for European visitors instead of refusing them.
- Store the decision, not the image. Keep the decision, the reasons and the expiration date, then delete the photo. The ID Document OCR API does not store the images you send.
Next Step
Get a key on the ID Document OCR API page, save us_license_check.py and run it on one of the sample licenses below. Then set min_age and young_until to your own policy. For age checks at a store or online, see our age verification tutorial, and for edited documents, the fake ID detection tutorial.
Sources
- AAMVA DL/ID Card Design Standard
- Sample licenses on Wikimedia Commons: California 2025, Massachusetts 2016, Washington D.C. 2018
Frequently Asked Questions
- Can an API read a US driver's license?
- Yes. The ID Document OCR API reads the front of a US driver's license and returns the license number, names, date of birth, issue and expiration dates, license class and sex as JSON, with document_type set to driving_license and country set to USA. In our test on 29 November 2026 it read the California, Massachusetts and Washington D.C. sample licenses in 1.4 to 1.7 seconds each.
- What do the numbers on a US driver's license mean?
- Licenses that follow the AAMVA card design standard number their fields: 1 family name, 2 given names, 3 date of birth, 4a issue date, 4b expiration date, 4d license number, 5 document discriminator, 9 class, 9a endorsements, 12 restrictions, 15 sex, 16 height. It is the same numbering as European licenses, which is why one parser can read both.
- Which license class can rent a car in the US?
- The regular car license is class C in California and class D in most other states, and commercial classes A and B also allow driving a car. The only class that never allows it is a motorcycle-only license, class M. The check_rental function in this tutorial denies M alone and accepts the other classes.
- How do I know the license number was read correctly?
- For states whose license number format is known, the API checks the number it read against that format and returns the result in verdict.national_checks.document_number_format. California numbers are one letter followed by seven digits, so a misread letter fails the check. Send the license to a person when the check is false.



