harspec GitHub

How it works

harspec is a small pipeline. Each stage is a short Python module you can read in a few minutes.

StageModuleWhat happens
1. Parse and filterhar.pyReads log.entries, decodes bodies, drops html, js, css, images, fonts, failed requests and CORS preflights.
2. Redactredact.pyRemoves secrets immediately, before the data goes anywhere else.
3. Group endpointspaths.pyTurns concrete URLs into templates with path parameters.
4. Infer schemasschema.pyBuilds a JSON schema per sample and merges them.
5. Detect authauth.pyFinds credential types, login endpoints and CSRF headers.
6. Build the specopenapi.pyAssembles OpenAPI 3.0.3 with operations, parameters, bodies, responses, security.
7. Generategenerators/Each output format is produced from the spec, not from raw traffic.

Path parameters

These segments become parameters: numbers, UUIDs, long hex strings, dates, and long mixed alphanumeric IDs. The parameter is named after the previous word.

/users/42/posts/7        ->  /users/{user_id}/posts/{post_id}
/owners/3f2a-...-9c1/pets ->  /owners/{owner_id}/pets
/v1/pets                 ->  /v1/pets          (v1 stays literal)

Short mixed IDs such as sku123 are detected by clustering: if three or more different values share the same surrounding path, harspec treats that position as a parameter. Turn this off with --no-cluster.

Schema inference

Seen in the captureResult
Field in every samplerequired
Field missing in some samplesoptional
null in some samplesnullable: true
Integer in one sample, decimal in anothernumber
String in one, number in anotheroneOf
UUID, date-time, date, email, URL stringsformat set accordingly
Same path, several status codesone response per status
Query parameter in every callrequired; otherwise optional

Query values arrive as text, so limit=10 is typed integer. Repeated keys become arrays.

Auth detection

harspec looks at request headers and cookies (values already redacted) and responses of login-like endpoints:

SignalDetected as
Authorization: Bearer ...bearerAuth (HTTP bearer)
Authorization: Basic ...basicAuth
X-API-Key, X-Auth-Token and similarAPI key in header
Cookie named like session, sid, auth, jwtcookieAuth
2xx JSON response on a login, token or oauth path containing a token fieldlogin endpoint, listed in AUTH.md
X-CSRF-Token and similarCSRF header, listed in AUTH.md

When a request sends both a token and a session cookie, the operation is marked with the token scheme only.

Why generators read the spec

Because every generator takes the OpenAPI dictionary, a generator never needs to understand HAR files, redaction or inference. It just walks paths. That keeps each generator around 60 lines and makes new ones easy to write.