PDF security blog

Generate a PDF Tamper-Detection Client from the OpenAPI Spec: Setup and Gotchas

HTPBE Team··10 min read
Generate a PDF Tamper-Detection Client from the OpenAPI Spec: Setup and Gotchas

This article is a snapshot – content was accurate as of December 2026 (code examples tested against the API as of November 2026). The product evolves actively; specific counts, examples, and detection rules may have changed since publication – see the changelog for the current state.

HTPBE? publishes an OpenAPI 3.1 document describing the three public endpoints. If your team already generates clients for every third-party API it calls, you can point the same tool at that document and get typed request and response models from it. The catch is that the generated code is only as good as the generator’s reading of the spec, and a few parts of this spec are easy to read wrong.

If you would rather own every line of HTTP, the Node.js integration guide writes the client by hand, with retries and response validation. Both routes end at the same two calls. This post is for teams whose convention is “generate from the spec”, and it covers what to check after the generator finishes.

If you work in Python, there is also an official client on PyPI (pip install htpbe) that wraps the same two calls. The rest of this post applies to every other language, and to teams that generate from the spec as a rule.

Why generate instead of hand-writing

A generated client gives you request and response types that come straight from the contract, so a field you misspell fails at compile time instead of in a log. It also gives you a mechanical way to pick up contract changes: fetch the spec again, regenerate, and read the diff. The trade-off is that you inherit the generator’s opinions about nullability, enums and error handling, and those opinions differ from tool to tool. Hand-written code does not have that problem, which is why neither approach is the “right” one.

Fetch the spec

The spec is published in two formats, built from the same source, so they cannot drift apart:

curl -O https://htpbe.tech/openapi.json
curl -O https://htpbe.tech/openapi.yaml

Both responses carry Access-Control-Allow-Origin: * and public cache headers, so browser-based tooling can fetch them without a proxy. If you prefer to read before you generate, the same document is rendered at /api/scalar, and the prose reference lives at /api.

Commit the downloaded file to your repository rather than fetching it during every build. Generation then becomes reproducible, and a contract change shows up as a reviewable diff.

This matters because info.version in this spec is the constant 1.0.0, so it cannot tell you whether anything changed. Diff the spec itself:

curl -s https://htpbe.tech/openapi.json -o openapi.new.json
diff <(jq -S . openapi.json) <(jq -S . openapi.new.json)

Generate with the tool you already use

Use whatever your codebase already standardises on. Common choices include openapi-generator, openapi-typescript, oapi-codegen and Kiota. These are examples of the category, not recommendations: HTPBE? has not tested or endorsed any of them, and none is required.

The part to check is OpenAPI 3.1 support. The spec declares openapi: 3.1.0, and 3.1 expresses nullable fields differently from 3.0. Tools and versions handle that differently, so do not assume support. Before you commit the output, open the generated model for a check result and confirm three things:

  1. creation_date is typed as an integer that can be null, not as a plain integer.
  2. modification_confidence accepts null as well as certain, high and none.
  3. The status field is an enum of intact, modified and inconclusive.

If any of those is wrong, the generator is not reading 3.1 type arrays correctly. Check its documentation for a 3.1 option before patching the output by hand.

You should end up with three operations, named from the spec’s operationId values:

operationIdMethod and pathSuccess
analyzePdfPOST /analyze201 with { id } and a Location header
getResultGET /result/{id}200 with the full check result
listChecksGET /checks200 with a paginated list

Tools that group operations by tag may put the first operation under Analysis and the other two under Results.

Auth

The spec declares one security scheme, bearerAuth, of type http with scheme bearer, applied to every operation. In practice that means your client sends:

Authorization: Bearer htpbe_live_...

with a live key, or htpbe_test_... with a test key. Look in your generator’s client configuration for a bearer-token setting, and confirm in a request log that the header goes out in this form.

Keep the key in an environment variable or your secrets store, and keep test and live keys in separate environments: a test key cannot read live checks, and a live key cannot read synthetic results.

Gotchas

The base URL is on a different host. The spec is served from htpbe.tech, but its servers entry is https://api.htpbe.tech/v1. A tool that derives the base URL from where it downloaded the spec, instead of from servers, will send requests to the wrong host. Set the base URL explicitly in the client configuration.

POST /analyze succeeds with 201, not 200. If your generator or your wrapper only treats 200 as success, every successful analysis looks like a failure. The body is { "id": "..." }; the Location header points at the result resource.

Idempotency-Key is an optional header parameter. It is a string of up to 255 characters on analyzePdf, so most generators surface it as an optional argument on that one operation. Reusing a key for the same account and the same url returns the original id without a second analysis or a second credit. Reusing it with a different url returns 422 with code idempotency_key_fingerprint_mismatch. A request still running with the same key returns 409 with a Retry-After header: retry with the same key, not a new one. How to design retries around this is covered in the batch processing post.

Error codes are not typed. Every error response shares one schema: required error and code, optional details, and request_id on 5xx responses. code is a plain string, not an enum, so the generated client will not give you a union of codes. Branch on code yourself, because the status alone is not enough: on /analyze, 422 covers an unparseable PDF, analysis_timeout and the idempotency mismatch, and 403 covers inactive_client, test_url_required and account_restricted. Which branches to retry and which to stop on is in the verdict-handling post.

Error responses differ per endpoint. 402 payment_required exists only on analyzePdf. getResult adds 404 and its only 403 is inactive_client. listChecks has neither 402 nor 404. If you write a shared error handler, do not assume every operation can return every status.

Enums grow. modification_markers is an array of public HTPBE_* IDs, and the spec types each item as an enum. IDs are only ever added, never renamed, but “added” is exactly what breaks a strict generated enum: if the API starts returning an ID that did not exist when your client was generated, a deserialiser that rejects unknown enum values will throw on a valid response. status_reason has the same shape, and its description says to treat an unrecognised value as inconclusive. Regenerate when the spec changes, and also make the parsing tolerant, because the API can update before you regenerate.

Check whether your generator has an open or non-strict enum option. If it does not, a thin layer over the raw values does the job:

type Verdict = 'intact' | 'modified' | 'inconclusive';

const KNOWN_REASONS = new Set([
  'consumer_software_origin',
  'online_editor_origin',
  'scanned_document',
  'unverifiable_metadata',
  'filled_form_origin',
  'fill_sign_origin',
  'analysis_incomplete',
  'visible_signature_origin',
]);

function readReason(raw: { status: Verdict; status_reason?: string }) {
  if (raw.status !== 'inconclusive') return null;
  // Absent or unrecognised: still inconclusive, route it the same way.
  if (!raw.status_reason || !KNOWN_REASONS.has(raw.status_reason)) {
    return 'unrecognised' as const;
  }
  return raw.status_reason;
}

For modification_markers, keep every ID as a plain string, known or not. Look up the ones you recognise, log the ones you do not, and never discard the result because an ID is new.

status_reason is not in the result’s required list. It appears only on inconclusive results, so the generated type should make it optional.

Placeholders are not measurements. When a check could not be completed in full, or on older records, counters and flags come back as 0, false or an empty string. A generated type cannot tell those apart from real values. Read status and status_reason first: analysis_incomplete is one signal that the numbers are placeholders, and older records can carry placeholders without it.

listChecks returns a reduced model. The list item has no modification_markers, date_sequence_valid, xref_count, signature counters or algorithm versions. The generator will produce a separate, smaller type for it. When you need the detail, call getResult with the item’s id. Pagination is limit (1–500, default 100) and offset, and the response carries data, has_more, total, limit and offset. The tool, creator and producer filters are exact and case-sensitive, and from_date and to_date are Unix timestamps in seconds.

Smoke-test with a test key

A test key accepts only the documented synthetic test URLs, returns deterministic results, and never debits credits. Call analyzePdf from your generated client with one of them; the raw request it should produce is:

curl -X POST https://api.htpbe.tech/v1/analyze \
  -H "Authorization: Bearer $HTPBE_TEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://api.htpbe.tech/v1/test/clean.pdf"}'

The response is 201 with the fixed ID 00000000-0000-4000-8000-000000000001. Synthetic IDs are valid UUID v4 strings, so a format: uuid validator in your generated code will accept them. getResult with that ID returns a synthetic intact result; .../test/modified-high.pdf and .../test/inconclusive.pdf give you the other two verdicts. These results are fixed test data, not analysis of any real file, so use them to exercise your types and branches, never as evidence of how a real document would be scored.

A few behaviours to expect, so they do not read as bugs in the generated code:

  • Sending a non-test URL with a test key returns 403 with code test_url_required. That is a good way to confirm your error branch fires.
  • getResult with a real check ID and a test key returns 404. Test keys resolve only synthetic IDs.
  • listChecks with a test key always returns an empty list.
  • Test keys accept Idempotency-Key but ignore it, so a test request never returns 409 or 422 idempotency_key_fingerprint_mismatch. Exercise those paths with a live key.
  • .../test/trigger-402.pdf returns 402 payment_required, the only way to reach that path with a test key.

The full list of test URLs and the scenario each returns is in the test-key guide.

What a generated client does not do

It gives you types and transport, not a policy. It will not:

  • retry 409, 429 or analysis_timeout, or honour Retry-After, unless you add that;
  • decide what to do with a verdict. intact means no modification was detected; it does not mean the document is genuine, and a file fabricated from scratch has no edit to find. inconclusive is the limit of the check, a normal branch to route, not an error to retry;
  • enforce the usage_caution object that comes with every result. safe_for_automated_adverse_decision is always false, and recommended_action is one of route_to_human_review, request_from_issuer or no_action. Your code has to read it and route accordingly;
  • tell you that the spec changed. That is the diff step above.

Next steps

Download openapi.json or openapi.yaml, generate, run the three checks on the result model, and smoke-test with a test key. Then wire the verdict routing from the verdict-handling post. When you are ready to run real documents, live keys draw from the credit pool described on the pricing page.

Share this article

Found this article helpful? Share it with others to spread knowledge about PDF security and fraud detection.

https://htpbe.tech/blog/generate-pdf-tamper-detection-client-from-openapi-spec

Secure your workflow

Create your account – check PDFs on the web or with an API key, both ready on signup.
No sales call. Cancel any time.