Zuplo

API Key Authentication

Secure Your AdonisJS API with API Keys

The Zuplo API Gateway protects your backend API from unauthorized access, abuse, and overload. Add API key authentication to your AdonisJS 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 AdonisJS 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 AdonisJS backend, adding API key authentication, and configuring your origin to trust requests from Zuplo using JWT verification.

1

Create a Route in Zuplo

First, create a new route in your Zuplo project that will proxy requests to your AdonisJS 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

Enable the JWT Service Plugin

Enable the JWT Service Plugin in your Zuplo project. This plugin generates JWTs that your origin API can validate, creating a secure trust relationship between Zuplo and your backend.

TypeScriptmodules/zuplo.runtime.ts
export function runtimeInit(runtime: RuntimeExtensions) {
  // Register the JWT Service Plugin
  runtime.addPlugin(new JwtServicePlugin());
}
JWT Service Plugin docs
4

Secure Your AdonisJS API with JWT Authentication

Configure your AdonisJS backend to validate the JWTs issued by Zuplo. This ensures that only requests coming through your Zuplo gateway are accepted.

TypeScript
import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import jwt from "jsonwebtoken";
import jwksClient from "jwks-rsa";

// Replace with your actual Zuplo deployment name or custom domain
const ISSUER = "https://my-api-a32f34.zuplo.api/__zuplo/issuer";

// Create a JWKS client to fetch public keys
const client = jwksClient({
  jwksUri: `${ISSUER}/.well-known/jwks.json`,
  cache: true,
  cacheMaxAge: 600000, // 10 minutes
});

// Function to get the signing key
async function getKey(header: jwt.JwtHeader): Promise<string> {
  const key = await client.getSigningKeyAsync(header.kid);
  return key.getPublicKey();
}

// Middleware to validate JWT
export async function validateJwt(
  { request, response }: HttpContextContract,
  next: () => Promise<void>,
) {
  try {
    const authHeader = request.header("authorization");
    const token = authHeader?.replace("Bearer ", "");

    if (!token) {
      return response.status(401).json({ error: "No token provided" });
    }

    const decoded = await new Promise((resolve, reject) => {
      jwt.verify(
        token,
        (header, callback) => {
          getKey(header)
            .then((key) => callback(null, key))
            .catch(callback);
        },
        { issuer: ISSUER, algorithms: ["RS256"] },
        (err, decoded) => {
          if (err) {
            reject(err);
          } else {
            resolve(decoded);
          }
        },
      );
    });

    request["user"] = decoded;
    await next();
  } catch (err) {
    return response
      .status(401)
      .json({ error: "Invalid token", details: err.message });
  }
}

// Example usage in a controller
export default class ProtectedController {
  public async show({ request, response }: HttpContextContract) {
    response.json({
      message: "Access granted",
      user: request["user"],
    });
  }
}

// Register the middleware in start/kernel.ts
// import { validateJwt } from 'path/to/your/middleware'
// Server.middleware.registerNamed({
//   jwtAuth: validateJwt
// })

// Apply middleware to routes in start/routes.ts
// Route.get('/protected', 'ProtectedController.show').middleware('jwtAuth')
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 a JWT will be forwarded to your AdonisJS 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 AdonisJS API in minutes.