If you're like me and enjoy poking around the network tab of your browser, you've probably noticed that most modern web applications communicate with their backend via HTTP requests. These requests are often made to RESTful endpoints, which are typically well-documented and easy to understand.
However, there are times when you may need to reverse engineer a frontend application to understand its API. This is where site-to-swagger comes in handy. It's a tool that can transform HTTP Archive (HAR) files into OpenAPI (formerly known as Swagger) documentation. By analyzing HAR files, it systematically extracts endpoint data and responses to generate comprehensive OpenAPI documentation.
Ensure the following prerequisites are installed:
- Node.js
- NPM
To get started, install the required packages using:
npm install
tldr: Open Browser with devtools.
- Open the Network tab in the DevTools
- Record your network activity by clicking the Record button or pressing Ctrl + E.
- Perform the actions that you want to record.
- Download the HAR file
- Replace the HAR file in
./examples
folder. (Rename as example.har or editsrc/server.ts
to point to your file) - Run
npm start
- Open
http://localhost:5678/docs
in your browser or checkexamples/example.json
andexamples/example.yaml
for the generated swagger files. - BONUS CONTENT - GENERATE CLIENTS AND SERVERS WITH
https://editor-next.swagger.io/
Paste the contents ofexamples/example.yaml
into the editor and generate clients and servers for your favorite language
-
Load HAR Content:
- Reads the HAR file and converts its content into a JSON format.
-
Information Extraction:
- Iterates over each HTTP request/response entry in the HAR file.
- Analyzes request specifics such as URL, method, query parameters, headers, and body.
- Processes responses labeled with a
content-type
ofapplication/json
.
-
OpenAPI/Swagger Generation:
- Transforms the gathered information into the OpenAPI format, detailing paths, methods, parameters, and responses.
- Translates JSON bodies from both requests and responses into JSON Schema.
-
Reporting:
- Constructs a summary report detailing insights into endpoints, utilized methods, and available response codes.
-
Resulting Output:
- Outputs the constructed Swagger documentation in a consumable JSON format. And YAML. And makes Swagger docs available at
http://localhost:5678/docs
.
- Outputs the constructed Swagger documentation in a consumable JSON format. And YAML. And makes Swagger docs available at
- Supports Multiple HAR Files:
- The tool can process multiple HAR files at once, combining the results into a single OpenAPI document.
- Supports Multiple HTTP Methods:
- The tool supports all HTTP methods, including
GET
,POST
,PUT
,PATCH
,DELETE
,HEAD
, andOPTIONS
.
- The tool supports all HTTP methods, including
- Supports Multiple Query Parameters:
- The tool supports multiple query parameters, including those with the same name.
- Tags by Path:
- The tool tags each endpoint by its path, grouping similar endpoints together. It intelligently ignores common path parameters like
/api/v1/*
to make more accurate tags.
- The tool tags each endpoint by its path, grouping similar endpoints together. It intelligently ignores common path parameters like
- Automatically creates dynamic path parameters:
- The tool automatically creates path parameters for each unique path parameter found in the HAR file by detecting UUIDs, IDs, and other common path parameters. e.g.
/api/v1/users/{userId}
.
- The tool automatically creates path parameters for each unique path parameter found in the HAR file by detecting UUIDs, IDs, and other common path parameters. e.g.
- Serves Swagger Documentation:
- The tool serves the generated Swagger documentation at
http://localhost:5678/docs
for easy consumption.
- The tool serves the generated Swagger documentation at
- Responses are processed only if tagged with a
content-type
ofapplication/json
. - If a parameter or header is identified in a request, the tool assumes its necessity for that specific request method.
- During the JSON Schema generation process, array-first items are utilized as references, and
null
values are treated as string types to ensure OpenAPI compatibility.