API (Application Programming Interface)
An API is an interface through which one program uses the functions of another. Web APIs offer them over HTTP; in ASP.NET Core you build them with minimal APIs or controllers and describe them with OpenAPI.
An API (application programming interface) is the interface through which one program uses the functions of another without knowing its inner workings. On the web, the term usually means a service that offers data over HTTP, often as JSON.
What belongs to it
Microsoft describes an API as a set of rules for how software components talk to each other. For a web API, three terms go together:
- Endpoint: a specific address, such as
/api/contacts/{id}. - Operation: an action at this endpoint, defined by the HTTP method (
GET,POST,PUT,DELETE), the path, the parameters, and the responses. - Contract: the description of what a caller may send and what it gets back. The OpenAPI standard exists for this.
Web APIs in ASP.NET Core
ASP.NET Core knows two ways: minimal APIs and controllers. Microsoft recommends minimal APIs for new projects. The smallest example:
var app = WebApplication.Create(args);
app.MapGet("/hello/{name}", (string name) => $"Hello {name}!");
app.Run();
For the contract, ASP.NET Core uses the package Microsoft.AspNetCore.OpenApi to generate an OpenAPI document from the endpoints (AddOpenApi, MapOpenApi). A user interface such as Swagger UI is not part of it and is added when needed. Further building blocks around an API are database access, sign-in and permissions, and error responses.
An example from the Kladde
In K2, the Kladde gets a web API with endpoints for contacts and appointments. It returns JSON, validates input, reports errors as ProblemDetails, and describes itself with OpenAPI. The Blazor pages and the tests talk to this API; its business rules live in the core of the Kladde, not in the endpoints.
How it differs
- API and REST: REST is an architectural style for web APIs; not every API is REST.
- API and SDK: An SDK is a toolkit that makes an API convenient to call.
- API and user interface: An API is meant for programs, a user interface for people.
- The term is wider than the web: The methods of a class library form an API as well. This entry means the web API.
In the course
In K2, module 2.3, you build the API step by step: The first web API, Errors as ProblemDetails, and OpenAPI and API design. If you want to design interfaces in more depth, K2, module 2.12, has the lesson REST done right.
Further reading on the blog
Sources
- Microsoft Learn, OpenAPI support in ASP.NET Core API apps: https://learn.microsoft.com/aspnet/core/fundamentals/openapi/overview?view=aspnetcore-10.0
- Microsoft Learn, APIs overview (ASP.NET Core): https://learn.microsoft.com/aspnet/core/fundamentals/apis?view=aspnetcore-10.0