API Reference

This page provides detailed documentation for the classes and methods available in the Serverpod Swagger package.

SwaggerUIRoute

The SwaggerUIRoute class is the main entry point for adding Swagger UI to your Serverpod server.

Constructor

1SwaggerUIRoute(
2  Directory projectDirectory, {
3  String mountPath = '/swagger/',
4})

Parameters

ParameterTypeDescription
projectDirectoryDirectoryThe root directory of your Serverpod project
mountPathStringThe base path where Swagger UI will be served (must end with a trailing slash)
apiSpecPathStringPath to the OpenAPI specification file

Methods

handleCall

1
2Future<bool> handleCall(HttpRequest request) async

Handles incoming HTTP requests to the Swagger UI route. This method is called by the Serverpod web server when a request is made to the configured mount path.

ServerpodSwaggerVersion

The ServerpodSwaggerVersion class provides version information for the Serverpod Swagger package.

Properties

PropertyTypeDescription
versionStringThe current version of the Serverpod Swagger package

Command-line Generator

The generate.dart script in the bin directory is used to generate OpenAPI specifications from your Serverpod protocol definitions.

Usage

dart run serverpod_swagger:generate [options]

Options

OptionDescriptionDefault
--outputPath to the output fileapispec.json
--titleAPI titleYour project name
--descriptionAPI descriptionEmpty
--versionAPI version1.0.0
--server-urlServer URLhttp://localhost:8080
--server-descriptionServer descriptionDevelopment server

Annotations

The following annotations can be used to customize the OpenAPI specification generation:

@Route

Specifies the route path for an endpoint. This annotation is provided by Serverpod and is used by the Swagger generator to determine the endpoint paths.

1('/users/:id')
2Future<User> getUser(Session session, int id);

@Method

Specifies the HTTP method for an endpoint. If not provided, the method is inferred from the endpoint name.

1('PATCH')
2('/users/:id')
3Future<User> updateUserPartial(Session session, int id, User user);

@Security

Specifies the security requirements for an endpoint.

1(['BearerAuth'])
2('/users/profile')
3Future<User> getUserProfile(Session session);

@Param

Specifies the parameter name for an endpoint parameter. This is useful when the parameter name in the Dart code doesn't match the desired parameter name in the API.

1('/users/search')
2Future<List<User>> searchUsers(
3  Session session,
4  ('query') String searchQuery,
5);

Next Steps