{"id":15011634,"url":"https://github.com/csharpfritz/instantapis","last_synced_at":"2025-04-09T04:04:40.857Z","repository":{"id":39624529,"uuid":"460245043","full_name":"csharpfritz/InstantAPIs","owner":"csharpfritz","description":"A library that generates Minimal API endpoints for an Entity Framework context.  ","archived":false,"fork":false,"pushed_at":"2022-10-15T11:31:53.000Z","size":467,"stargazers_count":452,"open_issues_count":22,"forks_count":58,"subscribers_count":27,"default_branch":"main","last_synced_at":"2025-04-09T04:04:31.071Z","etag":null,"topics":["asp-net-core","csharp","dotnet6","nuget-package","webapi"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csharpfritz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE-OF-CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-17T01:55:33.000Z","updated_at":"2025-03-02T04:59:31.000Z","dependencies_parsed_at":"2022-07-10T14:00:59.161Z","dependency_job_id":null,"html_url":"https://github.com/csharpfritz/InstantAPIs","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csharpfritz%2FInstantAPIs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csharpfritz%2FInstantAPIs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csharpfritz%2FInstantAPIs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csharpfritz%2FInstantAPIs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csharpfritz","download_url":"https://codeload.github.com/csharpfritz/InstantAPIs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974715,"owners_count":21026742,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","csharp","dotnet6","nuget-package","webapi"],"created_at":"2024-09-24T19:41:22.287Z","updated_at":"2025-04-09T04:04:40.829Z","avatar_url":"https://github.com/csharpfritz.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InstantAPIs\n\n[![Nuget](https://img.shields.io/nuget/v/InstantAPIs)](https://www.nuget.org/packages/InstantAPIs/)\n[![Instant APIs Documentation](https://img.shields.io/badge/docs-ready!-blue)](https://csharpfritz.github.io/InstantAPIs)\n![GitHub last commit](https://img.shields.io/github/last-commit/csharpfritz/InstantAPIs)\n![GitHub contributors](https://img.shields.io/github/contributors/csharpfritz/InstantAPIs)\n\nThis article contains two different ways to get an instant API:\n\n- An API based on a `DbContext`, it will generate the routes it needs given a database class. There are two implementations of this: Reflection and a source generator.\n- An API based on a JSON file.\n\n## DbContext based API\n\nA proof-of-concept library that generates Minimal API endpoints for an Entity Framework context. Right now, there are two implementations of this: one that uses Reflection (this is the one currently in the NuGet package), the other uses a source generator. Let's see how both of them work.\n\nFor a given Entity Framework context, `MyContext`\n\n```csharp\npublic class MyContext : DbContext \n{\n    public MyContext(DbContextOptions\u003cMyContext\u003e options) : base(options) {}\n\n    public DbSet\u003cContact\u003e Contacts =\u003e Set\u003cContact\u003e();\n    public DbSet\u003cAddress\u003e Addresses =\u003e Set\u003cAddress\u003e();\n\n}\n```\n\nWe can generate all of the standard CRUD API endpoints with the Reflection approach using this syntax in `Program.cs`\n\n```csharp\nvar builder = WebApplication.CreateBuilder(args);\n\nbuilder.Services.AddSqlite\u003cMyContext\u003e(\"Data Source=contacts.db\");\n\nvar app = builder.Build();\n\napp.MapInstantAPIs\u003cMyContext\u003e();\n\napp.Run();\n```\n\nNow we can navigate to `/api/Contacts` and see all of the Contacts in the database.  We can filter for a specific Contact by navigating to `/api/Contacts/1` to get just the first contact returned.  We can also post to `/api/Contacts` and add a new Contact to the database. Since there are multiple `DbSet`, you can make the same calls to `/api/Addresses`.\n\nYou can also customize the APIs if you want\n```csharp\napp.MapInstantAPIs\u003cMyContext\u003e(config =\u003e\n{\n\tconfig.IncludeTable(db =\u003e db.Contacts, ApiMethodsToGenerate.All, \"addressBook\");\n});\n```\n\nThis specifies that the all of the CRUD methods should be created for the `Contacts` table, and it prepends the routes with `addressBook`.\n\nThe source generator approach has an example in the `WorkingApi.Generators` project (at the moment a NuGet package hasn't been created for this implementation). You specify which `DbContext` classes you want to map with the `InstantAPIsForDbContextAttribute`, For each context, an extension method named `Map{DbContextName}ToAPIs` is created. The end result is similar to the Reflection approach:\n\n```csharp\n[assembly: InstantAPIsForDbContext(typeof(MyContext))]\n\nvar builder = WebApplication.CreateBuilder(args);\n\nbuilder.Services.AddSqlite\u003cMyContext\u003e(\"Data Source=contacts.db\");\n\nvar app = builder.Build();\n\napp.MapMyContextToAPIs();\n\napp.Run();\n```\n\nYou can also do customization as well\n```csharp\napp.MapMyContextToAPIs(options =\u003e\n\toptions.Include(MyContext.Contacts, \"addressBook\", ApisToGenerate.Get));\n```\n\nFeel free to try both approaches and let Fritz know if you have any issues with either one of them. The intent is to keep feature parity between the two for the forseable future.\n\n### Demo\n\nCheck out Fritz giving a demo, showing the advantage of InstantAPIs on YouTube: https://youtu.be/vCSWXAOEpBo\n\n\n\n## A JSON based API\n\nAn API will be generated based on JSON file, for now it needs to be named *mock.json*.\n\nA typical content in *mock.json* looks like so:\n\n```json\n{\n  \"products\" : [{\n    \"id\": 1,\n    \"name\": \"pizza\"\n  }, {\n    \"id\": 2,\n    \"name\": \"pineapple pizza\"\n  }, {\n    \"id\": 3,\n    \"name\": \"meat pizza\"\n  }],\n  \"customers\" : [{\n    \"id\": 1,\n    \"name\": \"customer1\"\n  }]\n  \n}\n```\n\nThe above JSON will create the following routes:\n\n|HTTP Verb  |Endpoint  |\n|---------|---------|\n|  GET   | /products        |\n|  GET   | /products/{id}        |\n|  POST   | /products        |\n|  DELETE   | /products/{id}        |\n|  GET   | /customers        |\n|  GET   | /customers/{id}        |\n|  POST   | /customers        |\n|  DELETE   | /customers/{id}        |\n\n### Demo\n\nTo use this, do the following:\n\n1. Create a new minimal API, if you don't already have one:\n\n   ```bash\n   dotnet new web -o DemoApi -f net6.0\n   cd DemoApi \n   ```\n\n1. Add the NuGet package for [InstantAPIs](https://www.nuget.org/packages/InstantAPIs/):\n\n   ```bash\n   dotnet add package InstantAPIs --prerelease\n   ```\n\n1. In *Program.cs*, add the following namespace:\n\n   ```csharp\n   using Mock;\n   ```\n\n1. Create a file *mock.json* and give it for example the following content:\n\n   ```json\n   {\n      \"products\" : [{\n        \"id\": 1,\n        \"name\": \"pizza\"\n      }]\n    }\n   ```\n\n1. Now add the following code for the routes to be created:\n\n   ```csharp\n   app.UseJsonRoutes();\n   ```\n\nHere's an example program:\n\n```csharp\nusing Mock;\n\nvar builder = WebApplication.CreateBuilder(args);\nvar app = builder.Build();\n\napp.MapGet(\"/\", () =\u003e \"Hello World!\");\napp.UseJsonRoutes();\napp.Run();\n```\n\n### Coming features\n\nSupport for:\n\n- [query parameters](https://github.com/csharpfritz/InstantAPIs/issues/40)\n- [PUT](https://github.com/csharpfritz/InstantAPIs/issues/39)\n\n## Community\n\nThis project is covered by a [code of conduct](https://github.com/csharpfritz/InstantAPIs/blob/main/CODE-OF-CONDUCT.md) that all contributors must abide by.  [Contributions are welcome and encouraged.](https://github.com/csharpfritz/InstantAPIs/blob/main/CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsharpfritz%2Finstantapis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsharpfritz%2Finstantapis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsharpfritz%2Finstantapis/lists"}