The unreasonably easy (and a little dirty) way to create an API client

Christian Sprecher
Sep 3, 2019 · 3 min read
neon sign that says “hello”
Photo by Drew Beamer on Unsplash

APIs

APIs are all the rage nowadays: every service should present a clear interface to interact with.

API Clients

Thankfully, the client side looks way simpler: if your API is well described (e.g. OpenAPI), it is easy to create a client with the appropriate tools:

An API client, the fast way

  1. Get a json which describes a request or response as completely as possible
  2. Go to api.quicktype.io and generate your kotlin classes
  3. Tweak those classes and make them easily serializable with kotlinx.serialization
  4. Use khttp to create a simple http client in kotlin
  5. Do awesome stuff
Example, straight from https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-v2-1/
Example, straight from https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-v2-1/
Generated Request Classes
Generated Response Classes
Everything serializable and therefore ready to be used in requests and responses

Summary

No schemas or semi-cooked API clients were hurt for this blog post. I presented a quick and fun way to explore APIs using some technologies that work well together. APIs are the cornerstone of a service oriented IT landscape, and being able to work with them is one of the most important skills for any IT developer and engineer.