SkillAgentSearch skills...

JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.

Install / Use

/learn @json-api-dotnet/JsonApiDotNetCore
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<a href="https://www.jsonapi.net"><img src="docs/home/assets/img/logo.svg" style="height: 345px; width: 345px"/></a>

JsonApiDotNetCore

Build Coverage NuGet GitHub License FIRST-TIMERS

A framework for building JSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core. Includes support for the Atomic Operations extension.

The ultimate goal of this library is to eliminate as much boilerplate as possible by offering out-of-the-box features, such as sorting, filtering, pagination, sparse fieldset selection, and side-loading related resources. You just need to focus on defining the resources and implementing your custom business logic. This library has been designed around dependency injection, making extensibility incredibly easy.

[!NOTE] OpenAPI support is now available, currently in preview. Give it a try!

Getting started

The following steps describe how to create a JSON:API project.

  1. Create a new ASP.NET Core Web API project:

    dotnet new webapi --no-openapi --use-controllers --name ExampleJsonApi
    cd ExampleJsonApi
    
  2. Install the JsonApiDotNetCore package, along with your preferred Entity Framework Core provider:

    dotnet add package JsonApiDotNetCore
    dotnet add package Microsoft.EntityFrameworkCore.Sqlite
    
  3. Declare your entities, annotated with JsonApiDotNetCore attributes:

    [Resource]
    public class Person : Identifiable<long>
    {
        [Attr] public string? FirstName { get; set; }
        [Attr] public string LastName { get; set; } = null!;
        [HasMany] public ISet<Person> Children { get; set; } = new HashSet<Person>();
    }
    
  4. Define your DbContext, seeding the database with sample data:

    public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
    {
        public DbSet<Person> People => Set<Person>();
    
        protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {
            builder.UseSqlite("Data Source=SampleDb.db");
            builder.UseAsyncSeeding(async (dbContext, _, cancellationToken) =>
            {
                dbContext.Set<Person>().Add(new Person
                {
                    FirstName = "John",
                    LastName = "Doe",
                    Children =
                    {
                        new Person
                        {
                            FirstName = "Baby",
                            LastName = "Doe"
                        }
                    }
                });
                await dbContext.SaveChangesAsync(cancellationToken);
            });
        }
    }
    
  5. Configure Entity Framework Core and JsonApiDotNetCore in Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddDbContext<AppDbContext>();
    builder.Services.AddJsonApi<AppDbContext>(options =>
    {
        options.UseRelativeLinks = true;
        options.IncludeTotalResourceCount = true;
    });
    
    var app = builder.Build();
    app.UseRouting();
    app.UseJsonApi();
    app.MapControllers();
    await CreateDatabaseAsync(app.Services);
    app.Run();
    
    static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
    {
        await using var scope = serviceProvider.CreateAsyncScope();
        var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
        await dbContext.Database.EnsureDeletedAsync();
        await dbContext.Database.EnsureCreatedAsync();
    }
    
  6. Start your API

    dotnet run
    
  7. Send a GET request to retrieve data:

    GET http://localhost:5000/people?filter=equals(firstName,'John')&include=children HTTP/1.1
    
    <details> <summary>Expand to view the JSON response</summary>
    {
      "links": {
        "self": "/people?filter=equals(firstName,%27John%27)&include=children",
        "first": "/people?filter=equals(firstName,%27John%27)&include=children",
        "last": "/people?filter=equals(firstName,%27John%27)&include=children"
      },
      "data": [
        {
          "type": "people",
          "id": "1",
          "attributes": {
            "firstName": "John",
            "lastName": "Doe"
          },
          "relationships": {
            "children": {
              "links": {
                "self": "/people/1/relationships/children",
                "related": "/people/1/children"
              },
              "data": [
                {
                  "type": "people",
                  "id": "2"
                }
              ]
            }
          },
          "links": {
            "self": "/people/1"
          }
        }
      ],
      "included": [
        {
          "type": "people",
          "id": "2",
          "attributes": {
            "firstName": "Baby",
            "lastName": "Doe"
          },
          "relationships": {
            "children": {
              "links": {
                "self": "/people/2/relationships/children",
                "related": "/people/2/children"
              }
            }
          },
          "links": {
            "self": "/people/2"
          }
        }
      ],
      "meta": {
        "total": 1
      }
    }
    
</details>

Learn more

The following links explain what this project provides, why it exists, and how you can use it.

About

Official documentation

Samples

  • The examples directory provides ready-to-run sample API projects, which are documented here.
  • The integration tests directory covers many advanced use cases, which are documented here. This includes topics such as batching, multi-tenancy, authorization, soft-deletion, obfuscated IDs, resource inheritance, alternate routing, custom metadata, error handling and logging.
  • The Ember.js Todo List App showcases a JsonApiDotNetCore API and an Ember.js client with token authentication.

Related projects

Compatibility

The following chart should help you pick the best version, based on your environment. See also our versioning policy.

| .NET | Entity Framework Core | JsonApiDotNetCore | Status | | -------- | --------------------- | ----------------- | -------------- | | 10 | 10 | 5.10.0+ | Stable | | 9 | 9 | 5.5.0+ | Stable | | 8 | 8, 9 | 5.5.0+ | Stable | | 7 | 7 | 5.0.3 - 5.6.0 | Out of support | | 6 | 7 | 5.0.3 - 5.6.0 | Out of support | | 6 | 6 | 5.0.0 - 5.6.0 | Out of support | | 5 | 5 | 4.x | Out of support | | Core 3.1 | 3.1, 5 | 4.x | Out of support | | Core 2.x | 2.x | 3.x | Out of support |

Trying out the latest build

After each commit to the master branch, a new pre-release NuGet package is automatically published to feedz.io. To try it out, follow the steps below:

  1. Create a nuget.config file in the same directory as your .sln file, with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="json-api-dotnet" value="https://f.feedz.io/json-api-dotnet/jsonapidotnetcore/nuget/index.json" />
        <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    
  2. In your IDE, browse the list of packages from the json-api-dotnet feed. Make sure pre-release packages are included in the list.

Contributing

Have a question, found a bug or want to submit code changes? See our contributing guidelines.

Build from source

To build the code from this repository locally, run:

dotnet build

Running tests locally requires access to a PostgreSQL database. If you have d

Related Skills

View on GitHub
GitHub Stars716
CategoryDevelopment
Updated3d ago
Forks162

Languages

C#

Security Score

100/100

Audited on Mar 20, 2026

No findings