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

Achieve 99.7% field-level accuracy and sub-1500ms latency for real-time KYC and identity verification.

Steve HarringtonUpdated 2026-01-16
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 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.

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.

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 200 free requests 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"` // e.g., CNP, CPF, NIN
	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"`
	DateOfExpiry   string `json:"date_of_expiry"`
}

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

	// 1. Read Image
	imageBytes, err := os.ReadFile(imagePath)
	if err != nil {
		fmt.Printf("Error reading file: %v\n", err)
		return
	}

	// 2. Encode to Base64
	base64Image := base64.StdEncoding.EncodeToString(imageBytes)

	// 3. Prepare Payload
	payload, _ := json.Marshal(map[string]string{"img": base64Image})

	// 4. Create Request
	req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payload))
	if err != nil {
		fmt.Printf("Request creation failed: %v\n", err)
		return
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("x-api-key", apiKey) // Required Header

	// 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 result ApiResponse
	if err := json.Unmarshal(body, &result); err != nil {
		fmt.Printf("JSON Parse Error: %v\n", err)
		return
	}

	if result.Success {
		data := result.Data
		fmt.Println("✅ National ID Extracted!")
		fmt.Printf("Country:    %s\n", data.CountryCode)
		fmt.Printf("Name:       %s %s\n", data.GivenNames, data.Surname)
		fmt.Printf("Doc Number: %s (Series: %s)\n", data.DocumentNumber, data.CardSeries)
		fmt.Printf("Personal #: %s\n", data.PersonalNumber)
		fmt.Printf("Address:    %s\n", data.Address)
	} else {
		fmt.Printf("Extraction failed: %s\n", result.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

  • 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.
  • Date Normalization: Returns all dates (Birth, Issue, Expiry) in a standardized YYYY-MM-DD format.

Sample JSON Output

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

{
  "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"
  }
}

Frequently Asked Questions

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