5 Creating a JSON API with minimal APIs

 

This chapter covers

  • Creating a minimal API application to return JSON to clients
  • Generating responses with IResult
  • Using filters to perform common actions like validation
  • Organizing your APIs with route groups

So far in this book you’ve seen several examples of minimal API applications that return simple Hello World responses. These are great for getting started, but you can also use minimal APIs to build full-featured HTTP API applications. In this chapter you’ll learn about HTTP APIs, how they differ from a server-rendered application, and when to use them.

In section 5.2 you’ll start by expanding on the minimal API applications you’ve already seen. We’ll explore some basic routing concepts and show how values can be automatically extracted from the URL. You’ll then learn how to handle additional HTTP verbs like POST and PUT, and explore the various ways to define your APIs.

In section 5.3 you’ll learn about the different return types you can use with minimal APIs. You’ll learn to use the Results and TypedResults helper classes to easily create HTTP responses that use status codes like 201 Created and 404 Not Found. You’ll also learn how to follow web standards for describing your errors by using the built-in support for Problem Details.

5.1 What is an HTTP API and when should you use one?

5.2 Defining minimal API endpoints

5.2.1 Extracting values from the URL with routing

5.2.2 Mapping verbs to endpoints

5.2.3 Defining route handlers with functions

5.3 Generating responses with IResult

5.3.1 Returning status codes with Results and TypedResults

5.3.2 Returning useful errors with ProblemDetails

5.3.3 Converting all your responses to Problem Details

5.3.4 Returning other data types

5.4 Running common code with endpoint filters

5.4.1 Adding multiple filters to an endpoint

5.4.2 Filters or middleware: Which should you choose?

5.4.3 Generalizing your endpoint filters

5.4.4 Implementing the IEndpointFilter interface

5.5 Organizing your APIs with route groups

5.6 Summary