Basic Usage Examples
This page presents practical examples of using the ParseMyFile API for different common use cases.
Example 1: Processing a Simple Invoice
YAML Configuration
yaml
# simple-invoice.yaml
schemas:
data:
type: object
properties:
invoice_number:
type: string
description: invoice number
invoice_date:
type: string
description: invoice issue date (format YYYY-MM-DD)
client_name:
type: string
description: client name
total_amount:
type: double
description: total amount including taxcURL Request
bash
curl -X POST "https://api.parsemyfile.com/api/v1/generate" \
-H "X-API-KEY: your_api_key_here" \
-F "file=@invoice.pdf" \
-F "yaml_file=@simple-invoice.yaml"Expected Response
json
{
"status": "success",
"data": {
"invoice_number": "INV-2024-001",
"invoice_date": "15/01/2024",
"client_name": "ABC Company",
"total_amount": "1250.00"
},
"execution_time": "2.1543",
}Example 2: Form Data Extraction
YAML Configuration
yaml
# registration-form.yaml
schemas:
data:
type: object
properties:
form_title:
type: string
description: form title
last_name:
type: string
description: family name
first_name:
type: string
description: first name
email:
type: string
description: email address
phone:
type: string
description: phone number
street:
type: string
description: street number and name
postal_code:
type: string
description: postal code
city:
type: string
description: city
country:
type: string
description: country
preferences:
type: array<string>
description: communication preferencesJavaScript Code
javascript
async function processForm(formData, apiKey) {
try {
const response = await fetch('https://api.parsemyfile.com/api/v1/generate', {
method: 'POST',
headers: {
'X-API-KEY': apiKey
},
body: formData
});
if (!response.ok) {
throw new Error(`Error ${response.status}: ${response.statusText}`);
}
const result = await response.json();
if (result.status === 'success') {
console.log('Extracted data:', result.data.extracted_fields);
return result.data;
} else {
console.warn('Partial extraction:', result.warnings);
return result.data;
}
} catch (error) {
console.error('Error during processing:', error);
throw error;
}
}
// Usage
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('yaml_file', yamlFileInput.files[0]);
processForm(formData, 'your_api_key')
.then(data => {
console.log('Processing successful:', data);
})
.catch(error => {
console.error('Error:', error);
});Example 3: Image Processing with OCR
YAML Configuration
yaml
# image-ocr.yaml
schemas:
data:
type: object
properties:
document_title:
type: string
description: document title
main_content:
type: string
description: main document content
document_date:
type: string
description: document date (format YYYY-MM-DD)
signature:
type: string
description: detected signaturePython Code
python
import requests
import json
from pathlib import Path
def process_image_with_ocr(image_path, yaml_path, api_key):
"""
Process an image with OCR and return extracted data
"""
url = "https://api.parsemyfile.com/api/v1/generate"
headers = {"X-API-KEY": api_key}
try:
with open(image_path, 'rb') as image_file, open(yaml_path, 'rb') as yaml_file:
files = {
'file': ('image.jpg', image_file, 'image/jpeg'),
'yaml_file': ('config.yaml', yaml_file, 'text/yaml')
}
response = requests.post(url, headers=headers, files=files)
if response.status_code == 200:
result = response.json()
return result['data']['extracted_fields']
else:
print(f"Error {response.status_code}: {response.text}")
return None
except FileNotFoundError as e:
print(f"File not found: {e}")
return None
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
return None
# Usage
image_path = "document.jpg"
yaml_path = "image-ocr.yaml"
api_key = "your_api_key"
result = process_image_with_ocr(image_path, yaml_path, api_key)
if result:
print("Extracted data:")
for field, value in result.items():
print(f" {field}: {value}")Example 4: Contract Processing
YAML Configuration
yaml
# contract.yaml
schemas:
data:
type: object
properties:
contract_title:
type: string
description: contract title
contracting_parties:
type: string
description: contracting parties names
start_date:
type: string
description: contract start date (format YYYY-MM-DD)
end_date:
type: string
description: contract end date (format YYYY-MM-DD)
amount:
type: double
description: contract amount in euros
signatures:
type: array<string>
description: detected signature areasPHP Code
php
<?php
function processContract($filePath, $yamlPath, $apiKey) {
$url = 'https://api.parsemyfile.com/api/v1/generate';
$postData = [
'file' => new CURLFile($filePath, 'application/pdf', 'contract.pdf'),
'yaml_file' => new CURLFile($yamlPath, 'text/yaml', 'contract.yaml')
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-API-KEY: ' . $apiKey
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$result = json_decode($response, true);
return $result['data']['extracted_fields'];
} else {
throw new Exception("API Error: " . $response);
}
}
// Usage
try {
$extractedData = processContract('contract.pdf', 'contract.yaml', 'your_api_key');
echo "Extracted data:\n";
foreach ($extractedData as $field => $value) {
echo " $field: $value\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>Example 5: Batch Processing
Python Code for Batch Processing
python
import os
import requests
import json
from pathlib import Path
import time
def process_batch(documents_dir, yaml_path, api_key, delay=1):
"""
Process multiple documents in batch
"""
results = []
documents = list(Path(documents_dir).glob('*.pdf'))
print(f"Processing {len(documents)} documents...")
for i, doc_path in enumerate(documents, 1):
print(f"Processing {i}/{len(documents)}: {doc_path.name}")
try:
with open(doc_path, 'rb') as doc_file, open(yaml_path, 'rb') as yaml_file:
files = {
'file': (doc_path.name, doc_file, 'application/pdf'),
'yaml_file': ('config.yaml', yaml_file, 'text/yaml')
}
response = requests.post(
'https://api.parsemyfile.com/api/v1/generate',
headers={'X-API-KEY': api_key},
files=files
)
if response.status_code == 200:
result = response.json()
results.append({
'file': doc_path.name,
'status': 'success',
'data': result['data']
})
print(f" ✓ Success (confidence: {result['data']['metadata']['confidence_score']:.2f})")
else:
results.append({
'file': doc_path.name,
'status': 'error',
'error': response.text
})
print(f" ✗ Error {response.status_code}")
except Exception as e:
results.append({
'file': doc_path.name,
'status': 'error',
'error': str(e)
})
print(f" ✗ Exception: {e}")
# Delay between requests to respect limits
if i < len(documents):
time.sleep(delay)
return results
# Usage
results = process_batch('./documents', 'config.yaml', 'your_api_key')
# Save results
with open('results.json', 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print(f"\nProcessing completed. {len([r for r in results if r['status'] == 'success'])}/{len(results)} documents processed successfully.")Tips for Examples
1. Test with Simple Documents
Start with well-structured, high-quality documents before moving to more complex cases.
2. Adjust Configuration
Modify the confidence threshold and preprocessing options according to your needs:
yaml
processing:
confidence_threshold: 0.7 # More permissive
preprocessing:
- "enhance_contrast" # For dark images
- "deskew" # For tilted documents3. Handle Errors
Always implement robust error handling:
javascript
try {
const result = await processDocument(file, yaml, apiKey);
// Handle success
} catch (error) {
if (error.status === 401) {
// Authentication problem
} else if (error.status === 422) {
// Validation problem
} else {
// Other error
}
}4. Optimize Performance
- Use configurations adapted to your documents
- Implement caching for recurring configurations
- Process documents in batches when possible