A cross-platform Node.js package to read Belgian eID card public data using Python eidreader with automatic dependency checking. Perfect for Electron apps and Node.js applications.
- 🔍 Cross-platform support: macOS, Windows, and Linux
- 🐍 Python integration: Uses the reliable eidreader Python package
- 🔧 Automatic setup: Checks and installs missing dependencies
- 🛡️ Error handling: Comprehensive error handling with specific error codes
- ⚡ Configurable: Customizable retry logic and paths
- 📦 npm package: Easy to install and use in any Node.js project
- 🖥️ Electron ready: Works seamlessly in Electron applications
npm install @kodeglot/node-python-eid-reader
Before using this package, you need to install:
- Python 3.x - Download from python.org
- Belgian eID middleware - Download from Belgian government
- eidreader Python package - Will be installed automatically
import { readEidData } from '@kodeglot/node-python-eid-reader';
try {
const eidData = await readEidData();
console.log('Card holder:', eidData.firstName, eidData.name);
console.log('Card number:', eidData.cardNumber);
} catch (error) {
console.error('Failed to read eID:', error.message);
}
import { EidReader } from '@kodeglot/node-python-eid-reader';
const reader = new EidReader({
verbose: true, // Enable detailed logging
maxRetries: 5, // Custom retry attempts
retryDelay: 500, // Custom retry delay (ms)
pythonPath: '/usr/bin/python3', // Custom Python path
pkcs11LibPath: '/custom/path/libbeidpkcs11.dylib' // Custom PKCS#11 library
});
try {
const eidData = await reader.readEidData();
console.log('Success:', eidData);
} catch (error) {
console.error('Error:', error.message, error.code);
}
import { checkRequirements, installRequirements } from '@kodeglot/node-python-eid-reader';
// Check if all requirements are met
const result = await checkRequirements();
// NOTE: In Electron/Node.js integration, check result.data.passed, not result.passed
if (result.data && result.data.passed) {
console.log('All requirements are met!');
} else {
console.log('Missing requirements:', result.data?.results);
// Attempt to install missing requirements
const installResult = await installRequirements();
if (installResult.success) {
if (installResult.data && installResult.data.installed) {
console.log('Requirements installed successfully!');
} else {
console.log('All requirements are already available!');
}
} else {
console.log('Failed to install requirements:', installResult.info);
if (installResult.error) {
console.log('Error:', installResult.error.message);
}
}
}
Convenience function to read eID data with default options.
Parameters:
options
(optional):EidReaderOptions
- Configuration options
Returns: Promise<EidData>
- The eID card data
Main class for reading eID data with full control over options.
new EidReader(options?: EidReaderOptions)
Options:
verbose?: boolean
- Enable verbose logging (default: false)maxRetries?: number
- Maximum retry attempts (default: 3)retryDelay?: number
- Delay between retries in ms (default: 300)pythonPath?: string
- Custom Python interpreter pathpkcs11LibPath?: string
- Custom PKCS#11 library path
readEidData(): Promise<EidData>
- Read eID data from the cardcheckRequirements(): Promise<{passed: boolean, results: any}>
- Check if requirements are metinstallRequirements(): Promise<ReaderResult<{installed: boolean}>>
- Install missing requirements
interface EidData {
cardNumber: string; // Card number (unique identifier)
chipNumber: string; // Chip number
validityDateBegin: string; // Card validity start date
validityDateEnd: string; // Card validity end date
municipality: string; // Issuing municipality
nationality: string; // Nationality
birthLocation: string; // Birth location
birthDate: string; // Birth date
name: string; // Last name
firstName: string; // First name(s)
sex: string; // Gender
documentType: string; // Document type
address: {
streetAndNumber: string; // Street and number
zip: string; // ZIP code
municipality: string; // Municipality
};
photo: string; // Base64 encoded photo
}
Custom error class with error codes for better error handling.
Error Codes:
REQUIREMENTS_NOT_MET
- Missing dependenciesPYTHON_NOT_FOUND
- Python interpreter not foundEIDREADER_FAILED
- Python eidreader script failedSPAWN_ERROR
- Failed to start Python processMAX_RETRIES_EXCEEDED
- Max retry attempts exceededINVALID_OUTPUT
- Invalid output from eidreaderPARSE_ERROR
- Failed to parse eID data
This package works seamlessly in Electron applications with built-in enable/disable functionality. Here's an example:
// main.js (Electron main process)
import { readEidData } from '@kodeglot/node-python-eid-reader';
let eidReaderEnabled = true; // Default to enabled
// Handle eID reading with enable/disable check
ipcMain.handle('read-eid', async (event) => {
if (!eidReaderEnabled) {
return { success: false, error: 'eID reader is disabled', code: 'DISABLED' };
}
try {
const eidData = await readEidData({ verbose: true });
return { success: true, data: eidData };
} catch (error) {
return { success: false, error: error.message, code: error.code };
}
});
// Handle enable/disable eID reader
ipcMain.handle('set-eid-enabled', async (event, enabled) => {
eidReaderEnabled = enabled;
return { success: true, enabled: eidReaderEnabled };
});
// Handle get eID reader status
ipcMain.handle('get-eid-status', async () => {
return { success: true, enabled: eidReaderEnabled };
});
// renderer.js (Electron renderer process)
const { ipcRenderer } = require('electron');
async function readEID() {
try {
const result = await ipcRenderer.invoke('read-eid');
if (result.success) {
console.log('eID data:', result.data);
} else if (result.code === 'DISABLED') {
console.log('eID reader is disabled');
} else {
console.error('Failed:', result.error);
}
} catch (error) {
console.error('IPC error:', error);
}
}
async function toggleEidReader(enabled) {
try {
const result = await ipcRenderer.invoke('set-eid-enabled', enabled);
if (result.success) {
console.log(`eID reader ${enabled ? 'enabled' : 'disabled'}`);
}
} catch (error) {
console.error('Toggle error:', error);
}
}
async function getEidStatus() {
try {
const result = await ipcRenderer.invoke('get-eid-status');
if (result.success) {
console.log(`eID reader is ${result.enabled ? 'enabled' : 'disabled'}`);
}
} catch (error) {
console.error('Status error:', error);
}
}
The Electron integration includes:
- Toggle Switch: Visual toggle to enable/disable the eID reader
- Status Indicator: Shows current enabled/disabled state
- Automatic Blocking: Reading operations are blocked when disabled
- State Persistence: Status is maintained during the app session
- Error Handling: Proper error codes for disabled state
- The requirements check result is an object with a
data
property. - To check if all requirements are met, use
result.data.passed
in your UI code. - Example:
if (result.success) {
if (result.data && result.data.passed) {
// All requirements are met
} else {
// Some requirements are missing
}
}
- This ensures your UI accurately reflects the backend requirements check result.
The installRequirements()
function now provides better feedback:
- All requirements available: Returns success with "All requirements are already available!"
- Requirements installed: Returns success with "Requirements installed successfully!"
- Installation failed: Returns failure with detailed info and error messages
Example handling in your UI:
const result = await installRequirements();
if (result.success) {
if (result.data && result.data.installed) {
console.log('Requirements installed successfully!');
} else {
console.log('All requirements are already available!');
}
} else {
console.log('Failed to install requirements:', result.info);
if (result.error) {
console.log('Error:', result.error.message);
}
}
The package includes TypeScript examples demonstrating different usage patterns:
npm run example:node
npm run example:electron
The examples are written in TypeScript and show:
- Basic usage with the convenience function
- Advanced usage with the
EidReader
class - Error handling and requirements management
- Electron integration with IPC handlers
-
"Python not found"
- Ensure Python 3.x is installed and in your PATH
- Use the
pythonPath
option to specify a custom Python path
-
"eidreader not found"
- The package will attempt to install it automatically
- Manual installation:
pip install eidreader
-
"Middleware not properly installed"
- Install Belgian eID middleware from eid.belgium.be
- On macOS, ensure the middleware is in
/Library/Belgium Identity Card/
- On Windows, ensure the middleware is in
C:\Program Files\Belgium Identity Card\
orC:\Program Files (x86)\Belgium Identity Card\
-
"Device error"
- Ensure the eID card is properly inserted
- Try removing and reinserting the card
- Check if the card reader is working
-
"Permission denied"
- On macOS/Linux, you may need to run with sudo for middleware access
- The package will prompt for password when needed
Enable verbose logging to see detailed information:
const reader = new EidReader({ verbose: true });
const eidData = await reader.readEidData();
You can set these environment variables to customize behavior:
PYKCS11LIB
- Path to PKCS#11 libraryDYLD_LIBRARY_PATH
- Library path (macOS)PYTHONPATH
- Python module search path
- Middleware typically installed in
/Library/Belgium Identity Card/
- May require sudo for first-time access
- DYLD_LIBRARY_PATH is automatically configured
- Middleware typically installed in
C:\Program Files\Belgium Identity Card\
- Ensure the middleware is properly registered
- May require running as administrator
- Middleware typically installed in
/usr/lib/
- May need to install additional packages:
sudo apt-get install libbeidpkcs11-0
# Clone the repository
git clone https://github.com/Kodeglot/Node-Python-EID-Reader.git
cd Node-Python-EID-Reader
# Install dependencies
npm install
# Build the package
npm run build
# Run tests
npm start
# Check requirements
npm run check
# Run examples
npm run example:node
npm run example:electron
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues:
- Check the troubleshooting section
- Search existing issues
- Create a new issue with detailed information about your problem
- Initial release
- Cross-platform support
- Automatic dependency checking
- Electron integration
- Comprehensive error handling