How it works
harspec is a small pipeline. Each stage is a short Python module you can read in a few minutes.
| Stage | Module | What happens |
|---|---|---|
| 1. Parse and filter | har.py | Reads log.entries, decodes bodies, drops html, js, css, images, fonts, failed requests and CORS preflights. |
| 2. Redact | redact.py | Removes secrets immediately, before the data goes anywhere else. |
| 3. Group endpoints | paths.py | Turns concrete URLs into templates with path parameters. |
| 4. Infer schemas | schema.py | Builds a JSON schema per sample and merges them. |
| 5. Detect auth | auth.py | Finds credential types, login endpoints and CSRF headers. |
| 6. Build the spec | openapi.py | Assembles OpenAPI 3.0.3 with operations, parameters, bodies, responses, security. |
| 7. Generate | generators/ | 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 capture | Result |
|---|---|
| Field in every sample | required |
| Field missing in some samples | optional |
null in some samples | nullable: true |
| Integer in one sample, decimal in another | number |
| String in one, number in another | oneOf |
| UUID, date-time, date, email, URL strings | format set accordingly |
| Same path, several status codes | one response per status |
| Query parameter in every call | required; 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:
| Signal | Detected as |
|---|---|
Authorization: Bearer ... | bearerAuth (HTTP bearer) |
Authorization: Basic ... | basicAuth |
X-API-Key, X-Auth-Token and similar | API key in header |
| Cookie named like session, sid, auth, jwt | cookieAuth |
| 2xx JSON response on a login, token or oauth path containing a token field | login endpoint, listed in AUTH.md |
X-CSRF-Token and similar | CSRF 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.