SwiftApi 8.0.7

dotnet add package SwiftApi --version 8.0.7
                    
NuGet\Install-Package SwiftApi -Version 8.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SwiftApi" Version="8.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SwiftApi" Version="8.0.7" />
                    
Directory.Packages.props
<PackageReference Include="SwiftApi" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SwiftApi --version 8.0.7
                    
#r "nuget: SwiftApi, 8.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SwiftApi@8.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SwiftApi&version=8.0.7
                    
Install as a Cake Addin
#tool nuget:?package=SwiftApi&version=8.0.7
                    
Install as a Cake Tool

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 GET
  • PostAction โ†’ HTTP POST
  • PutAction โ†’ HTTP PUT
  • DeleteAction โ†’ HTTP DELETE

๐Ÿ› ๏ธ Getting Started

dotnet add package SwiftApi

๐Ÿ”ง Basic Interface Usage

  1. 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);
}
  1. Define your schema:
[SchemaModel]
public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    [Required]
    public string Email { get; set; }
}
  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.