> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/extend-your-project/tutorials/creating-a-backoffice-api/documenting-your-controllers.md).

# Documenting Your Controllers

Documenting your API controllers using OpenAPI simplifies the creation of detailed and interactive API documentation. Adding the appropriate attributes automatically generates comprehensive information about routes, parameters, and response types. This will enhance the developer experience and ensure clarity and consistency in your API documentation.

## ApiExplorerSettings

With the `ApiExplorerSettings` attribute, we can put all our endpoints into a given group. This is a nice way of organizing our endpoints in the Swagger UI.

{% code title="MyItemApiController.cs" %}

```csharp
[ApiExplorerSettings(GroupName = "My item API")]
public class MyItemApiController : ManagementApiControllerBase
```

{% endcode %}

## ProducesResponseType Attribute

Use \[ProducesResponseType] to specify the possible responses for each action method. This helps generate accurate documentation for your API.\
For example, in the GetItem method:

{% code title="MyItemApiController.cs" %}

```csharp
[HttpGet("{id:guid}")]
[ProducesResponseType<MyItem>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public IActionResult GetItem(Guid id)
{
// Method implementation
}
```

{% endcode %}

Here, `[ProducesResponseType]` specifies that a 200 OK response will return a MyItem, and a 404 Not Found response will return a ProblemDetails.

## Example Documentation for Each Controller Method

To get an idea of how to document each controller method, below are some examples of how to document each operation for an API controller.\
The controller is from the [Creating your own API article](/umbraco-cms/extend-your-project/tutorials/creating-a-backoffice-api.md)

### GetAllItems

{% code title="MyItemApiController.cs" %}

```csharp

[HttpGet]
[ProducesResponseType<PagedViewModel<MyItem>>(StatusCodes.Status200OK)]
public IActionResult GetAllItems(int skip = 0, int take = 10)
```

{% endcode %}

### GetItem

{% code title="MyItemApiController.cs" %}

```csharp
[HttpGet("{id:guid}")]
[ProducesResponseType<MyItem>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public IActionResult GetItem(Guid id)
```

{% endcode %}

### CreateItem

{% code title="MyItemApiController.cs" %}

```csharp
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
public IActionResult CreateItem(string value)
```

{% endcode %}

### UpdateItem

{% code title="MyItemApiController.cs" %}

```csharp
[HttpPut("{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public IActionResult UpdateItem(Guid id, string value)
```

{% endcode %}

### DeleteItem

{% code title="MyItemApiController.cs" %}

```csharp
[HttpDelete("{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public IActionResult DeleteItem(Guid id)
```

{% endcode %}

## Verifying the changes

Run your application and navigate to the OpenAPI UI at /umbraco/openapi.\
Verify that your API documentation is correctly displaying the routes, parameters, and response types.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/extend-your-project/tutorials/creating-a-backoffice-api/documenting-your-controllers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
