Codebeispiele
Was jedes Beispiel tut
Jedes Beispiel lädt eine Datei zu POST /v1/extract hoch und folgt dann denselben Schritten:
- Bei
429 rate_limited,502,503oder504: warten und erneut versuchen, höchstens dreimal. Wenn die Antwort einenRetry-After-Header hat, warten Sie die volle Zahl an Sekunden. Diese Antworten werden nicht berechnet. - Bei
429 quota_exhaustedund bei jedem anderen Fehler: abbrechen und denerror.codemelden. - Bei
200:successlesen. Beitrueverwenden Sie diefieldsjedes Dokuments, dessen eigenessuccessden Werttruehat. Beifalselesen Siefailure_reason. Der Grundno ocr engine availableist eine vorübergehende Störung des Dienstes, Sie können dieselbe Datei also später erneut senden. Jeder andere Grund braucht ein besseres Bild, keinen neuen Versuch.
Ersetzen Sie YOUR_API_KEY durch Ihren tatsächlichen API-Schlüssel. Behalten Sie den Schlüssel auf Ihrem Server, niemals in Browser-Code.
Jeder Erfolg wird berechnet, auch für eine Datei, die Sie schon einmal gesendet haben. Speichern Sie das Ergebnis auf Ihrer Seite.
cURL
curl -s -X POST "https://api.idparse.com/v1/extract" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "file=@passport.jpg"Auch den Status und die Antwort-Header anzeigen:
curl -s -i -X POST "https://api.idparse.com/v1/extract" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "file=@passport.jpg"Nutzung prüfen (kostenlos):
curl -s "https://api.idparse.com/v1/quota" \
-H "Authorization: Bearer YOUR_API_KEY"PHP
Dieses Beispiel verwendet Guzzle (composer require guzzlehttp/guzzle).
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
$apiKey = getenv('IDPARSE_API_KEY');
$path = 'passport.jpg';
$client = new Client([
'base_uri' => 'https://api.idparse.com/v1/',
'http_errors' => false, // read error answers instead of throwing
'timeout' => 100,
]);
for ($attempt = 1; $attempt <= 3; $attempt++) {
$response = $client->post('extract', [
'headers' => ['Authorization' => 'Bearer '.$apiKey],
'multipart' => [
['name' => 'file', 'contents' => Utils::tryFopen($path, 'r')],
],
]);
$status = $response->getStatusCode();
$body = json_decode((string) $response->getBody(), true);
$code = $body['error']['code'] ?? null;
$canRetry = in_array($status, [502, 503, 504], true)
|| ($status === 429 && $code === 'rate_limited');
if ($canRetry && $attempt < 3) {
// Not charged. Wait the full Retry-After value.
$wait = (int) ($response->getHeaderLine('Retry-After') ?: 5);
sleep($wait);
continue;
}
break;
}
if ($status !== 200) {
fwrite(STDERR, "Request failed: HTTP $status, code $code".PHP_EOL);
fwrite(STDERR, 'Request ID: '.$response->getHeaderLine('X-Request-ID').PHP_EOL);
exit(1);
}
if ($body['success'] === true) {
// Charged as one scan, also when the file holds several documents.
foreach ($body['documents'] as $document) {
if ($document['success'] !== true) {
continue; // not verified: see "Partial Results"
}
echo 'Document number: '.$document['fields']['document_number'].PHP_EOL;
echo 'Surname: '.($document['fields']['surname'] ?? 'not readable').PHP_EOL;
}
} elseif ($body['failure_reason'] === 'no ocr engine available') {
// Free of charge. A temporary fault of the service: send the same file again later.
echo 'Service not ready, try again later.'.PHP_EOL;
} else {
// Free of charge. Ask for a better image, do not send the same file again.
echo 'Not readable: '.$body['failure_reason'].PHP_EOL;
}
echo 'Scans remaining: '.$response->getHeaderLine('X-IDParse-Scans-Remaining').PHP_EOL;Python
Dieses Beispiel verwendet requests (pip install requests).
import os
import sys
import time
import requests
API_KEY = os.environ["IDPARSE_API_KEY"]
URL = "https://api.idparse.com/v1/extract"
PATH = "passport.jpg"
for attempt in range(1, 4):
with open(PATH, "rb") as handle:
response = requests.post(
URL,
headers={"Authorization": f"Bearer {API_KEY}"},
files={"file": handle},
timeout=100,
)
body = response.json()
code = body.get("error", {}).get("code")
can_retry = response.status_code in (502, 503, 504) or (
response.status_code == 429 and code == "rate_limited"
)
if can_retry and attempt < 3:
# Not charged. Wait the full Retry-After value.
wait = int(response.headers.get("Retry-After", 5))
time.sleep(wait)
continue
break
if response.status_code != 200:
print(f"Request failed: HTTP {response.status_code}, code {code}", file=sys.stderr)
print("Request ID:", response.headers.get("X-Request-ID"), file=sys.stderr)
sys.exit(1)
if body["success"]:
# Charged as one scan, also when the file holds several documents.
for document in body["documents"]:
if not document["success"]:
continue # not verified: see "Partial Results"
print("Document number:", document["fields"]["document_number"])
print("Surname:", document["fields"]["surname"] or "not readable")
elif body["failure_reason"] == "no ocr engine available":
# Free of charge. A temporary fault of the service: send the same file again later.
print("Service not ready, try again later.")
else:
# Free of charge. Ask for a better image, do not send the same file again.
print("Not readable:", body["failure_reason"])
print("Scans remaining:", response.headers.get("X-IDParse-Scans-Remaining"))Node.js
Dieses Beispiel braucht Node.js 18 oder neuer. Es verwendet die eingebauten Funktionen fetch, FormData und Blob und hat deshalb keine Abhängigkeiten. Speichern Sie es als ES-Modul, zum Beispiel als extract.mjs.
import { readFile } from 'node:fs/promises';
const API_KEY = process.env.IDPARSE_API_KEY;
const ENDPOINT = 'https://api.idparse.com/v1/extract';
const PATH = 'passport.jpg';
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000));
const bytes = await readFile(PATH);
let response;
let body;
let code;
for (let attempt = 1; attempt <= 3; attempt++) {
const form = new FormData();
form.append('file', new Blob([bytes]), 'document.jpg');
response = await fetch(ENDPOINT, {
method: 'POST',
headers: { Authorization: `Bearer ${API_KEY}` },
body: form,
});
body = await response.json();
code = body.error?.code;
const canRetry = [502, 503, 504].includes(response.status)
|| (response.status === 429 && code === 'rate_limited');
if (canRetry && attempt < 3) {
// Not charged. Wait the full Retry-After value.
const wait = Number(response.headers.get('Retry-After') ?? 5);
await sleep(wait);
continue;
}
break;
}
if (response.status !== 200) {
console.error(`Request failed: HTTP ${response.status}, code ${code}`);
console.error('Request ID:', response.headers.get('X-Request-ID'));
process.exit(1);
}
if (body.success) {
// Charged as one scan, also when the file holds several documents.
for (const document of body.documents) {
if (!document.success) {
continue; // not verified: see "Partial Results"
}
console.log('Document number:', document.fields.document_number);
console.log('Surname:', document.fields.surname ?? 'not readable');
}
} else if (body.failure_reason === 'no ocr engine available') {
// Free of charge. A temporary fault of the service: send the same file again later.
console.log('Service not ready, try again later.');
} else {
// Free of charge. Ask for a better image, do not send the same file again.
console.log('Not readable:', body.failure_reason);
}
console.log('Scans remaining:', response.headers.get('X-IDParse-Scans-Remaining'));Setzen Sie den Content-Type-Header in keinem dieser Beispiele selbst. Die HTTP-Bibliothek fügt die Multipart-Boundary (das Trennzeichen zwischen den Formularteilen) für Sie hinzu.