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

# Secure Hono APIs with API Key Authentication

Secure your Hono API using a shared secret.

## How Zuplo Handles It

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

## Hono Backend Code

```typescript
import { Hono } from "hono";
import { Context } from "hono";
import { timingSafeEqual } from "crypto";

// Middleware to validate shared secret header
async function validateSharedSecret(c: Context, next: Function) {
  const secret = c.req.headers.get("x-shared-secret");
  const expectedSecret = process.env.SHARED_SECRET;

  if (!expectedSecret) {
    return c.json({ error: "Server configuration error" }, 500);
  }

  if (!secret) {
    return c.json({ error: "No secret provided" }, 401);
  }

  // Use timing-safe comparison to prevent timing attacks
  if (
    secret.length !== expectedSecret.length ||
    !timingSafeEqual(Buffer.from(secret), Buffer.from(expectedSecret))
  ) {
    return c.json({ error: "Invalid secret" }, 401);
  }

  await next();
}

const app = new Hono();

// Example usage
app.get("/protected", validateSharedSecret, (c) => {
  return c.json({ message: "Access granted" });
});

export default app;
```

## 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)
