Generate TypeScript API clients from OpenAPI/Swagger specifications with support for Fetch and Axios HTTP clients
—
The swagger-typescript-api command-line interface provides full access to the code generation capabilities with comprehensive options for integration into build pipelines and development workflows.
# Install globally
npm install -g swagger-typescript-api
# Install locally in project
npm install --save-dev swagger-typescript-api# Main command with subcommands
swagger-typescript-api [command] [options]
sta [command] [options] # Short alias
# Available subcommands
swagger-typescript-api generate [options] # Generate TypeScript API client
swagger-typescript-api generate-templates [options] # Generate EJS templates
# Help and version
swagger-typescript-api --help # Show help
swagger-typescript-api --version # Show versionThe main command for generating TypeScript API clients from OpenAPI specifications.
# Generate from local file
swagger-typescript-api generate --path ./openapi.json
# Generate from URL
swagger-typescript-api generate --path https://petstore.swagger.io/v2/swagger.json
# Generate with custom output
swagger-typescript-api generate --path ./api-spec.yaml --output ./src/api --name ApiClient.ts# Input/Output Options
--path, -p <path> # Path or URL to OpenAPI schema (required)
--output, -o <path> # Output directory (default: "./")
--name, -n <filename> # Output filename (default: "Api.ts")
--templates, -t <path> # Custom templates directory
# HTTP Client Options
--axios # Use Axios HTTP client
--http-client <type> # HTTP client type: "fetch" | "axios"
# Generation Options
--client # Generate API client class (default: true)
--responses, -r # Generate response type definitions
--route-types # Generate route type definitions
--modular # Generate separate files for client, contracts, routes# Type Generation Options
--add-readonly # Generate readonly properties
--another-array-type # Use Array<Type> instead of Type[]
--extract-enums # Extract enums to TypeScript enum construction
--generate-union-enums # Generate enums as union types (T1 | T2 | TN)
--enum-names-as-values # Use x-enumNames as enum values
# Extraction Options
--extract-request-body # Extract request body types to data contracts
--extract-response-body # Extract response body types to data contracts
--extract-response-error # Extract response error types to data contracts
--extract-request-params # Extract request parameters to data contracts
--extract-responses # Extract all responses from /components/responses
# Processing Options
--default-as-success, -d # Use "default" response status as success response
--patch # Fix small errors in swagger schema definition
--unwrap-response-data # Unwrap data item from response
--disable-throw-on-error # Don't throw errors on non-successful responses
# Naming & Organization
--api-class-name <name> # Name of the main API class (default: "Api")
--type-prefix <prefix> # Prefix for generated type names
--type-suffix <suffix> # Suffix for generated type names
--module-name-index <n> # Path index for route module separation
--module-name-first-tag # Use first tag for module names
# Code Style Options
--sort-types # Sort data contracts alphabetically
--sort-routes # Sort routes alphabetically
--single-http-client # Allow HttpClient instance in Api constructor
# Output Options
--js # Generate JavaScript with declaration files
--clean-output # Clean output directory before generation
--default-response <type># Default type for empty response schema (default: "void")# Debug & Logging
--debug # Show detailed processing information
--silent # Output only errors to console
# Configuration
--custom-config <path> # Path to custom configuration file# Basic API generation
swagger-typescript-api generate --path ./openapi.json --output ./src/api
# Generate with Axios and response types
swagger-typescript-api generate \
--path https://api.example.com/openapi.json \
--output ./api \
--axios \
--responses \
--route-types
# Generate modular structure with enums
swagger-typescript-api generate \
--path ./api-spec.yaml \
--output ./generated \
--modular \
--extract-enums \
--extract-request-body \
--extract-response-body
# Generate with custom naming and sorting
swagger-typescript-api generate \
--path ./swagger.json \
--name MyApiClient.ts \
--api-class-name MyApi \
--type-prefix API \
--sort-types \
--sort-routes
# Generate JavaScript output
swagger-typescript-api generate \
--path ./openapi.json \
--js \
--output ./dist/apiGenerate customizable EJS templates for API code generation.
# Generate default templates
swagger-typescript-api generate-templates --output ./my-templates
# Generate modular templates
swagger-typescript-api generate-templates \
--output ./templates \
--modular \
--http-client axios# Output Options
--output, -o <path> # Output directory for templates
--clean-output # Clean output directory before generation
--rewrite, -r # Overwrite existing templates
# Template Configuration
--http-client <type> # HTTP client type for templates: "fetch" | "axios"
--modular # Generate modular template structure
# Debug Options
--debug # Show detailed processing information
--silent # Output only errors to console# Generate Fetch-based templates
swagger-typescript-api generate-templates \
--output ./templates/fetch \
--http-client fetch
# Generate Axios modular templates
swagger-typescript-api generate-templates \
--output ./templates/axios-modular \
--http-client axios \
--modular \
--clean-output
# Use custom templates in generation
swagger-typescript-api generate \
--path ./openapi.json \
--templates ./my-custom-templates \
--output ./apiCreate a custom configuration file for complex setups:
// swagger-typescript-api.config.js
export default {
hooks: {
onCreateRoute: (routeData) => {
// Custom route processing
return routeData;
},
onFormatTypeName: (typeName) => {
// Custom type naming
return `I${typeName}`;
}
},
primitiveTypeConstructs: (struct) => ({
...struct,
string: {
...struct.string,
'date-time': () => 'Date',
}
})
};# Use custom configuration
swagger-typescript-api generate \
--path ./openapi.json \
--custom-config ./swagger-typescript-api.config.js{
"scripts": {
"api:generate": "swagger-typescript-api generate --path ./api-spec.json --output ./src/api",
"api:dev": "swagger-typescript-api generate --path http://localhost:3000/api-docs --output ./src/api --debug",
"api:build": "swagger-typescript-api generate --path ./openapi.yaml --output ./dist/api --js --clean-output"
}
}# GitHub Actions example
- name: Generate API Client
run: |
npx swagger-typescript-api generate \
--path https://api.production.com/openapi.json \
--output ./src/generated-api \
--responses \
--route-types \
--clean-output#!/bin/bash
# build-api.sh
# Download latest API spec
curl -o ./temp-spec.json https://api.example.com/openapi.json
# Generate TypeScript client
swagger-typescript-api generate \
--path ./temp-spec.json \
--output ./src/api \
--modular \
--extract-enums \
--sort-types \
--clean-output
# Clean up
rm ./temp-spec.json
echo "API client generated successfully"The CLI provides detailed error messages and exit codes:
# Check exit code
swagger-typescript-api generate --path ./invalid.json
echo $? # Non-zero exit code on failureCommon Error Scenarios:
Debug Mode:
# Enable debug output for troubleshooting
swagger-typescript-api generate --path ./schema.json --debugInstall with Tessl CLI
npx tessl i tessl/npm-swagger-typescript-api