English
Topic 12. HTTP API with Minimal APIs: endpoint groups, Problem Details, JWT authentication, the composition root and OpenAPI
Goal: expose the business layer over HTTP: turn results into status codes and RFC 9457 Problem Details with stable codes, map the endpoints of authentication, profiles, users, chats and messages, protect them with JWT bearer tokens and a refresh cookie, compose the application in Program.cs, describe it with OpenAPI and Scalar, and test it through the real HTTP pipeline.
Where we are
Module 4 builds the web layer (Fig. 12.1). After this topic Chatex is an API you can call from the browser, from Scalar, from .http files and from tests.
Fig. 12.1. The web layer on top of the business layer
- Starting point: topic 11 — the business layer is complete; 60 tests pass. Chatex.Api is still the "Hello World" of topic 2.
- At the end of the topic the API runs on
http://localhost:5214with an interactive reference at/scalar, and the new test projectChatex.Api.Testschecks 16 scenarios over HTTP.
Steps
- From results to HTTP — Minimal APIs · status codes and Problem Details · the signed-in user · request and response contracts
- Authentication endpoints — the refresh token cookie · register, login, refresh, logout
- Endpoint groups — profiles, users, chats and messages · route parameters · typed results
- The composition root — packages and settings ·
AddApi· JWT bearer · the middleware pipeline · OpenAPI and Scalar · running the API - Testing over HTTP —
WebApplicationFactory· a client per user · HTTP assertions · the endpoint tests - Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 3, Backend in C# (slides "The composition root: Program.cs", "From Result to HTTP", "Authentication with rotating tokens")
Program.cs,DependencyInjection.cs,Endpoints/,Infrastructure/Docs/HttpApi.md– routes, tokens, paging, limits and error codes- The C# course topic REST APIs