National ID OCR API for Go: Raw HTTP for Maximum Control

Achieve 99.7% field-level accuracy and sub-1500ms via a Hybrid Vision AI & MRZ Validation engine for real-time KYC.

A diagram showing a raw image of a National ID card being processed by the StructOCR API and outputting a structured JSON object with key-value pairs.
Figure 1: StructOCR converts raw National ID images into validated JSON data.

Why National ID OCR is Difficult

Open-source OCR engines like Tesseract fail on real-world National ID documents. The core challenge is variability: inconsistent lighting creates glare and shadows, while mobile captures introduce skew and rotation. These image defects cripple template-based approaches. Furthermore, parsing the output requires maintaining brittle RegEx patterns for dozens of document layouts, a significant engineering overhead. Manually implementing validation for checksums, such as those in a Machine-Readable Zone (MRZ), is error-prone and complex. These factors combine to create a high-maintenance, low-accuracy system unsuitable for production KYC workflows.

Enterprise-Grade Extraction with StructOCR

StructOCR bypasses the limitations of generic OCR. Our id parsing api is powered by pre-trained Deep Learning models specifically architected for identity documents. Upon receiving an image, our system performs automatic pre-processing, including deskewing, denoising, and glare removal. This normalized image is then processed by our specialized models which locate and extract specific data fields—not just raw text lines. Unlike Tesseract's raw text dump, we deliver a standardized JSON output with validated fields (e.g., `date_of_birth`), eliminating the need for complex post-processing logic on your end, and ensuring adherence to standards like the 14 digits structure where applicable.

Production Use Cases

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

Live Demo: ID card scanner

No registration required. Upload a file to test the extraction.

1
Upload
2
Results

Drop files here or click to browse

JPG · PNG · WebP  ·  up to 500 files · max 4.5 MB each

No files selected

Ready to use this in production? Get 20 free API calls — no credit card needed.

Get 20 Free API Calls →

Implementation: Go (Golang)

The following Go code provides a robust implementation. It handles file reading, Base64 encoding, and parses the region-specific JSON fields (like CNP, CPF, or NIN) 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 20 free credits instantly:
// 👉 https://structocr.com/register

// --- Struct Definitions ---
type ApiResponse struct {
	Success bool           `json:"success"`
	Data    NationalIdData `json:"data"`
	Error   string         `json:"error,omitempty"`
}

type NationalIdData struct {
	CountryCode    string           `json:"country_code"`
	DocumentNumber string           `json:"document_number"`
	PersonalNumber string           `json:"personal_number"`
	CardSeries     string           `json:"card_series"`
	Surname        string           `json:"surname"`
	GivenNames     string           `json:"given_names"`
	Sex            string           `json:"sex"`
	DateOfBirth    string           `json:"date_of_birth"`
	Address        string           `json:"address"`
	Additional     AdditionalFields `json:"additional_fields"`
}

type AdditionalFields struct {
	MrzLine1 string `json:"mrz_line_1"`
	MrzLine2 string `json:"mrz_line_2"`
	MrzLine3 string `json:"mrz_line_3"`
}

func main() {
	apiURL := "https://api.structocr.com/v1/national-id"
	apiKey := "YOUR_API_KEY_HERE"
	imagePath := "id_card.jpg"

	imageBytes, err := os.ReadFile(imagePath)
	if err != nil { return }

	base64Image := base64.StdEncoding.EncodeToString(imageBytes)
	payload, _ := json.Marshal(map[string]string{"img": base64Image})

	req, _ := http.NewRequest("POST", apiURL, bytes.NewBuffer(payload))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("x-api-key", apiKey)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil { return }
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)

	var result ApiResponse
	if err := json.Unmarshal(body, &result); err != nil { return }

	if result.Success {
		data := result.Data
		fmt.Println("✅ National ID Extracted!")
		fmt.Printf("Doc Number: %s\n", data.DocumentNumber)
		fmt.Printf("Name:       %s %s\n", data.GivenNames, data.Surname)
		if data.Additional.MrzLine1 != "" {
			fmt.Printf("MRZ Line 1: %s\n", data.Additional.MrzLine1)
			fmt.Printf("MRZ Line 2: %s\n", data.Additional.MrzLine2)
		}
	}
}

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

  • Hybrid VIZ + MRZ AI: Cross-validates unstructured visual data against cryptographic MRZ checksums (TD1/TD2) for zero hallucination.
  • Specialized Numbers: Extracts region-specific IDs like CNP (Romania), CPF (Brazil), and NIN (Nigeria).
  • Multi-line Addresses: Intelligently reconstructs full addresses from fragmented lines on ID cards.

Sample JSON Output

StructOCR returns a normalized JSON object containing both Visual Zone (VIZ) extraction and raw Machine-Readable Zone (MRZ) lines.

{
  "success": true,
  "data": {
    "type": "national_id",
    "country_code": "ROU",
    "nationality": "ROMANA",
    "document_number": "123456",
    "card_series": "KS",
    "personal_number": "1920319123456",
    "surname": "POPESCU",
    "given_names": "ANDREI",
    "sex": "M",
    "date_of_birth": "1992-03-19",
    "place_of_birth": "Jud. CS Mun. Reșița",
    "address": "Jud. CS Orș. Bocșa Str. Nucilor Nr. 15",
    "date_of_issue": "2020-05-10",
    "date_of_expiry": "2030-05-10",
    "issuing_authority": "SPCLEP Bocșa",
    "additional_fields": {
      "phone_number": null,
      "tramite_number": null,
      "ejemplar": null,
      "mrz_line_1": "IDROU123456<0<<<<<<<<<<<<<<<<",
      "mrz_line_2": "9203195M3005108ROU19203191234562",
      "mrz_line_3": null
    }
  }
}

Frequently Asked Questions

Do you support Machine Readable Zones (MRZ) on ID cards?

Yes! Our engine natively supports ICAO 9303 standard MRZ formats (TD1/TD2) found on many global ID cards. Our Hybrid architecture extracts both the raw MRZ lines and cross-validates them against the Visual Zone (VIZ) for maximum accuracy.

How does StructOCR compare to AWS Textract or Google Vision?

General-purpose OCR services return unstructured lines of text and their coordinates. StructOCR is a specialized service that understands the layout and fields of identity documents. We return a structured JSON object with labeled fields like 'surname' and 'date_of_birth', eliminating the need for you to build and maintain parsing logic.

Do you store the uploaded images?

No. Images are processed in-memory and are purged immediately after the API request is completed. We do not persist Personally Identifiable Information (PII).

How to handle blurry images?

Our API includes an automated image enhancement pipeline that performs deskewing, denoising, and contrast correction before the core OCR engine runs. This significantly improves accuracy on sub-optimal or blurry images common in mobile uploads.

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