Zuplo

API Key Authentication

Secure Your HAProxy API with API Keys

The Zuplo API Gateway protects your backend API from unauthorized access, abuse, and overload. Add API key authentication to your HAProxy 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 HAProxy 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 HAProxy 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 HAProxy 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 HAProxy API with the Shared Secret

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

CC
// Example HAProxy configuration with Lua script for shared secret validation

// First, ensure Lua is enabled in your HAProxy configuration
global
    lua-load /etc/haproxy/validate_secret.lua

frontend my_frontend
    bind *:80
    http-request lua.validate_secret

    # Define the backend to forward requests to if validated
    default_backend my_backend

backend my_backend
    server my_server localhost:8080

// Lua script: /etc/haproxy/validate_secret.lua
core.register_action("validate_secret", {"http-req"}, function(txn)
    -- Fetch the shared secret from environment variable
    local expected_secret = os.getenv("SHARED_SECRET")
    if not expected_secret then
        txn:set_var("txn.error_message", "Server configuration error")
        txn:done()
        return txn:send_response(500, "Internal Server Error", txn:get_var("txn.error_message"))
    end

    -- Get the secret from the HTTP header
    local secret = txn.req:get_header("x-shared-secret")
    if not secret then
        txn:set_var("txn.error_message", "No secret provided")
        txn:done()
        return txn:send_response(401, "Unauthorized", txn:get_var("txn.error_message"))
    end

    -- Perform a timing-safe comparison
    if #secret ~= #expected_secret then
        txn:set_var("txn.error_message", "Invalid secret")
        txn:done()
        return txn:send_response(401, "Unauthorized", txn:get_var("txn.error_message"))
    end

    local matched = 0
    for i = 1, #secret do
        if secret:byte(i) ~= expected_secret:byte(i) then
            matched = 1
        end
    end

    if matched == 1 then
        txn:set_var("txn.error_message", "Invalid secret")
        txn:done()
        return txn:send_response(401, "Unauthorized", txn:get_var("txn.error_message"))
    end

    -- If no error, proceed to the backend
    txn:done()
end)
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 HAProxy 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 HAProxy API in minutes.