OpenAPI Python Generator
The OpenAPI Python Generator tool creates Python FastAPI server code based on your OpenAPI specification 3.0 specification. This code helps you quickly scaffold API endpoints defined in your OpenAPI document, so that you can focus on implementing business logic instead of writing boilerplate code.
Important: Ensure that you have created OpenAPI document beforehand.
- Key highlights
-
- Generates a Python FastAPI backend server based on an input OpenAPI 3.0 specification.
- Supports generation of two different server types, a mock server and a direct to database Postgres server.
The following operations are supported in the OpenAPI Python Generator tool:
Parameter | Required | Description | Default |
---|---|---|---|
input_file
|
Yes | The OpenAPI Specification in YAML or JSON format. | None |
server_type
|
No | The server type you want to generate. You can choose between the simple or database-crud server types | Simple |
Simple Server
A minimalist server providing the basic setup to start building your service.
- Example prompt
-
Generate some Python FastAPI server code based on the following openapi @
Database CRUD Operations Server
An extension of the Simple Server that incorporates database logic to perform Create, Read, Update, and Delete (CRUD) operations, suitable for applications that require persistent data management.
- Example prompt
-
Build FastAPI backend code, including some database integration, using the OpenAPI file @
Generate python-based server code
To generate a python code, complete the following steps:
- Log in to API Agent. For details, see Getting started.
- Enter the following prompt, and click
Send:
and select the required file, for example,Generate FastAPI server code from the openapi document @
order_apispec.yaml
.Note: Entering @ after the prompt displays a list of OpenAPI documents available in your workspace. - Click Send. The OpenAPI Python Generator tool communicates with the API Agent sources and displays the proposed plan.
- Click Start to begin code generation. Once complete, you can view a brief summary of the next steps required to download the generated code as well as how to run and deploy it.
- Click the download icon to save the ZIP file containing the code.
- In Visual Studio Code, open the Terminal and run the following command to unzip the downloaded
file into a folder, for example,
app-code
.unzip PostgreSQL-order-python.zip
- Open the
README.md
file to view the instructions on how to get started with the downloaded code. TheREADME.md
provides detailed information on how to build, install, customize, and deploy the application. - Go to the Explorer pane, open the
database-props
file. - Replace the existing password with
9Iron-head
, and save the file. - Follow the steps in the Installation and Running the Server sections of the
README.md
file to build, install, and run the server.- Open a Terminal window and navigate to the extracted
folder:
cd app-code
- Create a virtual environment
python3 -m venv venv
- Activate the Virtual Environment.
- For Unix or macOS:
source ./venv/bin/activate
- For Windows:
.\venv\Scripts\activate
- For Unix or macOS:
- Install the required packages listed in
requirements.txt
pip install -r requirements.txt
- Run the server using the following
command
The server will be accessible at:PYTHONPATH=src uvicorn openapi_server.main:app --host 0.0.0.0 --port 8080
http://localhost:8080
- Open a Terminal window and navigate to the extracted
folder:
- To deploy the application, run the
deploy.py
script with the required input arguments as shown below.
Replace the placeholders with the following:python deploy.py <IBM Cloud API key> <IBM Cloud Resource Group name> <IBM Cloud Project name> <ICR Namespace name>
<IBM Cloud API key>
: The IAM API key for your IBM Cloud account.<IBM Cloud Resource Group name>
: The resource group to be used within the specified IBM Cloud account.<IBM Cloud Project name>
: A custom name for your IBM Code Engine project, application, and associated artifacts. This value will serve as a prefix for all IBM Code Engine artifacts created during deployment.<ICR Namespace name>
: The name of an existing IBM Container Registry (ICR) namespace within your IBM Cloud account. This namespace will serve as the destination where IBM Code Engine pushes the built container image for your application source code. Ensure this namespace exists in the global region of the ICR (for example,icr.io/<ICR Namespace name>
).
For example,
(replacepython deploy.py <ibm-cloud-apikey> default xxx-app-default testfest
uniqueapikey
with API key, andxxx-app-default
with your unique app name) - Once the script completes, a success message similar to the following will appear if the
deployment was successful:
App <IBM Cloud Project name>-app deployed successfully. App <IBM Cloud Project name>-app URL: https://<IBM Cloud Project name>-app.1o0xvbar1wkk.us-south.codeengine.appdomain.cloud
- Copy the app URL from the success message and paste it into a browser.
- Open the
openapi.yaml
file, copy the endpoint from the GET request, and append the endpoint to the app URL in the browser's address bar. - Run the request in the browser to view the application result.
- To modify the business logic, refer to the Customizing Logic section in
README.md
.