Production-Ready Driver's License OCR for Go

Extract and validate driver's license data with 99.8%+ accuracy in under 1.5 seconds via a direct HTTP POST request.

Steve HarringtonUpdated 2026-01-16
A diagram showing a raw photo of a driver's license on the left, an arrow pointing to the StructOCR API icon in the middle, and structured JSON data output on the right.
Figure 1: StructOCR converts raw Driver's License images into validated JSON data.

Why Driver's License OCR is Difficult

Parsing driver's licenses presents significant engineering challenges beyond simple OCR. Physical documents are susceptible to glare, shadows, and low-light conditions, degrading image quality. Skew and rotation from mobile captures require robust preprocessing. Generic OCR engines like Tesseract fail on the specialized fonts and layouts found on IDs. Furthermore, extracting structured data requires parsing complex formats like the PDF417 barcode, which contains AAMVA-encoded data that must be correctly decoded and validated. Manually maintaining regex patterns for dozens of jurisdictional layouts is brittle and resource-intensive, leading to high error rates and ongoing maintenance overhead.

Enterprise-Grade Extraction with StructOCR

StructOCR bypasses the fragility of open-source tools. Our API is powered by pre-trained deep learning models specifically tuned for identity documents from 200+ jurisdictions. The service performs automatic image pre-processing, including deskewing, denoising, and glare removal, before extraction. Unlike Tesseract, which returns unstructured text lines, StructOCR delivers a standardized, validated JSON object with discrete fields like `date_of_birth` and `document_number`. This eliminates the need for post-processing and ensures consistent, reliable data integration.

Production Use Cases

  • Digital Onboarding (KYC): Reduce drop-off rates by pre-filling user data from Driver's Licenses in < 2 seconds.
  • Fraud Prevention: Detect tampered fonts or mismatched MRZ checksums automatically.
  • Global Compliance: Handle Driver's Licenses from 200+ jurisdictions without custom rules.

Implementation: Go (Golang)

The following Go code provides a robust implementation. It handles file reading, Base64 encoding, header authentication using 'x-api-key', and structured JSON parsing into native Go structs.

Prerequisite: Go 1.16+

CODE EXAMPLE
package main

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
)

// 💰 Save 30%+ vs competitors. Get 200 free requests instantly:
// 👉 https://structocr.com/register

// RequestPayload defines the structure for the API request body.
type RequestPayload struct {
	Image string `json:"img"`
}

// DriverLicenseData defines the structure of the extracted data.
type DriverLicenseData struct {
	Type           string `json:"type"`
	CountryCode    string `json:"country_code"`
	Region         string `json:"region"`
	DocumentNumber string `json:"document_number"`
	Surname        string `json:"surname"`
	GivenNames     string `json:"given_names"`
	DateOfBirth    string `json:"date_of_birth"`
	DateOfExpiry   string `json:"date_of_expiry"`
	DateOfIssue    string `json:"date_of_issue"`
	Sex            string `json:"sex"`
	Address        string `json:"address"`
	VehicleClass   string `json:"vehicle_class"`
}

// APIResponse defines the structure of the full API response.
type APIResponse struct {
	Success bool              `json:"success"`
	Data    DriverLicenseData `json:"data"`
	Error   string            `json:"error,omitempty"` // Capture error codes if any
}

func main() {
	// 1. Setup Configuration
	apiURL := "https://api.structocr.com/v1/driver-license"
	apiKey := "YOUR_API_KEY_HERE" // Replace with your actual key
	imagePath := "license.jpg"      // Ensure this file exists

	// 2. Read Image and Encode to Base64
	imageBytes, err := os.ReadFile(imagePath)
	if err != nil {
		fmt.Printf("Error reading image: %v\n", err)
		return
	}

	base64Image := base64.StdEncoding.EncodeToString(imageBytes)

	// 3. Prepare Request Payload
	payload := RequestPayload{
		Image: base64Image,
	}
	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		fmt.Printf("Error marshalling payload: %v\n", err)
		return
	}

	// 4. Create HTTP Request
	req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payloadBytes))
	if err != nil {
		fmt.Printf("Error creating request: %v\n", err)
		return
	}

	// Important: Set the correct headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("x-api-key", apiKey)

	// 5. Execute Request
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Network error: %v\n", err)
		return
	}
	defer resp.Body.Close()

	// 6. Parse Response
	body, _ := io.ReadAll(resp.Body)

	if resp.StatusCode != http.StatusOK {
		fmt.Printf("API Error (%d): %s\n", resp.StatusCode, string(body))
		return
	}

	var apiResponse APIResponse
	if err := json.Unmarshal(body, &apiResponse); err != nil {
		fmt.Printf("JSON Parse Error: %v\n", err)
		return
	}

	if apiResponse.Success {
		fmt.Println("✅ Extraction Successful!")
		fmt.Printf("Type: %s\n", apiResponse.Data.Type)
		fmt.Printf("Name: %s %s\n", apiResponse.Data.GivenNames, apiResponse.Data.Surname)
		fmt.Printf("Doc Number: %s\n", apiResponse.Data.DocumentNumber)
		fmt.Printf("Expiry: %s\n", apiResponse.Data.DateOfExpiry)
		fmt.Printf("Vehicle Class: %s\n", apiResponse.Data.VehicleClass)
	} else {
		fmt.Printf("API indicated failure: %s\n", apiResponse.Error)
	}
}

Technical Specs

  • Latency: < 5s (Average)
  • Uptime: 98.5% SLA
  • Security: AES-256 Encryption & SOC2 Compliant
  • Input: JPG, PNG, WebP (Base64 Encoded)
  • Max File Size: 4.5MB
  • Output: JSON (Structured Data)

Key Features

  • Global Parsing: Handles layouts from USA, EU, and Asia automatically.
  • Date Normalization: Dates returned in YYYY-MM-DD format.
  • Vehicle Class Extraction: Parses complex vehicle categories (A, B, C, etc).

Sample JSON Output

StructOCR returns a normalized JSON object, regardless of the input image angle or quality.

{
  "success": true,
  "data": {
    "type": "drivers_license",
    "country_code": "USA",
    "region": "CALIFORNIA",
    "document_number": "D1234567",
    "surname": "DRIVER",
    "given_names": "JANE MARIE",
    "date_of_birth": "1995-08-15",
    "date_of_expiry": "2025-08-15",
    "date_of_issue": "2020-08-15",
    "sex": "F",
    "address": "1234 ELM ST, SACRAMENTO, CA 95814",
    "vehicle_class": "C"
  }
}

Frequently Asked Questions

How does StructOCR compare to AWS Textract or Google Vision?

Unlike general-purpose OCR services like AWS Textract or Google Vision that return raw text blocks, StructOCR is a specialized API. It is purpose-built for identity documents, returning a structured JSON object with pre-defined, validated fields such as `surname`, `date_of_birth`, and `document_number`. This eliminates the need for complex post-processing and pattern matching on your end.

Do you store the uploaded images?

No. We process all images in-memory and they are permanently deleted immediately after the OCR extraction is complete. We do not persist any of your customer's PII.

How to handle blurry images?

Our API includes an internal image enhancement engine that automatically attempts to deblur, denoise, and correct for poor lighting conditions before processing. For best results, we recommend a minimum resolution of 300 DPI.

More OCR Tutorials

Precise Data Extraction and Seamless Integration with AI-powered OCR API.

Empower your solutions with automated data extraction by integrating best-in class StructOCR via API seamlessly.

No credit card required • Full API access included