Exemples de code : Node.js, Python, PHP et cURL
Des exemples complets, prêts à copier-coller, qui soumettent une tâche, l’attendent avec un délai croissant et téléchargent le résultat, dans quatre langages.
Réponse courte
Les trois mêmes appels fonctionnent dans tous les langages : POST /api/v1/jobs/ avec une Idempotency-Key, interrogation de GET /api/v1/jobs/{jobId}/ avec un délai croissant, puis téléchargement de output.downloadUrl. Les exemples ci-dessous sont des programmes complets, avec délais d’expiration et gestion des erreurs ; définissez MAXSCANNER_API_KEY dans l’environnement avant de les exécuter.
Node.js (18+)
Utilise le fetch intégré : aucun paquet n’est nécessaire.
const KEY = process.env.MAXSCANNER_API_KEY;
const BASE = 'https://maxscanner.app/api/v1';
const headers = { Authorization: `Bearer ${KEY}`, 'Content-Type': 'application/json' };
const create = await fetch(`${BASE}/jobs/`, {
method: 'POST',
headers: { ...headers, 'Idempotency-Key': 'report-2026-11-pdf' },
body: JSON.stringify({ url: 'https://example.com/report.docx', filename: 'report.docx', format: 'pdf' }),
});
if (!create.ok) throw new Error(`create failed: ${create.status} ${await create.text()}`);
const { jobId } = await create.json();
let delay = 2000, job;
for (let i = 0; i < 60; i++) {
job = await (await fetch(`${BASE}/jobs/${jobId}/`, { headers })).json();
if (job.status === 'completed' || job.status === 'failed') break;
await new Promise((r) => setTimeout(r, delay));
delay = Math.min(delay * 1.5, 10000);
}
if (job.status !== 'completed') throw new Error(job.error || 'job did not finish');
const file = await fetch(job.output.downloadUrl);
await (await import('node:fs/promises')).writeFile('report.pdf', Buffer.from(await file.arrayBuffer()));Python
import os, time, requests
KEY = os.environ["MAXSCANNER_API_KEY"]
BASE = "https://maxscanner.app/api/v1"
S = requests.Session()
S.headers.update({"Authorization": f"Bearer {KEY}"})
r = S.post(f"{BASE}/jobs/", json={"url": "https://example.com/report.docx", "filename": "report.docx", "format": "pdf"},
headers={"Idempotency-Key": "report-2026-11-pdf"}, timeout=30)
r.raise_for_status()
job_id = r.json()["jobId"]
delay, job = 2.0, {}
for _ in range(60):
job = S.get(f"{BASE}/jobs/{job_id}/", timeout=30).json()
if job["status"] in ("completed", "failed"):
break
time.sleep(delay)
delay = min(delay * 1.5, 10)
if job.get("status") != "completed":
raise RuntimeError(job.get("error") or "job did not finish")
with open("report.pdf", "wb") as fh:
fh.write(requests.get(job["output"]["downloadUrl"], timeout=60).content)PHP
<?php
$key = getenv('MAXSCANNER_API_KEY');
$base = 'https://maxscanner.app/api/v1';
function api(string $method, string $url, string $key, ?array $body = null, array $extra = []): array {
$ch = curl_init($url);
$headers = array_merge(["Authorization: Bearer $key", 'Content-Type: application/json'], $extra);
curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => 30]);
if ($body !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
return $data ?? [];
}
$job = api('POST', "$base/jobs/", $key, ['url' => 'https://example.com/report.docx', 'filename' => 'report.docx', 'format' => 'pdf'], ['Idempotency-Key: report-2026-11-pdf']);
$delay = 2;
for ($i = 0; $i < 60; $i++) {
$status = api('GET', "$base/jobs/{$job['jobId']}/", $key);
if (in_array($status['status'], ['completed', 'failed'], true)) break;
sleep($delay);
$delay = min($delay * 2, 10);
}
if ($status['status'] === 'completed') file_put_contents('report.pdf', file_get_contents($status['output']['downloadUrl']));cURL et le shell
Nécessite jq. Pratique pour tester une clé ou un format avant d’écrire le code de l’application.
JOB=$(curl -s -X POST 'https://maxscanner.app/api/v1/jobs/' \
-H "Authorization: Bearer $MAXSCANNER_API_KEY" -H 'Content-Type: application/json' \
-H 'Idempotency-Key: report-2026-11-pdf' \
-d '{ "url": "https://example.com/report.docx", "filename": "report.docx", "format": "pdf" }' | jq -r .jobId)
until STATUS=$(curl -s "https://maxscanner.app/api/v1/jobs/$JOB/" -H "Authorization: Bearer $MAXSCANNER_API_KEY"); \
echo "$STATUS" | jq -e '.status == "completed" or .status == "failed"' > /dev/null; do sleep 5; done
curl -s -o report.pdf "$(echo "$STATUS" | jq -r .output.downloadUrl)"OpenAPI et assistants de code IA
La description OpenAPI 3.1, disponible à l’adresse https://maxscanner.app/openapi/maxscanner-v1.yaml, permet de générer un client typé avec votre générateur habituel. Les assistants de code peuvent lire https://maxscanner.app/llms.txt et https://maxscanner.app/ai/api.md, qui résument les mêmes endpoints et les mêmes règles.
Questions fréquentes
Existe-t-il un SDK officiel ?
Pas encore. L’API est suffisamment compacte pour que les exemples ci-dessus, ou un client généré à partir du fichier OpenAPI, la couvrent entièrement.