// HTTP4K

Functional, modular and testable API Clients

http4k Connect is a lightweight API Client toolkit which includes libraries for connecting to popular third-party cloud services and AI backends

// INTEGRATIONS

More than 35 integrations

http4k Connect integrates with many popular Cloud and AI HTTP APIs.

OpenAI Anthropic Ollama LangChain DynamoDB S3 Lambda SQS EventBridge Firehose CloudWatch KMS Kafka GitLab Mattermost Google Analytics

Highlights

Tiny
footprint

In true http4k-style, Connect clients rely on minimal dependencies and zero reflection, perfect for lightweight and security-conscious applications.

Total
modularisation

Extension of the clients is as simple as combining function calls or implementing a single data class with just 2 simple methods.

Kotlin-
centric

Every API Client leverages Result types and is exception-safe, so you can finally code like it’s Kotlin that you’re writing!

Battle-tested
in the wild

Connect API Clients have been used to build high volume applications in Banking, Publishing, eCommerce & Government projects.

First class
testability

Most API Clients come with an in-memory cloud-service fake which can be used for lightning fast test suites or spun up as a server.

All fakes are chaos-enabled, so you can test what happens to your code in failure scenarios.

Total
control

Most API Client libraries hide their HTTP client from you, stopping you from implementing observability or customisation.

Connect allows any http4k client module to be plugged in, so you can leverage the full power of http4k

Ultimate
type safety

API Clients leverage TinyTypes to provide typesafe coding and automatic serialisation.

Stop relying on unsafe, stringly-typed APIs.

Simple Key/value
storage

Ships with a storage abstraction for simple key-value storage, and adapters for popular backends.

Plugin in-memory, S3, JDBC or Redis with a single line of code!

// HOW IT WORKS

Use, extend or build API clients

Use and extend the built-in API clients...

  1. 01

    Use a client

    Create the API client and call it. Inject your own HTTP client for observability.

    ListBuckets.kt
    val s3 = S3.Http(http = JavaHttpClient())
    
    val buckets: Result<BucketList, RemoteFailure> = s3.listBuckets()
    
    val bucketNames: List<BucketName> = buckets.map { it.items }.orThrow()
  2. 02

    Add an action

    Is your API action not supported? No problem - write your own as an extension.

    InvertBucket.kt
    data class InvertBucket(val bucketName: BucketName) : S3Action<BucketName> {
        override fun toRequest() = Request(POST, Uri.of("/$bucketName/invert"))
    
        override fun toResult(response: Response) =
            if (response.status.successful)
                Success(BucketName.of(response.bodyString()))
            else Failure(asRemoteFailure(response))
    }
    
    fun S3.invertBucket(bucketName: BucketName) = invoke(InvertBucket(bucketName))

... or build your own API client in four steps

  1. 01

    Define an action

    An API Action is an interface with functions that marshal the contents of HTTP messages.

    APIAction.kt
    interface APIAction<R> {
        fun toRequest(): Request
        fun toResult(response: Response): Result<R, RemoteFailure>
    }
  2. 02

    Write the client

    An API Client is a class with a single function. It handles the transport for the remote API.

    API.kt
    class API(rawHttp: HttpHandler) {
        private val transport = SetBaseUriFrom(Uri.of("https://api.com"))
            .then(rawHttp)
    
        operator fun <R> invoke(action: APIAction<R>): Result<R, RemoteFailure> =
            action.toResult(transport(action.toRequest()))
    }
  3. 03

    Implement actions

    Implement each action with a single class and an extension function. Compose calls together without bloating your API Client.

    Reverse.kt
    data class Reverse(val value: String) : APIAction<String> {
        override fun toRequest() = Request(POST, "/reverse").body(value)
    
        override fun toResult(response: Response): Result<String, RemoteFailure> =
            Success(response.bodyString())
    }
    
    fun API.reverse(value: String) = this(Reverse(value))
  4. 04

    Call it

    Create the API Client with an HTTP client, and call it as normal. Failures are trapped in the returned Result.

    Usage.kt
    val api = API(JavaHttpClient())
    
    val result: Result<String, RemoteFailure> = api.reverse("hello")

Learn from our examples

See how to harness the http4k Connect toolkit in your projects.

Check it out
pumb
scarf