OpenAPI Generator CLI

7.21.0 · active · verified Mon Apr 13

The `openapi-generator-cli` Python package provides a command-line interface (CLI) for the OpenAPI Generator, a robust tool that automatically generates API client libraries (SDKs), server stubs, documentation, and configuration from an OpenAPI Specification (supporting both 2.0 and 3.x). The Python package acts as a thin wrapper around the official Java-based OpenAPI Generator, downloading and executing the appropriate JAR file. It maintains an active development pace, with new versions often released daily to mirror updates in the core Java project, typically including numerous enhancements, bug fixes, and sometimes breaking changes with fallbacks.

Warnings

Install

Quickstart

This quickstart demonstrates how to install the `openapi-generator-cli` and use it to generate a Python client library from a public OpenAPI Specification. The `openapi-generator-cli` itself is a command-line tool, and direct Python imports are not typically used for the generator. Instead, you execute the CLI command to produce source code (e.g., an SDK) in a chosen language, which you then integrate into your project and import as a separate library. Replace `https://petstore.swagger.io/v2/swagger.json` with your own OpenAPI spec URL or file path, and adjust the generator (`-g python`) and output directory (`-o my_python_client`) as needed. The commented Python code provides an example of how a *generated* Python client might be used.

pip install openapi-generator-cli

# Check installed version
openapi-generator-cli version

# Generate a Python client from a public OpenAPI specification
# This will create a directory named 'my_python_client' containing the generated SDK
openapi-generator-cli generate \
  -i https://petstore.swagger.io/v2/swagger.json \
  -g python \
  -o my_python_client

# To use the generated client (example, assuming 'my_python_client' was created):
# cd my_python_client
# pip install .
#
# Then in a Python script:
# from openapi_client import api_client, configuration
# from openapi_client.api import pet_api
#
# # Configure API key authorization: api_key
# config = configuration.Configuration()
# config.api_key['api_key'] = 'YOUR_API_KEY_HERE'
#
# # Create an API client
# client = api_client.ApiClient(configuration=config)
# api = pet_api.PetApi(client)
#
# # Example: List pets (assuming the generated client structure)
# # try:
# #     all_pets = api.find_pets_by_status(status=['available'])
# #     print(all_pets)
# # except Exception as e:
# #     print(f"Error: {e}")

view raw JSON →