Skip to main content

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 client
  • ClientFormat.REACT - React-optimized client with hooks
  • ClientFormat.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 to ClientFormat.FETCH
  • output_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 spec
  • TypeError: If openapi_spec is neither a dict nor a string

ClientFormat Enum

  • ClientFormat.FETCH - Generate a client using the native Fetch API
  • ClientFormat.REACT - Generate a client optimized for React applications
  • ClientFormat.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 typescript and tsx)
  • typescript and tsx (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

openapi_ts_client-1.1.2.tar.gz (49.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_ts_client-1.1.2-py3-none-any.whl (47.8 kB view details)

Uploaded Python 3

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

Hashes for openapi_ts_client-1.1.2.tar.gz
Algorithm Hash digest
SHA256 fd0be3360b9e37771044a2065a894889e341a04426f40086a1dc50e2b3cb1806
MD5 c9e7b3740bf85ab4e4acc2fb5b3d4640
BLAKE2b-256 90af4a217a9819ffeca6697b55baa81e2daa178e4f93d61d28ed7d0b62bcaedf

See more details on using hashes here.

File details

Details for the file openapi_ts_client-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_ts_client-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5557dbafb540b09291e20a87e09b3e60f61c0df8fcdfaf4b300a4a81f6958712
MD5 9ab5a56cc3312d47bbd8e44400197210
BLAKE2b-256 bc9d3a24cb5891b1678c1af7fac9748a4f1298d8b3fcdd3586618da329281be3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page