Need help with swagger-axios-codegen?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

195 Stars 63 Forks MIT License 368 Commits 13 Opened issues


swagger client to use axios and typescript

Services available


Need anything else?

Contributors list


A swagger client uses axios and typescript

GitHub Workflow StatusNpmVersionnpm open issues

require node > v8.0.0

it will always resolve
or reject
with Promise

support other similar to

library, for example Fly.js, required setting
ISwaggerOptions.useCustomerRequestInstance = true

the es6 version is generated by calling typescript

Welcome PRs and commit issue

By the way. you can support this repo via Star star sta st s... ⭐️ ⭐️ ⭐️ ⭐️ ⭐️




Get Started

  yarn add swagger-axios-codegen
export interface ISwaggerOptions {
  /** service name suffix eg. 'Service' **/
  serviceNameSuffix?: string
  /** enum prefix eg. 'Enum' **/
  enumNamePrefix?: string
  methodNameMode?: 'operationId' | 'path'
  /** path of the generated file eg. './src/service' **/
  outputDir?: string
  /** generated file name eg. 'index.ts' **/
  fileName?: string
  /** path to remote source file eg. 'https://localhost:8080/api/v1/swagger.json' **/
  remoteUrl?: string
  /** path to local source file eg. './swagger.json' **/
  source?: any
  useStaticMethod?: boolean | undefined
  /** client can pass custom headers to the service methods **/
  useCustomerRequestInstance?: boolean | undefined
  /** filter by service name (first tag) or method name using multimatch ( **/
  include?: Array
  /** include extra types which are not included during the filtering Eg. ["Foo", "Bar"] **/
  includeTypes?: Array
  /** filter urls by following clauses **/
  urlFilters?: Array
  /** custom function to format the output file (default: prettier.format()) **/
  format?: (s: string) => string
  /** match with tsconfig */
  strictNullChecks?: boolean | undefined
  /** definition Class mode */
  modelMode?: 'class' | 'interface'
  /** use class-transformer to transform the results */
  useClassTransformer?: boolean,
  // force the specified swagger or openAPI version,
  openApi?: string | undefined,
  // extend file url. It will be inserted in front of the service method
  extendDefinitionFile?: string | undefined
  // mark generic type
  extendGenericType?: string[] | undefined
  /** split request service.  Can't use with sharedServiceOptions*/
  multipleFileMode?: boolean | undefined
  /** shared service options to multiple service. Can't use with MultipleFileMode */
  sharedServiceOptions?: boolean | undefined

const defaultOptions: ISwaggerOptions = { serviceNameSuffix: 'Service', enumNamePrefix: 'Enum', methodNameMode: 'operationId', outputDir: './service', fileName: 'index.ts', useStaticMethod: true, useCustomerRequestInstance: false, include: [], strictNullChecks: true, /** definition Class mode ,auto use interface mode to streamlined code*/ modelMode?: 'interface' useClassTransformer: false }

use local swagger api json

const { codegen } = require('swagger-axios-codegen')
  methodNameMode: 'operationId',
  source: require('./swagger.json')

use remote swagger api json

const { codegen } = require('swagger-axios-codegen')
  methodNameMode: 'operationId',
  remoteUrl:'You remote Url'

use static method

    methodNameMode: 'operationId',
    remoteUrl: 'http://localhost:22742/swagger/v1/swagger.json',
    outputDir: '.',
    useStaticMethod: true


import { UserService } from './service'
const userService = new UserService()
await userService.GetAll();


import { UserService } from './service'

await UserService.GetAll();

use custom axios.instance

import axios from 'axios'
import { serviceOptions } from './service'
const instance = axios.create({
  baseURL: '',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}

serviceOptions.axios = instance

use other library

import YourLib from ''
import { serviceOptions } from './service'

serviceOptions.axios = YourLib

filter service and method

fliter by multimatch using 'include' setting

  methodNameMode: 'path',
  source: require('../swagger.json'),
  outputDir: './swagger/services',
  include: [
    // 'Products*',
    { 'User': ['*', '!history'] },

If you are using special characters in your service name (which is the first tag) you must assume they have been escaped.

For example the service names are

// API
"paths": {
  "/Products/Get": {
    "post": {
      "tags": [
      "operationId": "Get",
// Codegen config
  methodNameMode: 'path',
  source: require('../swagger.json'),
  outputDir: './swagger/services',
  include: ['MyAppFirstModule*'] // Only Products and Customers will be included. As you can see dots are escaped being contract names.

use class transformer to transform results

This is helpful if you want to transform dates to real date objects. Swagger can define string formats for different types. Two if these formats are


If a

is enabled and a format is set on a string, the result string will be transformed to a

// swagger.json

  "ObjectWithDate": {
    "type": "object",
    "properties": {
      "date": {
        "type": "string",
        "format": "date-time"
const { codegen } = require('swagger-axios-codegen')
  methodNameMode: 'operationId',
  useClassTransformer: true,

Resulting class:

export class ObjectWithDate {
  @Type(() => Date)
  public date: Date;

The service method will transform the json response and return an instance of this class

use validation model

    modelMode: 'class',
    generateValidationModel: true

The option above among with class model mode allows to render the model validation rules. The result of this will be as follows:

export class FooFormVm {
  'name'?: string;
  'description'?: string;

constructor(data: undefined | any = {}) { this['name'] = data['name']; this['description'] = data['description']; }

public static validationModel = { name: { required: true, maxLength: 50 }, description: { maxLength: 250 }, }; }

So you can use the validation model in your application:

function isRequired(vm: any, fieldName: string): boolean {
  return (vm && vm[fieldName] && vm[fieldName].required === true);
function maxLength(vm: any, fieldName: string): number {
  return (vm && vm[fieldName] && vm[fieldName].maxLength ? vm[fieldName].maxLength : 4000);

Now you can use the functions

var required = isRequired(FooFormVm.validationModel, 'name');
var maxLength = maxLength(FooFormVm.validationModel, 'description');
At the moment there are only two rules are supported -

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.