golden nugget online

Instant APIs In .NET

To create our first instant API we need to create a simple minimal API or web project, we can do this using the CLI. Navigate to a folder for this golden nugget online API and execute the command:

dotnet golden nugget online web

Then we need to install the NuGet libraries required for this project. We will use Entity Framework and Sqlite:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Sqlite

And for instant API we can use the library created by , Fritz.InstantAPIs.

dotnet add package Fritz.InstantAPIs --version 0.2.0

Now we are ready for coding. Since we will use EF we need to create a context and a model. In this case, we will MyContext and the model ToDoItem, it is possible to add it in the same Program.cs at the bottom. 

public class MyContext: DbContext {
    public MyContext(DbContextOptions < MyContext > options): base(options) {}
    public DbSet < ToDoItem > ToDoItems => Set < ToDoItem > ();
}
public class ToDoItem {
    public Guid Id {
        get;
        set;
    }
    public string Name {
        get;
        set;
    }
    public string Description {
        get;
        set;
    }
}

And now we can setup the context using Sqlite:

builder.Services.AddSqlite<MyContext>("Data Source=ToDo.db");

Now we are ready to configure our instant API we only need to add two lines in order to do this:

builder.Services.AddInstantAPIs(); //To include the services for Instant Apis
app.MapInstantAPIs<MyContext>(); //map all the endpoints for this context

So finally our program.cs will look like this:

using Fritz.InstantAPIs;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSqlite < MyContext > ("Data Source=ToDo.db");
builder.Services.AddInstantAPIs();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapInstantAPIs < MyContext > ();
app.Run();
public class MyContext: DbContext {
    public MyContext(DbContextOptions < MyContext > options): base(options) {}
    public DbSet < ToDoItem > ToDoItems => Set < ToDoItem > ();
}
public class ToDoItem {
    public Guid Id {
        get;
        set;
    }
    public string Name {
        get;
        set;
    }
    public string Description {
        get;
        set;
    }
}

At this point, we can execute the project and explore swagger to see all the endpoints generated by the library Fritz.InstantAPIs

Instant APIs in .NET

By default we will have the endpoint to get, getbyid, create, update and delete elements. This is a great way to create really quick APIs for proof of concept, demos and, try golden nugget online libraries and technologies.

We can also perform some configuration in the API, for example specify which methods we want to create for each model. For example:

app.MapInstantAPIs<MyContext>(config =>
{
	config.IncludeTable(db => db.ToDoItems, ApiMethodsToGenerate.Get, "ToDoItems");
});

We can execute the project again and access swager, and for this case we will have only the get methods.

With this knowledge, you are ready for creating your first Instant API and try this amazing tool. You can check out the official repository or this library:

You can find the code used in this article