ApiKee 1.0.8
dotnet add package ApiKee --version 1.0.8
NuGet\Install-Package ApiKee -Version 1.0.8
<PackageReference Include="ApiKee" Version="1.0.8" />
<PackageVersion Include="ApiKee" Version="1.0.8" />
<PackageReference Include="ApiKee" />
paket add ApiKee --version 1.0.8
#r "nuget: ApiKee, 1.0.8"
#:package ApiKee@1.0.8
#addin nuget:?package=ApiKee&version=1.0.8
#tool nuget:?package=ApiKee&version=1.0.8
ApiKee .NET
ApiKee is a lightweight and easy-to-use .NET library for managing and securing your API endpoints. Whether you want to validate API keys locally or via a server, ApiKee ensures smooth integration with minimal configuration. This library is also compatible with Swagger, providing additional security to your API documentation.
Table of Contents
- Concept Overview
- Getting Started
- Usage
- Protecting Swagger
- Server Validation Options
- Contribution
- License
Concept Overview
ApiKee is built around the idea of API key-based security, providing two main ways to validate requests:
Local Validation:
- Check requests locally using a predefined key.
- The key can be stored in:
- App settings.
- Environment variables.
- A database.
Server Validation:
- Validate requests by sending them to a central server.
- Supports:
- Your own custom server.
- ApiKee Premium Server (with a dashboard and analytics at apikee.com).
Getting Started
Install the
ApiKee
NuGet package:dotnet add package ApiKee
Add
ApiKee
to yourProgram.cs
orStartup.cs
.Protect your controllers or endpoints by adding the
[ApiKee]
attribute.
Usage
1. Local Validation
Local validation checks the API key locally without involving any external server.
How It Works
- A request must include the header
X-Api-Key
with a valid key. - The key is validated against a value stored in:
- App settings.
- An environment variable.
- A database.
Setup
Add API Key to App Settings:
{ "ApiKee": { "LocalKey": "your-local-api-key" } }
Configure in
Program.cs
:var builder = WebApplication.CreateBuilder(args); // Load configuration var apiKeeSection = builder.Configuration.GetSection("ApiKee"); var apiKeeConfig = new ApiKeeConfig { LocalKey = apiKeeSection["LocalKey"] }; ApiKee.Extensions.ApiKeeConfiguration.ConfigureApiKee(apiKeeConfig); // Add services builder.Services.AddControllers(); builder.Services.AddApiKee(); builder.Services.AddApiKeeSwagger(); var app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(); app.UseApiKee(); app.MapControllers(); app.Run();
Protect an Endpoint:
[HttpGet] [ApiKee] public IActionResult GetSecureData() { return Ok("This is a secure endpoint"); }
Key Alternatives:
- Use an environment variable:
LocalKey = Environment.GetEnvironmentVariable("APIKEE_LOCAL_KEY")
- Retrieve the key from a database:
LocalKey = GetApiKeyFromDatabase() // Implement your logic
- Use an environment variable:
2. Server Validation
Server validation sends request details to a central server for validation. This is ideal for dynamic or centralized API key management.
How It Works
- A request must include the header
X-Api-Key
with a valid key. - Details like the
EndpointId
,ProjectId
, andEnvironment
are sent to the server for validation.
Setup
Add Server Configuration to App Settings:
{ "ApiKee": { "ServerKey": "your-server-key", "ProjectId": "your-project-id", "Environment": "staging", "ServerUrl": "https://premium.apikee.com" } }
Configure in
Program.cs
:var builder = WebApplication.CreateBuilder(args); // Load configuration var apiKeeSection = builder.Configuration.GetSection("ApiKee"); var apiKeeConfig = new ApiKeeConfig { ServerKey = apiKeeSection["ServerKey"], ProjectId = apiKeeSection["ProjectId"], Environment = apiKeeSection["Environment"], ServerUrl = apiKeeSection["ServerUrl"] }; ApiKee.Extensions.ApiKeeConfiguration.ConfigureApiKee(apiKeeConfig); // Add services builder.Services.AddControllers(); builder.Services.AddApiKee(); builder.Services.AddApiKeeSwagger(); var app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(); app.UseApiKee(); app.MapControllers(); app.Run();
Protect an Endpoint:
[HttpGet] [ApiKee("secure-endpoint-id")] public IActionResult GetSecureData() { return Ok("This is a server-validated secure endpoint"); }
Protecting Swagger
The [ApiKee]
attribute automatically protects your Swagger documentation. Requests to /swagger
will require a valid API key.
To use:
Add the middleware in
Program.cs
:app.UseApiKee();
Provide the API key in Swagger UI:
- Open Swagger at
/swagger
. - Click Authorize.
- Add the
X-Api-Key
header with your API key.
- Open Swagger at
Server Validation Options
1. Build Your Own Server
If you want to build your own server, here’s the expected API structure:
- POST /validate:
- Request body:
{ "EndpointId": "secure-endpoint-id", "ApiKey": "provided-api-key", "ProjectId": "your-project-id", "Environment": "staging" }
- Response:
{ "isValid": true, "message": "Validation successful" }
- Request body:
2. Use ApiKee Premium Server
Leverage the ApiKee Premium Server at apikee.com to simplify server-side validation. Benefits include:
- A centralized API key management dashboard.
- Request analytics and monitoring.
- Support for multiple projects and environments.
Contribution
Contributions are welcome! If you’d like to improve ApiKee, follow these steps:
- Fork the Repository: Start by forking the ApiKee repository on GitHub.
- Create a Branch:
git checkout -b feature/your-feature-name
- Make Changes: Implement your changes and add appropriate tests.
- Test Your Code: Ensure all tests pass:
dotnet test
- Submit a Pull Request: Push your branch and submit a pull request to the main repository.
Ideas for Contribution
- Add new validation methods or sources.
- Improve the documentation or examples.
- Enhance integration with third-party services.
For detailed contribution guidelines, visit the ApiKee GitHub repository. Thank you for helping make ApiKee better!
License
The ApiKee library is released under an open-source license. You are free to use, modify, and distribute the library in compliance with the licensing terms.
- Note: The open-source license applies to the library only.
- For server-side validation, you have two options:
- Build your own server: You can implement your own validation server by adhering to the ApiKee specifications. See the API references for more details.
- Use the ApiKee Premium Server: ApiKee provides a ready-to-use server and dashboard at apikee.com, which includes additional features like analytics and centralized key management. This is a paid service.
For the full license text, visit the ApiKee GitHub repository.
ApiKee is a powerful and easy-to-use library for securing your API endpoints. Whether you manage API keys locally or prefer a centralized server solution, ApiKee ensures a smooth experience for developers.
For more details or to explore premium features, visit apikee.com.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Swashbuckle.AspNetCore (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.