---
title: "Secure Echo APIs with API Key Authentication"
description: "Secure your Echo API using a shared secret."
canonicalUrl: "https://zuplo.com/use-cases/api-key-auth/go/echo/secure-header"
framework: "Echo"
language: "Go"
authStrategy: "shared secret header"
pageType: use-case
---

# Secure Echo APIs with API Key Authentication

Secure your Echo API using a shared secret.

## How Zuplo Handles It

Put Zuplo in front of your Echo backend to authenticate API keys and forward a shared secret header so your origin only accepts traffic from Zuplo.

## Echo Backend Code

```go
package main

import (
	"crypto/subtle"
	"net/http"
	"os"

	"github.com/labstack/echo/v4"
)

// Middleware to validate shared secret header
func validateSharedSecret(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		secret := c.Request().Header.Get("X-Shared-Secret")
		expectedSecret := os.Getenv("SHARED_SECRET")

		if expectedSecret == "" {
			return echo.NewHTTPError(http.StatusInternalServerError, "Server configuration error")
		}

		if secret == "" {
			return echo.NewHTTPError(http.StatusUnauthorized, "No secret provided")
		}

		// Use constant time comparison to prevent timing attacks
		if subtle.ConstantTimeCompare([]byte(secret), []byte(expectedSecret)) != 1 {
			return echo.NewHTTPError(http.StatusUnauthorized, "Invalid secret")
		}

		return next(c)
	}
}

func main() {
	e := echo.New()

	// Example route protected by the shared secret
	e.GET("/protected", func(c echo.Context) error {
		return c.JSON(http.StatusOK, map[string]string{
			"message": "Access granted",
		})
	}, validateSharedSecret)

	e.Start(":8080")
}
```

## Example Request

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

## Learn More

- [API Key Authentication on Zuplo](https://zuplo.com/docs/policies/api-key-auth-inbound)
- [JWT Authentication on Zuplo](https://zuplo.com/docs/policies/open-id-jwt-auth-inbound)
- [All use cases](https://zuplo.com/use-cases)
