メインコンテンツへ移動

次に使うツールを探す

ドキュメント/SDK と開発者ツール/コード例:Node.js、Python、PHP、cURL
SDK と開発者ツール

コード例:Node.js、Python、PHP、cURL

ジョブを送信し、バックオフしながら完了を待ち、結果をダウンロードするまでの完全なサンプルを 4 つの言語で紹介します。そのままコピーして使えます。

要点

どの言語でも、同じ 3 つの呼び出しで動作します。Idempotency-Key を付けて POST /api/v1/jobs/ を送信し、バックオフしながら GET /api/v1/jobs/{jobId}/ をポーリングし、最後に output.downloadUrl をダウンロードします。以下のサンプルは、タイムアウトとエラー処理を備えた完全なプログラムです。実行する前に、環境変数 MAXSCANNER_API_KEY を設定してください。

Node.js(18 以降)

組み込みの fetch を使うため、パッケージは不要です。

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 とシェル

jq が必要です。アプリケーションのコードを書く前に、キーや形式を試すのに便利です。

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 と AI コーディングアシスタント

https://maxscanner.app/openapi/maxscanner-v1.yaml にある OpenAPI 3.1 の定義から、普段使っているジェネレーターで型付きのクライアントを生成できます。コーディングアシスタントは https://maxscanner.app/llms.txt と https://maxscanner.app/ai/api.md を読み込めます。どちらにも同じエンドポイントとルールがまとめられています。

よくある質問

公式の SDK はありますか?

まだありません。API は小規模なので、上記のサンプルか、OpenAPI ファイルから生成したクライアントで十分に対応できます。

関連ガイド