Generating python code from an OpenAPI

API Agent helps you to generate Python code from an OpenAPI specification, deploy the generated code to IBM Code Engine, and update the source OpenAPI document to reflect the Code Engine application URL after deployment.

About this task

Use API Agent to generate Python FastAPI server code based on your OpenAPI Specification 3.0. For more details about how to generate the Python FastAPI server code, see OpenAPI Python Generator tool.

The following steps gives you a single scenario flow example of how to create server code for a database CRUD server.

Procedure

To generate a Python code, complete the following steps:

  1. Log in to API Agent. For details, see Getting started.
  2. Enter the following prompt, and click Send:
    Generate some Python FastAPI server with database interaction code based on the following openapi @
    and select the required file, for example, order_apispec.yaml.
    Note: Entering @ after the prompt displays a list of OpenAPI documents available in your workspace.
  3. Click Send. The OpenAPI Python Generator tool communicates with the API Agent sources and displays the proposed plan.
  4. Click Start to begin code generation. Once complete, you can view a brief summary of the next steps that are required to download the generated code as well as how to run and deploy it.
  5. Click the download icon to save the ZIP file containing the code.
  6. In Visual Studio Code, open the Terminal and run the following command to extract the downloaded file into a folder, for example, app-code.
    unzip PostgreSQL-order-python.zip
  7. Open the README.md file to view the instructions on how to get started with the downloaded code. The README.md provides detailed information on how to build, install, customize, and deploy the application.
  8. Enter the following prompt to deploy zip up the generated folder (ensure that your credentials are filled in):
    zip -r code.zip app_code/
    Note: The file size must be less than 1MB. Remove the venv/ folder before zipping your code and deploying.
  9. The next action suggested:
    Deploy the application code from code.zip
    Note: If the next planned action is to deploy the generated Python FastAPI server application to IBM Code Engine using the Code deployment tool, ensure that the source OpenAPI document from which the application was generated is updated after deployment to reflect the Code Engine application URL as the value of servers[0].url. This step is crucial if the subsequent planned action is to create a draft API and publish it in API Connect using the API Connect Task tool. For more information, see the Code deployment status section.