Minimal API InstantAPIs

Simple CRUD API from Jeffrey Fritz. It supports DbContext and an API based on JSON file. The JSON file is a good alternative to JSON Server as it will not pull down 100s of node modules that I actually dont understand.

The value of this Instant APIs is I can bring up my BFF in minutes and focus my time on building my front end application. The contract often changes as we build out the front end so if the BFF is flexible its a huge win.

See Jeffrey Fritz - Build Web APIs Instantly for your Web Application with ASP.NET Core 6

Another alternative package is Fast Endpoints (package) which would be well worth checking out.

Setup

  1. Create the project and install Fritz.InstantAPIs
1
2
3
4
dotnet new web -o DemoApi -f net6.0
cd DemoApi

dotnet add package Fritz.InstantAPIs --prerelease
  1. Create the json file, example db.json. At the time of writing this guids are not supported as the primary key #sad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"products": [
{
"id": 1,
"name": "pizza"
},
{
"id": 2,
"name": "pineapple pizza"
}
],
"customers": [
{
"id": 1,
"name": "pizza"
}
]
}
  1. Update Program.cs as follows
1
2
3
4
5
6
7
8
using Mock;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseJsonRoutes(o => o.SetFilename("db.json"));

app.Run();
  1. Run the app
1
dotnet run

Postman requests

Postman is the most popular API platform tool but you can use anything you like such as SoapUI.

You can then run postman requests

GET

1
GET https://localhost:7203/products

response 200

1
2
3
4
5
6
7
8
9
10
[
{
"id": 1,
"name": "pizza"
},
{
"id": 2,
"name": "pineapple pizza"
}
]

POST

1
2
3
4
5
6
POST https://localhost:7203/products

BODY
{
"name": "foo"
}

response 200

1
2
3
{
"name": "foo"
}

GET {id}

1
GET https://localhost:7203/products/5

response 200

1
2
3
4
{
"name": "foo",
"Id": 5
}

DELETE {id}

1
DELETE https://localhost:7239/customers/1

response 200

1
OK

Swagger

You can then also add swagger support, this would probably be more useful if you are using the Dbcontext and plan to use the API for more than just local development of your front end application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using Mock;

var builder = WebApplication.CreateBuilder(args);

//swag!
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseJsonRoutes(o => o.SetFilename("db.json"));

//sawg!
app.UseSwagger();
app.UseSwaggerUI();

app.Run();