The Microsoft ASP.NET Core API documentation is a great place to familiarize yourself with API concepts. It can be found on the official ASP.NET Core site.
Public APIs in Umbraco
A public API in Umbraco is created as any other ASP.NET Core API:
ProductsController.cs
using Microsoft.AspNetCore.Mvc;
namespace UmbracoDocs.Samples;
[ApiController]
[Route("/api/shop/products")]
public class ProductsController : Controller
{
[HttpGet]
public IActionResult GetAll() => Ok(new[] { "Table", "Chair", "Desk", "Computer" });
}
Adding member protection to public APIs
To protect your APIs based on front-end membership, you can annotate your API controllers with the [UmbracoMemberAuthorize] attribute.
There are 3 parameters that can be supplied to control how the authorization works:
// Comma delimited list of allowed member types
string AllowType
// Comma delimited list of allowed member groups
string AllowGroup
// Comma delimited list of allowed member Ids
string AllowMembers
To allow all members, use the attribute without supplying any parameters.
You can apply these attributes either at controller level or at action level.