Zuplo

API Key Authentication

Secure Your Http4k API with API Keys

The Zuplo API Gateway protects your backend API from unauthorized access, abuse, and overload. Add API key authentication to your Http4k API in minutes.

Secure Access

Authenticate every request before it reaches your backend.

Control Costs

Set rate limits and quotas to prevent runaway usage.

Ensure Reliability

Shield your backend from overload with traffic management.

How it works

The Zuplo API Gateway sits between your clients and your Http4k backend, providing a secure layer of protection and control.

Client
Zuplo API Gateway

Customer VPC

Backend

Step-by-step tutorial

It takes only a few minutes to put Zuplo in front of your Http4k backend, adding API key authentication, and configuring your origin to trust requests from Zuplo using shared secrets.

1

Create a Route in Zuplo

First, create a new route in your Zuplo project that will proxy requests to your Http4k backend. This route will be the entry point for your API consumers.

Creating a route in Zuplo
Learn how to create routes

📄 OpenAPI native. Import your existing OpenAPI spec to instantly create routes and power your API documentation .

2

Add API Key Authentication Policy

Add the API Key Authentication policy to your route. This policy validates incoming API keys and ensures only authorized consumers can access your API.

Adding API Key Authentication policy in Zuplo
API Key Authentication docs

🔐 Leaked key? No problem. As a GitHub Secret Scanning partner, Zuplo can automatically revoke exposed keys before they can be exploited.

3

Add Set Header Policy

Add the Set Header policy to your route. This policy sets the shared secret header on requests to ensure only requests from Zuplo are accepted by your API.

Adding Set Header policy in Zuplo
Set Header Policy docs
4

Secure Your Http4k API with the Shared Secret

Configure your Http4k backend to validate the shared secret header set by Zuplo. This ensures that only requests coming through your Zuplo gateway are accepted.

KotlinKotlin
import org.http4k.core.Filter
import org.http4k.core.HttpHandler
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.filter.ServerFilters
import org.http4k.lens.Header
import java.security.MessageDigest
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

val sharedSecret = System.getenv("SHARED_SECRET") ?: error("Server configuration error")

fun timingSafeEqual(a: ByteArray, b: ByteArray): Boolean {
    if (a.size != b.size) return false
    var result = 0
    for (i in a.indices) {
        result = result or (a[i].toInt() xor b[i].toInt())
    }
    return result == 0
}

fun validateSharedSecret(): Filter = Filter { next ->
    { request ->
        val secretHeader = Header.required("x-shared-secret")

        val secret = secretHeader(request)
        val expectedSecretBytes = sharedSecret.toByteArray()
        val secretBytes = secret.toByteArray()

        if (!timingSafeEqual(secretBytes, expectedSecretBytes)) {
            Response(Status.UNAUTHORIZED).body("Invalid secret")
        } else {
            next(request)
        }
    }
}

val app: HttpHandler = ServerFilters.InitialiseRequestContext()
    .then(validateSharedSecret())
    .then { Response(Status.OK).body("Access granted") }

fun main() {
    val request = Request(GET, "/protected").header("x-shared-secret", System.getenv("SHARED_SECRET") ?: "")
    println(app(request))
}
5

Call Your API Through Zuplo

Now you can call your API through Zuplo using an API key. The request will be authenticated at the gateway, and securely forwarded to your Http4k backend.

Terminalbash
curl -X GET \
  'https://your-api.zuplo.dev/your-route' \
  -H 'Authorization: Bearer YOUR_API_KEY'

Ready to secure your API?

Get started with Zuplo for free and add API key authentication to your Http4k API in minutes.