Skip to main content

How to generate and use a Java SDK in Kotlin

This tutorial applies to the following SDK languages:

TypeScriptJavaPythonC#GoPHP
Kotlin Supported

liblab generates Java SDKs that are fully compatible with Kotlin.

SDKs simplify the developer experience by encapsulating API features into accessible functions, avoiding direct API complexities and streamlining integration.

Kotlin's modern design and seamless Java interoperability make it an excellent development choice, while IntelliJ IDEA offers robust tools to support Kotlin projects.

This tutorial shows how to import and use liblab’s Java SDK in Kotlin projects with Maven and IntelliJ IDEA.

tip

Before getting started you'll want to make sure you have a liblab account and the liblab CLI installed.

You'll also need:

  • Maven version 3.6 or higher installed.
  • IntelliJ IDEA. You can use the Community Edition, which is free and open source.

Configure liblab

Start by setting up a liblab project so you can generate the SDK. Follow the steps to create and configure the liblab project.

Create a folder for the SDK project:

mkdir -p kotlin-sdk
cd kotlin-sdk

Initialize the liblab CLI in this folder by running the following command. This will create a file called liblab.config.json:

liblab init

Update the liblab.config.json file with the following changes:

  • Change "specFilePath" to "https://raw.githubusercontent.com/PokeAPI/pokeapi/refs/heads/master/openapi.yml". This tutorial uses the PokéAPI spec, but you can use your own API.
  • For "languages", keep only the "java" option.
  • (optional) Update the SDK and API names:
{
"sdkName": "kotlin-sdk",
"apiVersion": "1.0.0",
"apiName": "kotlin-sdk",
"specFilePath": "https://raw.githubusercontent.com/PokeAPI/pokeapi/refs/heads/master/openapi.yml",
"languages": [
"java"
],
//..
}
SDK Configuration

Explore the configuration documentation to discover the available settings and enhancements.

Generate the SDK

With the configuration set, generate the SDK using the following command:

liblab build -y

The liblab CLI will validate the configuration, OpenAPI specs, and build the SDK. You should see output like:

✓ No issues detected in the liblab config file.

No hooks found, SDKs will be generated without hooks.

⚠ Validation succeeded with warnings

Created /Users/username/projects/tests/kotlin-sdk/output/api-schema-validation.json with the full linting results

Your SDKs are being generated. Visit the liblab portal (https://app.liblab.com/apis/kotlin-api/builds/1234) to view more details on your build(s).
✓ Java built
Successfully generated SDK for Java. ♡ You can find it inside: /Users/username/projects/tests/kotlin-sdk/output

You'll find your SDK files in the output directory:

output/
├── api-schema-validation.json
└── java/

Test the SDK using IntelliJ

IntelliJ Required

IntelliJ IDEA is recommended for Kotlin applications. Testing the examples with other IDEs, such as Visual Studio Code, may not work correctly.

Open IntelliJ IDEA, select the generated Kotlin example directory (output/java/kotlin-example) containing the pom.xml file, and click OK.

IntelliJ opening Kotlin example

After opening the project, you must generate the SDK .jar file. Open a terminal in IntelliJ and run the following commands:

cd ../
mvn clean install

Now open src/main/kotlin/Main.kt and click on the Play button to run MainKt.

Execute the SDK example using the IntelliJ IDEA interface

This will call the pokemon.abilityList method from the SDK. This method queries a list of Pokémon abilities using pagination. The terminal output will look like this:

PaginatedAbilitySummaryList(
count=367,
next=https://pokeapi.co/api/v2/ability/?offset=10&limit=8,
previous=https://pokeapi.co/api/v2/ability/?offset=0&limit=2,
results=[
AbilitySummary(
name=speed-boost,
url=https://pokeapi.co/api/v2/ability/3/
),
AbilitySummary(
name=battle-armor,
url=https://pokeapi.co/api/v2/ability/4/
),
AbilitySummary(
name=sturdy,
url=https://pokeapi.co/api/v2/ability/5/
),
...
]
)

Process finished with exit code 0
info

You can explore all other PokeAPI docs and code snippets by checking the generated SDK documentation in the folder output/java/documentation. Kotlin-specific documentation will have the .kt.md file extension.

Next Steps

Now that you've packaged your SDKs you can learn how to integrate them with your CI/CD pipeline and publish them to their respective package manager repositories.

We currently have guides for: