Zuplo

API Key Authentication

Secure Your API with API Keys

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

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

rust
use rocket::{get, routes, Rocket, Route, request::{self, FromRequest, Request}, http::Status};
use rocket::http::hyper::header::AUTHORIZATION;
use rocket::async_trait;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation, jwk::JwkSet};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use rocket::tokio::sync::RwLock;

const ISSUER: &str = "https://my-api-a32f34.zuplo.api/__zuplo/issuer";
const JWKS_URI: &str = "https://my-api-a32f34.zuplo.api/__zuplo/issuer/.well-known/jwks.json";

#[derive(Debug, Deserialize)]
struct Claims {
    sub: String,
    // Add other claims fields if needed
}

#[derive(Debug)]
struct JwtToken(pub Claims);

#[async_trait]
impl<'r> FromRequest<'r> for JwtToken {
    type Error = ();

    async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, ()> {
        let keys = request.rocket().state::<Arc<RwLock<JwkSet>>>().expect("JWKS not initialized");

        let auth_header = request.headers().get_one(AUTHORIZATION.as_str());
        if let Some(token) = auth_header.and_then(|h| h.strip_prefix("Bearer ")) {
            let validation = Validation {
                iss: Some(ISSUER.to_string()),
                algorithms: vec![Algorithm::RS256],
                ..Validation::default()
            };

            let jwks = keys.read().await;
            for jwk in &jwks.keys {
                if let Ok(decoding_key) = DecodingKey::from_jwk(jwk) {
                    if let Ok(decoded) = decode::<Claims>(&token, &decoding_key, &validation) {
                        return request::Outcome::Success(JwtToken(decoded.claims));
                    }
                }
            }
        }

        request::Outcome::Failure((Status::Unauthorized, ()))
    }
}

#[get("/protected")]
fn protected_route(_user: JwtToken) -> &'static str {
    "Access granted"
}

async fn fetch_jwks() -> Result<JwkSet, reqwest::Error> {
    let client = Client::new();
    let res = client.get(JWKS_URI).send().await?.json::<JwkSet>().await?;
    Ok(res)
}

#[rocket::main]
async fn main() {
    let jwks = fetch_jwks().await.expect("Failed to fetch JWKS");
    let jwks = Arc::new(RwLock::new(jwks));

    let _ = rocket::build()
        .manage(jwks)
        .mount("/", routes![protected_route])
        .launch()
        .await;
}
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 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 API in minutes.