This guide provides instructions for using Kotlin SDK generated by the Postman SDK Generator. It covers how to import the SDK into your Kotlin project, run example code, and best practices for using the SDK effectively in your applications.
Postman SDK Generator generates Java SDKs that are fully compatible with Kotlin. Since Kotlin runs on the JVM and has seamless Java interoperability, you can use the generated Java SDK directly in your Kotlin projects. The SDK structure, build configuration, and publishing process are identical to the Java SDK. This guide provides Kotlin-specific code examples for all SDK features.
The Java SDK includes the following key files:
[SdkName].java — Main SDK client, instantiated with [SdkName]Config.services/ — Service classes with both sync and async methods.models/ — Data models (POJOs with Lombok builders, enums, typed exceptions) with Jackson serialization.config/ — Configuration classes for auth, retry, and SDK client settings.pom.xml — Specifies dependencies (OkHttp, Jackson, Lombok).The following is an example of the typical structure of a generated Java SDK:
Each generated SDK includes an examples/ directory with a working project demonstrating SDK usage.
The example includes the following features:
examples/src/main/kotlin/Main.kt
You can import the generated SDK into your Kotlin project using Gradle, Maven, or project file path. Below are instructions for each method.
To import the generated SDK using Gradle, first install it to your local Maven repository:
Then, add the dependency to your build file:
Alternatively, if you want to reference the SDK as a JAR file, first build the fat JAR:
Then, add the JAR to your build file:
To import the generated SDK using a local Maven repository, do the following:
Install SDK to local Maven repository.
Add dependency to your project’s pom.xml.
Use the SDK in your code.
To import the generated SDK using the project file path, do the following:
Add SDK as a module in your project’s pom.xml.
Reference as dependency.
To share your generated Java SDK with the community, you can publish it to Maven Central. This allows other developers to easily add it as a dependency in their Maven or Gradle projects. Follow the steps below to publish your SDK to Maven Central.
To publish your SDK to Maven Central, you need the following:
To set up your environment for publishing to Maven Central, do the following:
Create account and verify your namespace:
com.example) through DNS or GitHub verification.Generate a GPG key.
Configure Maven settings.xml. Add the following to ~/.m2/settings.xml:
Add the following to your SDK’s pom.xml:
Deploy to Maven Central.
With autoPublish set to true, artifacts are automatically published after validation. If set to false, you can manually publish from the Central Portal UI at central.sonatype.com.
Verify the publication. After deployment, verify that your artifacts are available in the staging repository on the Central Portal, for example, https://repo1.maven.org/maven2/com/example/your-sdk/. Once verified, they’re released to Maven Central.
To publish updates to your SDK, do the following:
Use the following best practices when publishing your Java SDK to Maven Central:
You can further customize the SDK by modifying the generated code, adding new features, or integrating it with your existing codebase. Here are some advanced topics to explore.
The SDK supports various authentication methods. You can configure authentication when initializing the SDK client.
The SDK includes authentication support based on the security schemes defined in your API specification. The following examples show the possible authentication methods.
To update the credentials after initialization, run one of the following. The setApiKey, setAccessToken, and setBasicAuthCredentials methods are each only generated when the corresponding auth type is in the specification.
Every service method has a sync variant (methodName) and an async variant (methodNameAsync). Sync methods return the type directly (for example, Pet), while
async methods return CompletableFuture<T> (for example, CompletableFuture<Pet>).
For void methods, async returns CompletableFuture<Void>.
If you use Kotlin coroutines, you can convert CompletableFuture to a suspending call with the kotlinx-coroutines-jdk8 library:
The SDK supports multiple environments (for example, production, staging) with different base URLs.
Java supports all four levels of configuration: SDK (through SdkConfig or sdk.setEnvironment()/sdk.setBaseUrl()), and service, method, and request levels (through RequestConfig). Each level overrides the previous, with request being most specific.
The SDK raises custom exceptions for API errors, which include details about the error message, status code, and response body. You can catch these exceptions to handle errors gracefully in your application.
To handle errors, do the following:
To handle specific error codes, do the following:
When the API specification defines error response models, the SDK generates typed exception classes (for example, NotFoundErrorException) that extend ApiError and include the deserialized error model. The exception’s getMessage() and getStatus() work directly, but to access specification-defined error fields, use getError().
To set a global timeout, do the following:
To pass the retry configuration through SdkConfig, do the following:
The SDK generates validators that enforce OpenAPI constraints at runtime. These include minLength, maxLength, pattern, min, max, uniqueItems, and more.
Validation happens automatically when making API calls. The SDK validates parameters before sending the request. If validation fails, a ValidationException is thrown with a list of Violation objects describing what went wrong. You don’t need to call validators manually. They’re built into the service methods.
The list of supported constraints can also be grouped by type like this:
minLength, maxLength, pattern (regex)min, max (inclusive or exclusive)minItems, maxItems, uniqueItemsThe SDK uses JsonNullable<T> wrappers for optional fields, providing clear distinction between undefined, null, and present values. This is important for users building request models. Omitting an optional field versus explicitly setting it to null produces different JSON payloads.
Consider the following resources for using and customizing your Java SDK from Kotlin:
documentation/ directory in your SDK.mvn javadoc:javadoc.examples/ directory for a working code sample demonstrating basic SDK usage.CompletableFuture to Kotlin coroutines with .await().jackson-databind-nullable, a dependency used for optional nullable field handling.