The Easiest and Quickest Way to Generate an OpenAPI Spec for an Existing Website
This article shows how to generate an OpenAPI spec for an existing website using HTTP Archive (HAR) files.
Join the DZone community and get the full member experience.Join For Free
If you're working on a project that involves building an API for an existing website, you'll need an OpenAPI specification to describe the API. An OpenAPI spec is a document describing the endpoints, request and response parameters, authentication requirements, and other API details. It can be a time-consuming task to create an OpenAPI spec from scratch, but there are tools available that can help you generate one quickly and easily.
In this blog post, we'll show you how to generate an OpenAPI spec for an existing website using HTTP Archive (HAR) files, which are recordings of a web browser's interactions with a website.
Step 1: Collect API Endpoints
Before generating an OpenAPI spec, you need to collect all the API endpoints for the website you're working on. One way to do this is by using the Chrome browser's built-in developer tools to monitor network traffic and save it as a .har file.
Here's how to use Chrome to collect API endpoints; I will use the Bitbucket website for my demo.
- Open Chrome and navigate to the website for which you want to create an OpenAPI spec.
- Open the Chrome developer tools by either clicking the three dots in the upper right corner of the browser window and selecting "More tools"> "Developer tools" or right-clicking anywhere on the page and selecting "Inspect" from the context menu.
- In the developer tools window that appears, select the "Network" tab.
- Select the "XHR" filter to display only the API requests.
- Refresh the page to start capturing network traffic.
- Perform the actions on the website that trigger the API calls you want to include in the spec.
- In the network traffic log, locate the API requests and click on them to view their details.
- Click the "Export" button at the top of the network traffic log and save the file as a .har file.
Step 2: Generate an OpenAPI Spec With Tools
There are several tools available that can help you generate an OpenAPI spec from the .har file you created in Step 1. We'll use APIGit, a Git platform for API development, to generate the spec in this example.
Here's how to use APIGit to generate an OpenAPI spec:
- But first, go to APIGit and sign up for a free account if you don't already have one.
- Once logged in, create a new repository for your OpenAPI spec by clicking the "Create Repository" button.
- Name your repository and give it a description.
- Under the "API Specifications" tab, click the "add or upload spec" button and choose the .har file you created in Step 1.
- Click the "Create" button and wait for APIGit to process the file.
- Once the file is processed, you'll see a visual representation of your API endpoints.
Step 3: Repeat Step 1 to Collect More Actions
Collecting all APIs for the website at once is difficult, so you'll most likely need to repeat Step 1 for different use cases. For example, if you're working with Bitbucket, you may have already collected the APIs for creating a repository and submitting a commit. Now you may want to collect the APIs for creating a branch, making a pull request, merging the pull request, and so on.
To collect APIs for these additional use cases, follow the same process as in Step 1. Open the Chrome developer tools, navigate to the relevant page on the website, perform the actions that trigger the API calls, and save the network traffic as a .har file.
Step 4: Import New .Har File to Generate APIs
As you work on your website, you may need to collect additional APIs or change existing ones. To update your OpenAPI spec with these changes, you can import a new .har file into APIGit and generate the updated API description.
When importing a new .har file, you have two options for handling duplication:
- Import the .har file into the existing spec: APIGit will automatically detect any duplicate APIs and only save one copy. To do this, navigate to the current API spec and click the "Edit" button to enter the API editing page. Then, click the "Import" button at the top and upload the new .har file.
- Import the .har file into a new spec: If you want to keep the original spec and compare it with the new one to see what has changed, you can create a new spec and use the diff function to compare the changes. To do this, follow the same steps as described in Step 1, but instead of importing the file into the existing spec, click the "add or upload spec" button to create a new spec. You can then use the diff view by clicking the "switch" button to compare the changes between the original and updated specs.
Step 5: Repeating Steps 3 and 4 Until All APIs Are Collected
Collecting all the APIs for a website can be a time-consuming process, especially if the website has a lot of functionality. It may take several rounds of browsing the site and collecting .har files before you have all the necessary APIs for your OpenAPI spec. Here are the steps to repeat until you have collected all the APIs:
- Repeat Step 1: Use Chrome Developer Tools to browse the website and collect .har files for additional use cases.
- Repeat Step 2: Generate an updated OpenAPI spec using the new .har files. Again, remember to choose the appropriate option for handling duplication when importing the new .har file.
- Review the updated spec and make any necessary edits to ensure accuracy and completeness.
- Repeat Steps 1–3 as needed until you have collected all the necessary APIs for your website.
Step 6: Completing and Enhancing Your APIs
Once you have collected all the necessary APIs and generated your OpenAPI spec using APIGit or another tool, it's time to review and enhance your API description.
A comprehensive API spec should include detailed descriptions of each API, request and response definitions, data validation, and other important details. Then, with the autogenerated spec in hand, you can start to make edits and fill in any missing information.
APIGit offers an intuitive OpenAPI editor that can help you complete your APIs more quickly and easily. With the editor, you can easily add or edit tags, parameters, responses, and other details for each API. You can also validate your API definition to ensure that it complies with the OpenAPI specification.
In addition to completing the API definition, you can also enhance your APIs by adding documentation and examples to help other developers understand how to use your API. This can include detailed tutorials, code snippets, and other resources that make it easier for other developers to get started with your API.
By completing and enhancing your APIs, you can ensure that your API documentation is accurate, complete, and easy to use. This will help other developers to work more efficiently with your API and can ultimately lead to more adoption and success for your project.
Opinions expressed by DZone contributors are their own.