Skip to main content

Getting Started with OpenAPI Specification

In this guide we will generate Python and Java SDKs from an example OpenAPI Specification hosted at a fake endpoint.

System Requirement

Konfig's CLI requires Node 14+ to be setup on your machine.

1. Install konfig-cli

Run npm install -g konfig-cli or yarn global add konfig-cli to install Konfig's CLI.

CLI

_10
npm install -g konfig-cli

2. Create a directory and cd into it.

CLI

_10
/
_10
❯ mkdir my-sdks
_10
_10
/
_10
❯ cd my-sdks

3. Import your OpenAPI Specification (OAS)

In this example our OAS is hosted at https://api.petstore.com/petstore.yaml so we use the wget utility to download it (note that this is not a real endpoint). If your OAS is already locally available, you can simply copy it into your directory.

CLI

_12
/
_12
❯ wget https://api.petstore.com/petstore.yaml
_12
--2023-06-25 04:14:36-- https://api.petstore.com/petstore.yaml
_12
Resolving api.petstore.com (api.petstore.com)... 18.130.97.172, 35.178.30.108
_12
Connecting to api.petstore.com (api.petstore.com)|18.130.97.172|:443... connected.
_12
HTTP request sent, awaiting response... 200 OK
_12
Length: 125390 (122K) [application/json]
_12
Saving to: ‘petstore.yaml’
_12
_12
petstore.yaml 100%[=================================================================================================>] 122.45K 408KB/s in 0.3s
_12
_12
2023-06-25 04:14:37 (408 KB/s) - ‘petstore.yaml’ saved [125390/125390]

4. Setup your SDK directory

Run konfig init to initialize your directory with the necessary files to generate SDKs. You will be asked onboarding questions to bootstrap your configuration.

CLI
petstore.yaml

_11
/my-sdks
_11
❯ konfig init
_11
Downloaded version 1.1.118 of https://www.npmjs.com/package/konfig-spectral-ruleset
_11
Downloading Konfig's lint rules... done
_11
Setting up Spectral... done
_11
Setting up VScode settings.json... done
_11
? Select languages to generate SDKs for: Python, TypeScript
_11
? What is the SDK package name? Use hyphens to separate words (ex. acme-web) petstore
_11
? What is your Git user ID? konfig-dev
_11
? What is your Git repository name? my-sdks
_11
? What is the relative path to your spec? (ie. "<CURRENT_DIRECTORY>/path/to/api.yaml") petstore.yaml

After you complete the onboarding questions, you will see should have two files konfig.yaml and petstore.yaml in your directory.

CLI
petstore.yaml
konfig.yaml

_15
/my-sdks
_15
❯ konfig init
_15
Downloaded version 1.1.118 of https://www.npmjs.com/package/konfig-spectral-ruleset
_15
Downloading Konfig's lint rules... done
_15
Setting up Spectral... done
_15
Setting up VScode settings.json... done
_15
? Select languages to generate SDKs for: Python, TypeScript
_15
? What is the SDK package name? Use hyphens to separate words (ex. acme-web) petstore
_15
? What is your Git user ID? konfig-dev
_15
? What is your Git repository name? my-sdks
_15
? What is the relative path to your spec? (ie. "<CURRENT_DIRECTORY>/path/to/api.yaml") petstore.yaml
_15
_15
/my-sdks
_15
❯ ls
_15
konfig.yaml petstore.yaml

konfig init will also setup other miscellaneous files for linting and VSCode.

CLI
petstore.yaml
konfig.yaml

_25
/my-sdks
_25
❯ konfig init
_25
Downloaded version 1.1.118 of https://www.npmjs.com/package/konfig-spectral-ruleset
_25
Downloading Konfig's lint rules... done
_25
Setting up Spectral... done
_25
Setting up VScode settings.json... done
_25
? Select languages to generate SDKs for: Python, TypeScript
_25
? What is the SDK package name? Use hyphens to separate words (ex. acme-web) petstore
_25
? What is your Git user ID? konfig-dev
_25
? What is your Git repository name? my-sdks
_25
? What is the relative path to your spec? (ie. "<CURRENT_DIRECTORY>/path/to/api.yaml") petstore.yaml
_25
_25
/my-sdks
_25
❯ ls
_25
konfig.yaml petstore.yaml
_25
_25
/my-sdks
_25
❯ ls -a -1
_25
.
_25
..
_25
.konfig
_25
.spectral.yaml
_25
.vscode
_25
konfig.yaml
_25
petstore.yaml

The konfig.yaml configures what languages to generate SDKs for and how to generate them.

CLI
petstore.yaml
konfig.yaml

_22
# yaml-language-server: $schema=https://unpkg.com/konfig-lib@latest/konfig-yaml.schema.json
_22
_22
outputDirectory: /tmp/petstore-sdks-out
_22
specPath: petstore.yaml
_22
generators:
_22
python:
_22
version: 1.0.0
_22
packageName: petstore_client
_22
projectName: petstore-python-sdk
_22
outputDirectory: python
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/python
_22
typescript:
_22
version: 1.0.0
_22
npmName: petstore-typescript-sdk
_22
outputDirectory: typescript
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/typescript

Each generator has a version field to specify the version of the SDK to generate. We recommend using semantic versioning.

CLI
petstore.yaml
konfig.yaml

_22
# yaml-language-server: $schema=https://unpkg.com/konfig-lib@latest/konfig-yaml.schema.json
_22
_22
outputDirectory: /tmp/petstore-sdks-out
_22
specPath: petstore.yaml
_22
generators:
_22
python:
_22
version: 1.0.0
_22
packageName: petstore_client
_22
projectName: petstore-python-sdk
_22
outputDirectory: python
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/python
_22
typescript:
_22
version: 1.0.0
_22
npmName: petstore-typescript-sdk
_22
outputDirectory: typescript
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/typescript

An outputDirectory to specify the subpath relative to konfig.yaml that the SDK should be copied to.

CLI
petstore.yaml
konfig.yaml

_22
# yaml-language-server: $schema=https://unpkg.com/konfig-lib@latest/konfig-yaml.schema.json
_22
_22
outputDirectory: /tmp/petstore-sdks-out
_22
specPath: petstore.yaml
_22
generators:
_22
python:
_22
version: 1.0.0
_22
packageName: petstore_client
_22
projectName: petstore-python-sdk
_22
outputDirectory: python
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/python
_22
typescript:
_22
version: 1.0.0
_22
npmName: petstore-typescript-sdk
_22
outputDirectory: typescript
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/typescript

And git field to configure the userId and repoId of where the SDKs will be version controlled on GitHub.

CLI
petstore.yaml
konfig.yaml

_22
# yaml-language-server: $schema=https://unpkg.com/konfig-lib@latest/konfig-yaml.schema.json
_22
_22
outputDirectory: /tmp/petstore-sdks-out
_22
specPath: petstore.yaml
_22
generators:
_22
python:
_22
version: 1.0.0
_22
packageName: petstore_client
_22
projectName: petstore-python-sdk
_22
outputDirectory: python
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/python
_22
typescript:
_22
version: 1.0.0
_22
npmName: petstore-typescript-sdk
_22
outputDirectory: typescript
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/typescript

Other configurations are language-specific that change things like the package name or top-level class name used when generating the SDKs.

CLI
petstore.yaml
konfig.yaml

_22
# yaml-language-server: $schema=https://unpkg.com/konfig-lib@latest/konfig-yaml.schema.json
_22
_22
outputDirectory: /tmp/petstore-sdks-out
_22
specPath: petstore.yaml
_22
generators:
_22
python:
_22
version: 1.0.0
_22
packageName: petstore_client
_22
projectName: petstore-python-sdk
_22
outputDirectory: python
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/python
_22
typescript:
_22
version: 1.0.0
_22
npmName: petstore-typescript-sdk
_22
outputDirectory: typescript
_22
clientName: Petstore
_22
git:
_22
userId: konfig-dev
_22
repoId: my-sdks/tree/main/typescript

5. Generate SDKs

Run konfig generate to generate SDKs. In the background konfig will pick up your konfig.yaml along with your OAS and make a request to generate SDKs (this can take a minute or two).

CLI
petstore.yaml
konfig.yaml

_15
/my-sdks
_15
❯ konfig generate
_15
Output directory set to: /tmp/petstore-sdks-out
_15
Generating python, typescript SDKs... done
_15
Downloading 2 SDKs... done
_15
Deleting output directory... done
_15
Creating output directory... done
_15
Extracting SDKs... done
_15
Deleting contents of existing directory "python"... done
_15
Copying Python SDK to "python"... done
_15
Deleting contents of existing directory "typescript"... done
_15
Copying Typescript SDK to "typescript"... done
_15
Generating top-level README.md...
_15
Generating top-level README.md... done
_15
Writing top-level LICENSE... done

Once generation is complete, you will see two new directories containing your Python and TypeScript SDKs.

CLI
petstore.yaml
konfig.yaml

_42
/my-sdks
_42
❯ tree -L 2
_42
.
_42
├── LICENSE
_42
├── README.md
_42
├── flows
_42
│ ├── python
_42
│ └── typescript
_42
├── konfig.yaml
_42
├── petstore.yaml
_42
├── python
_42
│ ├── LICENSE
_42
│ ├── README.md
_42
│ ├── docs
_42
│ ├── petstore_client
_42
│ ├── poetry.lock
_42
│ ├── pyproject.toml
_42
│ ├── setup.cfg
_42
│ ├── setup.py
_42
│ └── test
_42
└── typescript
_42
├── README.md
_42
├── api
_42
├── api.ts
_42
├── base.ts
_42
├── client-custom.ts
_42
├── client.ts
_42
├── common.ts
_42
├── configuration.ts
_42
├── docs
_42
├── index.ts
_42
├── jest.config.ts
_42
├── models
_42
├── package.json
_42
├── pagination
_42
├── requestAfterHook.ts
_42
├── requestBeforeHook.ts
_42
├── tsconfig.json
_42
├── tsconfig.test.json
_42
└── yarn.lock
_42
_42
12 directories, 25 files

1. Install konfig-cli

Run npm install -g konfig-cli or yarn global add konfig-cli to install Konfig's CLI.

2. Create a directory and cd into it.

3. Import your OpenAPI Specification (OAS)

In this example our OAS is hosted at https://api.petstore.com/petstore.yaml so we use the wget utility to download it (note that this is not a real endpoint). If your OAS is already locally available, you can simply copy it into your directory.

4. Setup your SDK directory

Run konfig init to initialize your directory with the necessary files to generate SDKs. You will be asked onboarding questions to bootstrap your configuration.

After you complete the onboarding questions, you will see should have two files konfig.yaml and petstore.yaml in your directory.

konfig init will also setup other miscellaneous files for linting and VSCode.

The konfig.yaml configures what languages to generate SDKs for and how to generate them.

Each generator has a version field to specify the version of the SDK to generate. We recommend using semantic versioning.

An outputDirectory to specify the subpath relative to konfig.yaml that the SDK should be copied to.

And git field to configure the userId and repoId of where the SDKs will be version controlled on GitHub.

Other configurations are language-specific that change things like the package name or top-level class name used when generating the SDKs.

5. Generate SDKs

Run konfig generate to generate SDKs. In the background konfig will pick up your konfig.yaml along with your OAS and make a request to generate SDKs (this can take a minute or two).

Once generation is complete, you will see two new directories containing your Python and TypeScript SDKs.

CLI

_10
npm install -g konfig-cli

Congrats 🎉🎉🎉🎉

You have successfully generated your first SDKs. Next you can test and publish your SDKs which is also made easy by Konfig using konfig test and konfig publish.

info

When doing this on a real API you most likely will need to make modifications to your OAS to generate high quality SDKs. Konfig also makes this easy through its lint and fix utilities. You also probably want to setup Continuous Deployment through GitHub Action or polling. Sometimes its also necessary to add business logic in the SDKs which are also made easy by konfig through its native support for customization.


Interested?

If you are interested in generating SDKs with Konfig we would love to setup a time to talk. In the call we will learn about your particular requirements and answer any questions you might have. Typically it only takes a couple hours to get high quality SDKs and documentation after a conversation.