Generate TypeScript clients from OpenAPI specifications
Project description
openapi-ts-client
Generate TypeScript clients from OpenAPI specifications (2.0/Swagger and 3.x).
Installation
pip install openapi-ts-client
Or install from source:
pip install -e .
Usage
Basic Usage
from openapi_ts_client import generate_typescript_client, ClientFormat
# Load your OpenAPI specification (3.x example)
spec = {
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {
"/users": {
"get": {
"summary": "Get all users",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}
# Generate TypeScript client with default settings (Fetch API, current directory)
result = generate_typescript_client(spec)
print(result)
OpenAPI 2.0 (Swagger) Example
from openapi_ts_client import generate_typescript_client
# OpenAPI 2.0 specs use "swagger" instead of "openapi"
spec = {
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {}
}
result = generate_typescript_client(spec)
Using JSON String Input
from openapi_ts_client import generate_typescript_client
# Load spec from a JSON file
with open("openapi.json", "r") as f:
json_string = f.read()
result = generate_typescript_client(json_string)
Custom Output Format
The package supports three output formats:
ClientFormat.FETCH(default) - Native Fetch API clientClientFormat.REACT- React-optimized client with hooksClientFormat.ANGULAR- Angular-optimized client with services
from openapi_ts_client import generate_typescript_client, ClientFormat
# Generate a React client
result = generate_typescript_client(
spec,
output_format=ClientFormat.REACT
)
# Generate an Angular client
result = generate_typescript_client(
spec,
output_format=ClientFormat.ANGULAR
)
Custom Output Path
from openapi_ts_client import generate_typescript_client
# Output to a specific directory
result = generate_typescript_client(
spec,
output_path="./generated/api-client"
)
Full Example
from openapi_ts_client import generate_typescript_client, ClientFormat
# Load your OpenAPI spec
with open("api-spec.json", "r") as f:
spec = f.read()
# Generate a React client in a specific directory
result = generate_typescript_client(
openapi_spec=spec,
output_format=ClientFormat.REACT,
output_path="./src/api"
)
print(result)
API Reference
generate_typescript_client(openapi_spec, output_format=ClientFormat.FETCH, output_path=".")
Generate a TypeScript client from an OpenAPI specification.
Parameters:
openapi_spec(dict | str): The OpenAPI specification as a dictionary or JSON string. Supports both OpenAPI 2.0 (Swagger) and OpenAPI 3.x.output_format(ClientFormat, optional): The output client format. Defaults toClientFormat.FETCHoutput_path(str | Path, optional): The output directory path. Defaults to current directory"."
Returns:
str: A status message indicating the result of the generation process
Raises:
ValueError: If the specification is not a valid OpenAPI specTypeError: Ifopenapi_specis neither a dict nor a string
ClientFormat Enum
ClientFormat.FETCH- Generate a client using the native Fetch APIClientFormat.REACT- Generate a client optimized for React applicationsClientFormat.ANGULAR- Generate a client optimized for Angular applications
Supported OpenAPI Versions
- OpenAPI 2.0 (Swagger)
- OpenAPI 3.0.x
- OpenAPI 3.1.x
Requirements
- Python 3.8+
- Node.js (for
typescriptandtsx) typescriptandtsx(for verifying/running generated code)
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openapi_ts_client-1.1.2.tar.gz.
File metadata
- Download URL: openapi_ts_client-1.1.2.tar.gz
- Upload date:
- Size: 49.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd0be3360b9e37771044a2065a894889e341a04426f40086a1dc50e2b3cb1806
|
|
| MD5 |
c9e7b3740bf85ab4e4acc2fb5b3d4640
|
|
| BLAKE2b-256 |
90af4a217a9819ffeca6697b55baa81e2daa178e4f93d61d28ed7d0b62bcaedf
|
File details
Details for the file openapi_ts_client-1.1.2-py3-none-any.whl.
File metadata
- Download URL: openapi_ts_client-1.1.2-py3-none-any.whl
- Upload date:
- Size: 47.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5557dbafb540b09291e20a87e09b3e60f61c0df8fcdfaf4b300a4a81f6958712
|
|
| MD5 |
9ab5a56cc3312d47bbd8e44400197210
|
|
| BLAKE2b-256 |
bc9d3a24cb5891b1678c1af7fac9748a4f1298d8b3fcdd3586618da329281be3
|