|
| 1 | +## API v. API operation v. API endpoint |
| 2 | + |
| 3 | +The following sections explain the differences between an API, an API endpoint, and an API operation in the context of ASP.NET Core and OpenAPI documentation. |
| 4 | + |
| 5 | +### API (Application Programming Interface) |
| 6 | + |
| 7 | +An API is a set of rules and protocols for building and interacting with software applications. It defines how different software components should communicate. In general web development, "API" typically refers to a web service that exposes functionality over HTTP. |
| 8 | + |
| 9 | +In ASP.NET Core, an API is usually built using controllers or minimal APIs, which handle incoming HTTP requests and return responses. |
| 10 | + |
| 11 | +ASP.NET Core's internal naming conventions sometimes use "API" differently. For instance, in [API Explorer](/dotnet/api/microsoft.aspnetcore.mvc.apiexplorer), an "ApiDescription" actually represents an [API Operation](#api-operation) rather than the full API service. This distinction reflects internal naming conventions and sometimes differ from the broader definition used here. |
| 12 | + |
| 13 | +See [Introduction to the ApiExplorer in ASP.NET Core](https://andrewlock.net/introduction-to-the-apiexplorer-in-asp-net-core/) for more information on API Explorer. |
| 14 | + |
| 15 | +### API Operation |
| 16 | + |
| 17 | +An API operation represents a specific action or capability that an API provides. In ASP.NET Core, this corresponds to: |
| 18 | + |
| 19 | +* Controller action methods in MVC-style APIs |
| 20 | +* Route handlers in Minimal APIs |
| 21 | + |
| 22 | +Each operation is defined by its HTTP method (`GET`, `POST`, `PUT`, etc.), path, parameters, and responses. |
| 23 | + |
| 24 | +### API Endpoint |
| 25 | + |
| 26 | +An API endpoint is a specific URL: |
| 27 | + |
| 28 | +* That represents a specific resource or functionality exposed by the API. |
| 29 | +* Provides the exact address that a client needs to send an HTTP request to in order to interact with a particular API operation. |
| 30 | + |
| 31 | +An endpoint is a combination of the API's base URL and a specific path to the desired resource, along with the supported HTTP methods: |
| 32 | + |
| 33 | +* For controller-based APIs, endpoints combine the route template with controller and action. |
| 34 | +* For Minimal APIs, endpoints are explicitly defined with `app.MapGet()`, `app.MapPost()`, etc. |
| 35 | + |
| 36 | +For example, the `api/products/{id}` endpoint that supports the following operations: |
| 37 | + |
| 38 | +* `GET /api/products/{id}` |
| 39 | +* `PUT /api/products/{id}` |
| 40 | +* `PATCH /api/products/{id}` |
| 41 | +* `Delete /api/products/{id}` |
| 42 | +* `HEAD /api/products/{id}` |
| 43 | + |
| 44 | +Endpoints often include query parameters, for example, `GET /api/products?category=electronics&sort=price` |
| 45 | + |
| 46 | +### OpenAPI Documentation |
| 47 | + |
| 48 | +In the context of OpenAPI, the documentation describes the API as a whole, including all its endpoints and operations. OpenAPI provides a structured way to document APIs, making it easier for developers to understand how to interact with them. |
| 49 | + |
| 50 | +API Operations are the primary focus of OpenAPI documentation. The [OpenAPI specification](https://spec.openapis.org/oas/latest.html) organizes documentation by operations, which are grouped by paths (endpoints). Each operation is described with details such as parameters, request bodies, responses, and more. This structured format allows tools to generate client libraries, server stubs, and interactive documentation automatically. |
| 51 | + |
| 52 | +In an OpenAPI document: |
| 53 | + |
| 54 | +* The entire document describes the API as a whole |
| 55 | +* Each path item (like `/api/products/{id}`) represents an endpoint |
| 56 | +* Under each path, the HTTP methods (`GET`, `POST`, `PUT`, etc.) define the operations |
| 57 | +* Each operation contains details about parameters, request body, responses, etc. |
| 58 | + |
| 59 | +Example in OpenAPI JSON format: |
| 60 | + |
| 61 | +```JSON |
| 62 | +json{ |
| 63 | + "paths": { |
| 64 | + "/api/products/{id}": { // This is the endpoint |
| 65 | + "get": { // This is the operation |
| 66 | + "summary": "Get a product by ID", |
| 67 | + "parameters": [...], |
| 68 | + "responses": {...} |
| 69 | + }, |
| 70 | + "put": { // Another operation on the same endpoint |
| 71 | + "summary": "Update a product", |
| 72 | + "parameters": [...], |
| 73 | + "responses": {...} |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## API, API operation, and API endpoint comparison |
| 81 | + |
| 82 | +The following table summarizes the differences between an API, an API operation, and an API endpoint: |
| 83 | + |
| 84 | +| Concept | API Operation | API Endpoint | |
| 85 | +|-----------------|----------------------------------------------------|--------------------------------------------------| |
| 86 | +| **Definition** | A logical description of an API action: method + path + behavior | The actual configured HTTP route that listens for requests | |
| 87 | +| **Level** | Conceptual, what action can happen | Concrete, what URL and method are matched | |
| 88 | +| **Tied to** | OpenAPI API design/specification | ASP.NET Core routing at runtime | |
| 89 | +| **Describes** | What the API does for example, "create product" | Where and how to call it, for example, `POST https://localhost:7099/api/products`, `POST https://contoso.com/api/products` | |
| 90 | +| **In ASP.NET Core** | Controller actions or Minimal API methods, before routing resolves | Endpoint objects resolved at runtime | |
0 commit comments