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+

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-0.1.0.tar.gz (14.3 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-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file openapi_ts_client-0.1.0.tar.gz.

File metadata

  • Download URL: openapi_ts_client-0.1.0.tar.gz
  • Upload date:
  • Size: 14.3 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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f796df989af5ccf7fc5f1de12edfde805ebc255bd24d4993463e46ec5f519b8d
MD5 c85b5665ebc5de6c142d253664015773
BLAKE2b-256 c222573d61e17f1a10b9fb4519d8e962fd7c0a0c19c5f9cffb27279c091ea913

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openapi_ts_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b05ac00ab997225e8040464f1521031b38f9fa17d72fd73ef8c761efb40c9a98
MD5 05cd83f4101d09f47e3fced61be93ac4
BLAKE2b-256 03efe4722e3d5f035da528e25d00860223e2ab3abbfebaa2a4d66e1347fc68c5

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