harspec GitHub

Getting started

From zero to a generated client in five minutes. You only need Python 3.9 or newer.

1. Install

pip install harspec

Check it worked: harspec --version. If the command is not found, use python -m harspec instead.

2. Record a HAR file

A HAR file is a recording of every request the page made. All major browsers can export one.

BrowserHow
Chrome / EdgeF12, Network tab, tick Preserve log, use the site, right click a request, Save all as HAR with content.
FirefoxF12, Network tab, use the site, click the gear icon, Save All As HAR.
SafariDevelop menu, Show Web Inspector, Network tab, use the site, Export.

Tips for a good capture: log in, open the main screens, search, create and delete something. Every action you do adds endpoints to the spec. Use the Fetch/XHR filter if the list is noisy (harspec also filters automatically).

Important: The HAR contains live credentials. Use a test account, delete the file when done, and never commit it. harspec redacts secrets in its output, not in your original file.

Mobile apps? Route the phone through a proxy such as mitmproxy or Charles and export the session as HAR, then use harspec the same way.

3. Run harspec

harspec capture.har

You will see a summary like this:

harspec 0.1.0: 14 requests -> 11 API calls on api.petshop.example
  6 endpoints across 4 paths
  auth: bearerAuth, cookieAuth (login: POST /v1/auth/login)
  wrote to harspec-out/: openapi.yaml, AUTH.md, examples.sh, ...

If the capture has several hosts (for example an API plus analytics), harspec picks the busiest and tells you the others. Choose one with --host api.example.com.

4. Use the output

FileWhat to do with it
openapi.yamlOpen in Swagger Editor (editor.swagger.io), import into Postman or Insomnia, or feed to any OpenAPI tool.
client.tsCopy into your TypeScript project. Works with any runtime that has fetch (browsers, Node 18+, Deno, Bun).
client.pyCopy into your Python project. Needs pip install requests.
examples.shSet TOKEN and run any command to try an endpoint.
postman_collection.jsonPostman: Import, then set the token variable.
AUTH.mdRead first: it explains how to obtain a token.

Python client

from client import Client

api = Client(token="YOUR_TOKEN")
pets = api.get_pets(params={"limit": 10})
pet = api.get_pet(42)

TypeScript client

import { Client } from "./client";

const api = new Client({ token: "YOUR_TOKEN" });
const pets = await api.getPets({ limit: 10 });

Errors throw ApiError with the HTTP status and parsed body.

Try it without a HAR

The repository ships a sample capture of a fake pet shop API:

git clone https://github.com/AYUSH-PANDEY023/harspec.git
cd harspec
python -m harspec examples/sample.har