SwiftApi 8.0.7
dotnet add package SwiftApi --version 8.0.7
NuGet\Install-Package SwiftApi -Version 8.0.7
<PackageReference Include="SwiftApi" Version="8.0.7" />
<PackageVersion Include="SwiftApi" Version="8.0.7" />
<PackageReference Include="SwiftApi" />
paket add SwiftApi --version 8.0.7
#r "nuget: SwiftApi, 8.0.7"
#:package SwiftApi@8.0.7
#addin nuget:?package=SwiftApi&version=8.0.7
#tool nuget:?package=SwiftApi&version=8.0.7
SwiftApi
SwiftApi is a powerful .NET class library that transforms your interfaces โ or even your model classes โ into fully functional API endpoints with zero controller boilerplate. Designed for speed, simplicity, and flexibility, SwiftApi enables you to build scalable APIs with minimal code and maximum control.
๐ Key Features
โ Zero Controllers
Automatically exposes your service interfaces or models as API endpoints โ no need to write a single controller.โ๏ธ Unlimited Endpoints
Add as many interfaces or model classes as you like โ SwiftApi handles the routing dynamically.๐ Built-in Security
Easily secure endpoints with support for various authentication schemes (Bearer, Basic, API Key, etc.).๐ Swagger Support
Built-in Swagger/OpenAPI integration for instant, interactive API documentation.๐งฉ Endpoint Management
Enable, disable, or configure individual endpoints via attributes or settings โ without touching controllers.๐พ Response Caching
Built-in support for caching GET responses for improved performance and scalability.๐งฑ Model-Based API Generation
Automatically generate basic CRUD endpoints directly from model classes.๐ฏ .NET 8+ Compatible
Built on the latest .NET standards with full support for .NET 8 and future versions.
โ Supported Actions
SwiftApi currently supports the following HTTP actions via method attributes:
GetAction
โ HTTP GETPostAction
โ HTTP POSTPutAction
โ HTTP PUTDeleteAction
โ HTTP DELETE
๐ ๏ธ Getting Started
dotnet add package SwiftApi
๐ง Basic Interface Usage
- Define your interface:
[EndPoint("users")]
public interface IUserService
{
[GetAction]
Task<User?> GetUserByIdAsync([QueryParam] Guid id);
[GetAction("get-users")]
Task<List<User>> GetUsersAsync();
[PostAction("create-users")]
Task CreateUserAsync([BodyParam] User user);
[PutAction("update-users")]
Task UpdateUserAsync([RouteParam] Guid id, [BodyParam] User user);
[DeleteAction("delete-users")]
Task DeleteUserAsync([RouteParam] Guid id);
}
- Define your schema:
[SchemaModel]
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
[Required]
public string Email { get; set; }
}
- Register SwiftApi in
Program.cs
:
builder.Services.AddSwiftAPI();
app.MapSwiftAPI();
๐งฑ Model-Based Endpoint Generation (Interface Binding Required)
SwiftApi supports model-based endpoint generation by specifying the interface and its implementation directly on the model.
๐ Example with Generic Interface
[ModelEndPoint(typeof(IGenericService<User>), typeof(GenericService<User>))]
[SecureEndpoint]
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
[Required]
public string Email { get; set; }
}
๐ Example with Specific Interface
[ModelEndPoint(typeof(IPaymentService), typeof(PaymentService))]
public class Payment
{
public Guid Id { get; set; }
public string? Description { get; set; }
public decimal Amount { get; set; }
public DateTime PaymentDate { get; set; }
public string? Status { get; set; }
public string? PaymentMethod { get; set; }
public string? TransactionId { get; set; }
public string? Currency { get; set; }
}
Each model will generate full CRUD endpoints via the specified service interface and implementation.
๐ Securing Endpoints
Use attributes or configuration to secure endpoints per-method or globally.
๐ Securing Interface EndPoints
[EndPoint("users")]
[SecureEndpoint(role: "Admin,Manager")]
public interface IUserService
{
[GetAction("get-user-by-id")]
Task<User?> GetUserByIdAsync([QueryParam] Guid id);
[GetAction("get-users")]
[OpenAction] // Allows unauthenticated access
Task<List<User>> GetUsersAsync();
[PostAction("create-users")]
Task CreateUserAsync([BodyParam] User user);
[PutAction("update-users")]
Task UpdateUserAsync([RouteParam] Guid id, [BodyParam] User user);
[DeleteAction("delete-users")]
[SecureAction(policy: "delete")] //optional: specify the policy of the action
Task DeleteUserAsync([RouteParam] Guid id);
}
๐ Securing Model EndPoints
[ModelEndPoint(typeof(IGenericService<User>),
typeof(GenericService<User>))]
[SecureEndpoint(role: "Admin,Manager")]
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
[Required]
public string Email { get; set; }
}
Update your SwiftApi registration:
builder.Services.AddSwiftAPI(o =>
{
o.AuthScheme = AuthScheme.Basic; // Options: Bearer, Basic, ApiKey
});
โก Response Caching
Enable response caching for GET methods using the enableCache and cacheDuration variables in the [GetAction()] attribute.
[GetAction(enableCache: true, cacheDuration: 5)] // default false, default duration 1 min
Task<List<User>> GetUsersAsync();
You can configure global cache behavior in Program.cs
if needed.
๐งช Swagger Integration
Once your app is running, visit:
/swagger
To see your auto-generated, interactive documentation.
๐ License
This project is licensed under the MIT License.
๐โโ๏ธ Author
Nawaf AL-Maqbali
๐ง LinkedIn
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.OpenApi (>= 8.0.18)
- Swashbuckle.AspNetCore (>= 9.0.3)
- SwiftApi.Core (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.