Zuplo

API Key Authentication

Secure Your NancyFX API with API Keys

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

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

C#
using System;
using System.Security.Cryptography;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;

public class SecureModule : NancyModule
{
    public SecureModule()
    {
        // Use the middleware to secure this route
        Before += ValidateSharedSecret;

        Get("/protected", _ =>
        {
            return Response.AsJson(new { message = "Access granted" });
        });
    }

    private Response ValidateSharedSecret(NancyContext context)
    {
        var secretHeader = context.Request.Headers["x-shared-secret"];
        var expectedSecret = Environment.GetEnvironmentVariable("SHARED_SECRET");

        if (string.IsNullOrWhiteSpace(expectedSecret))
        {
            return HttpStatusCode.InternalServerError.WithReasonPhrase("Server configuration error");
        }

        if (string.IsNullOrWhiteSpace(secretHeader))
        {
            return HttpStatusCode.Unauthorized.WithReasonPhrase("No secret provided");
        }

        var secret = secretHeader.Single();

        if (!SecureCompare(secret, expectedSecret))
        {
            return HttpStatusCode.Unauthorized.WithReasonPhrase("Invalid secret");
        }

        return null;
    }

    private bool SecureCompare(string a, string b)
    {
        if (a.Length != b.Length)
            return false;

        using (var hmac = new HMACSHA256())
        {
            var hashA = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a));
            var hashB = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(b));

            return CryptographicOperations.FixedTimeEquals(hashA, hashB);
        }
    }
}

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        // Apply other configurations here if needed
    }
}
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 NancyFX 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 NancyFX API in minutes.