Zum Hauptinhalt springen

Nächstes Tool finden

Dokumentation/SDKs und Entwicklerwerkzeuge/Codebeispiele: Node.js, Python, PHP und cURL
SDKs und Entwicklerwerkzeuge

Codebeispiele: Node.js, Python, PHP und cURL

Vollständige Beispiele zum Kopieren, die einen Job senden, mit Backoff auf ihn warten und das Ergebnis herunterladen – in vier Sprachen.

Kurze Antwort

Dieselben drei Aufrufe funktionieren in jeder Sprache: POST /api/v1/jobs/ mit einem Idempotency-Key, GET /api/v1/jobs/{jobId}/ mit Backoff abfragen, dann output.downloadUrl herunterladen. Die folgenden Beispiele sind vollständige Programme mit Timeouts und Fehlerbehandlung; setzen Sie MAXSCANNER_API_KEY in der Umgebung, bevor Sie sie ausführen.

Node.js (18+)

Verwendet das integrierte fetch, daher sind keine Pakete nötig.

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 und die Shell

Erfordert jq. Praktisch, um einen Schlüssel oder ein Format zu testen, bevor Sie Anwendungscode schreiben.

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 und KI-Codierassistenten

Mit der OpenAPI-3.1-Beschreibung unter https://maxscanner.app/openapi/maxscanner-v1.yaml können Sie mit Ihrem gewohnten Generator einen typisierten Client erzeugen. Codierassistenten können https://maxscanner.app/llms.txt und https://maxscanner.app/ai/api.md lesen, die dieselben Endpunkte und Regeln zusammenfassen.

Häufige Fragen

Gibt es ein offizielles SDK?

Noch nicht. Die API ist so überschaubar, dass die Beispiele oben oder ein aus der OpenAPI-Datei generierter Client sie vollständig abdecken.

Verwandte Anleitungen