Morocco Passport OCR with the StructOCR Node.js SDK

Install the official Node.js SDK, extract validated Morocco passport fields, and add the same workflow to a TypeScript Express service.

A diagram showing a Moroccan passport image processed by the StructOCR Node.js API, returning structured JSON.
Figure 1: StructOCR converts raw Morocco Passport images into validated JSON data natively in your Node.js backend.

Why Moroccan Passport OCR is Difficult in Node.js

Building a reliable passport OCR pipeline from scratch is non-trivial for Moroccan documents. The Moroccan passport features a complex bilingual layout, utilizing both Right-to-Left (RTL) Arabic script and Left-to-Right (LTR) French text on the same data page. This bidirectional flow frequently causes errors in standard Node.js wrappers like Tesseract. Furthermore, the documents incorporate intricate security backgrounds and guilloche patterns that cause optical noise. Developing custom RegEx patterns in JavaScript to parse the Machine Readable Zone (MRZ) while correcting these multi-script alignment issues is highly brittle, often resulting in high manual review rates.

Enterprise-Grade Extraction with the StructOCR SDK

StructOCR simplifies document processing in your JavaScript ecosystem by replacing complex pipelines with a single async API call. Our service leverages pre-trained Deep Learning models optimized specifically for North African identity documents and the complexities of bidirectional Arabic/French typography. Our passport mrz ocr api automatically handles perspective correction, denoising, and glare removal. Instead of returning raw, unstructured text strings, the StructOCR Node.js SDK provides standardized JSON output with validated fields. This capability is crucial for applications managing international borders, as it eliminates the need for manual parsing, delivering production-ready data directly to your Express, NestJS, or Next.js applications. For the fastest Node.js integration, install the official StructOCR SDK and see the Node.js SDK documentation.

Production Use Cases

  • Digital Onboarding (e-KYC): Reduce drop-off rates by pre-filling user data from Moroccan Passports into your fintech or digital services apps in under 2 seconds.
  • Travel & Aviation Apps: Seamlessly integrate with Node.js backends for automated check-in systems and border management at hubs like Mohammed V International Airport (CMN) in Casablanca.
  • Financial Compliance: Ensure strict compliance with Bank Al-Maghrib and regional Anti-Money Laundering (AML) regulations by automatically and accurately verifying identity documents.

Live Demo: Passport 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
Need more testing? Create a free account to get 200 free credits (equals 100 Passport scans).

Morocco Passport OCR with the Node.js SDK

The official Node.js SDK handles file buffers, API communication, and structured passport results. Use the SDK directly or open the TypeScript + Express tab for a server endpoint. Keep your API key in STRUCTOCR_API_KEY.

Prerequisite: `npm install structocr`; set the server-side `STRUCTOCR_API_KEY` environment variable.

Prefer another stack? Open the Python SDK + FastAPI integration.

const StructOCR = require('structocr');

// Initialize the client with your API key
const apiKey = process.env.STRUCTOCR_API_KEY;
if (!apiKey) throw new Error('STRUCTOCR_API_KEY is not configured');
const client = new StructOCR(apiKey);

async function scanMoroccoPassport() {
  // Note: Supports JPG, PNG, WebP, PDF (Max 4.5MB)
  const imagePath = './morocco_passport_sample.jpg';

  try {
    console.log(`Processing Morocco passport: ${imagePath}...`);

    // The SDK handles file reading and the API call seamlessly
    const result = await client.scanPassport(imagePath);

    if (result.success && result.data) {
      const data = result.data;
      console.log('✅ Extraction Successful!');
      
      // Basic Identity (MRZ + VIZ)
      console.log(`Passport #: ${data.passport_number}`);
      console.log(`Name:       ${data.surname} ${data.given_names}`);
      console.log(`Nation:     ${data.nationality} (${data.country_code})`);
      
      // Visual Zone Specifics
      console.log(`Birth Place:${data.place_of_birth}`);
      console.log(`Authority:  ${data.issuing_authority}`);
      
      // Dates
      console.log(`DOB:        ${data.date_of_birth} (${data.sex})`);
      console.log(`Expiry:     ${data.date_of_expiry}`);

    } else {
      console.error('❌ Extraction Failed:', result.error || 'Unknown Error');
    }
  } catch (error) {
    console.error('An unexpected error occurred:', error.message);
  }
}

scanMoroccoPassport();

Technical Specs

  • Latency: < 4s (Average)
  • Uptime: 99.9% SLA
  • Security: AES-256 Encryption & SOC2 Compliant
  • Input: JPG, PNG, WebP, PDF (file path, Buffer, or Uint8Array; SDK converts to Base64 JSON)
  • Max File Size: 4.5MB
  • Output: JSON (Structured Data)

Key Features

  • Bi-directional Text Support: Accurately parses names and descriptors across LTR French and RTL Arabic scripts without throwing encoding or alignment errors in your Node environment.
  • Visual Extraction (VIZ): Reliably extracts data directly from the visual inspection zone, bypassing intricate national security backgrounds and watermarks.
  • Date Normalization: Returns all dates (Birth, Issue, Expiry) in a standardized YYYY-MM-DD format, ready for JavaScript `Date` objects.

Sample JSON Output

The Node.js SDK returns an object matching this normalized JSON structure.

{
  "success": true,
  "data": {
    "type": "passport",
    "country_code": "MAR",
    "nationality": "MAR",
    "passport_number": "AB123456",
    "surname": "EL AMRANI",
    "given_names": "YOUSSEF",
    "sex": "M",
    "date_of_birth": "1992-09-15",
    "place_of_birth": "CASABLANCA",
    "date_of_issue": "2023-04-10",
    "date_of_expiry": "2028-04-09",
    "issuing_authority": "DIRECTION GENERALE DE LA SURETE NATIONALE"
  }
}

Frequently Asked Questions

How does StructOCR compare to AWS Textract or Google Vision for Moroccan documents?

Generic OCR services often struggle with the bidirectional nature of Arabic/French documents, frequently misinterpreting the reading order. Furthermore, you remain responsible for writing JavaScript parsing logic and validating MRZ checksums. StructOCR is a specialized API trained specifically on these RTL documents, returning validated, labeled fields directly.

Do you store the uploaded images?

We do not store customer images. All data is processed in-memory (RAM) and is purged immediately after the API request is completed. We are a SOC2 compliant provider.

Can I pass a buffer instead of a file path in Node.js?

Yes. On Node.js server runtimes, the SDK accepts Buffer or Uint8Array, validates the decoded JPG, PNG, WebP, or PDF, and converts it to Base64 JSON before calling the REST API.

You May Also Like

Related tutorials, platform guides, and comparisons

Country Tutorial

Spain Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Spain passports.

Node.js · Passport
Country Tutorial

Vietnam Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Vietnam passports.

Node.js · Passport
Country Tutorial

Saudi Arabia Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Saudi Arabia passports.

Node.js · Passport
Country Tutorial

Japan Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Japan passports.

Node.js · Passport
Country Tutorial

Philippines Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Philippines passports.

Node.js · Passport
Country Tutorial

UK Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from UK passports.

Node.js · Passport
Country Tutorial

Portugal Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Portugal passports.

Node.js · Passport
Country Tutorial

Romania Passport OCR with Node.js SDK

Use the official StructOCR Node.js SDK with TypeScript and Express to extract structured MRZ and VIZ data from Romania passports.

Node.js · Passport
Country Tutorial

Poland Passport OCR with Python SDK

Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Poland passports with a server-side integration.

Python · Passport
Country Tutorial

Portugal Passport OCR with Python SDK

Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Portugal passports with a server-side integration.

Python · Passport

Technical Comparisons & Integrations

Explore platform integrations and competitive analysis

From tutorial to production in 5 minutes.

You've seen the code. Now get your API key, grab your 200 free credits, and see it work with your own images. No credit card required.

Instant Access Cancel Anytime 99.9% Uptime