Skip to content

API Documentation

Granit.ApiDocumentation generates OpenAPI documents and serves the Scalar interactive UI. One OpenAPI document is generated per declared API major version. Supports JWT Bearer and OAuth2 Authorization Code with PKCE for interactive authentication.

[DependsOn(typeof(GranitApiDocumentationModule))]
public class AppModule : GranitModule { }
{
"ApiDocumentation": {
"Title": "Clinic API",
"MajorVersions": [1, 2],
"Description": "Patient and appointment management API"
}
}

In Program.cs:

app.UseGranitApiDocumentation(); // Maps /openapi/v1.json, /openapi/v2.json, /scalar

Provide realistic example values for request DTOs without coupling to OpenAPI:

public class AppointmentSchemaExamples : ISchemaExampleProvider
{
public IReadOnlyDictionary<Type, JsonNode> GetExamples() => new Dictionary<Type, JsonNode>
{
[typeof(CreateAppointmentRequest)] = new JsonObject
{
["patientId"] = "d4e5f6a7-1234-5678-9abc-def012345678",
["doctorId"] = "a1b2c3d4-5678-9abc-def0-123456789abc",
["scheduledAt"] = "2026-04-15T09:30:00Z",
["durationMinutes"] = 30
}
};
}

Implementations of ISchemaExampleProvider are auto-discovered at startup.

Exclude inter-service endpoints from public documentation:

app.MapPost("/webhooks/keycloak", HandleKeycloakWebhook)
.WithMetadata(new InternalApiAttribute());

The module registers these OpenAPI transformers automatically:

TransformerPurpose
JwtBearerSecuritySchemeTransformerAdds Bearer security scheme when JWT is configured
OAuth2SecuritySchemeTransformerReplaces Bearer with OAuth2 Authorization Code when configured
SecurityRequirementOperationTransformerAnonymous endpoints override global security
ProblemDetailsSchemaDocumentTransformerAdds RFC 7807 ProblemDetails schema
ProblemDetailsResponseOperationTransformerDocuments 4xx/5xx Problem Details responses
TenantHeaderOperationTransformerDocuments X-Tenant-Id header when enabled
InternalApiDocumentTransformerRemoves [InternalApi] endpoints
WolverineOpenApiOperationTransformerEnhances Wolverine HTTP endpoint documentation
SchemaExampleSchemaTransformerApplies ISchemaExampleProvider examples
PropertyDefaultDescription
Title"API"OpenAPI document and Scalar UI title
MajorVersions[1]Major version numbers to document
DescriptionnullOpenAPI description (Markdown supported)
ContactEmailnullContact email in OpenAPI info
LogoUrlnullLogo URL for Scalar sidebar
FaviconUrlnullFavicon for Scalar page
EnableInProductionfalseExpose docs in Production
EnableTenantHeaderfalseDocument required tenant header
TenantHeaderName"X-Tenant-Id"Tenant header name
AuthorizationPolicynullPolicy for doc endpoints (null = inherit, "" = anonymous)
OAuth2.AuthorizationUrlnullOAuth2 authorization endpoint
OAuth2.TokenUrlnullOAuth2 token endpoint
OAuth2.ClientIdnullPublic OAuth2 client ID (PKCE-capable)
OAuth2.EnablePkcetrueEnable PKCE with S256
OAuth2.Scopes["openid"]OAuth2 scopes to request
CategoryKey typesPackage
ModuleGranitApiDocumentationModule
OptionsApiDocumentationOptions, OAuth2OptionsGranit.ApiDocumentation
Extension pointsISchemaExampleProvider, InternalApiAttributeGranit.ApiDocumentation
ExtensionsAddGranitApiDocumentation(), UseGranitApiDocumentation()Granit.ApiDocumentation