API Documentation (Swagger / OpenAPI)

Finch has a built-in OpenAPI documentation system. You describe each API route using ApiDoc, and Finch generates a machine-readable OpenAPI JSON spec and a Swagger UI that lets developers explore and test your API from a browser.

Setting up API docs requires three steps:

  1. Create an ApiController instance.
  2. Register the documentation routes (OpenAPI JSON output and Swagger UI).
  3. Attach ApiDoc objects to the routes that should appear in the documentation.

Step 1 — Create the ApiController

ApiController reads all routes from your app and generates the OpenAPI spec:

final apiController = ApiController(
  title: 'My App API',
  app: app,
);

Step 2 — Register Documentation Routes

Add two routes to your router: one for the raw OpenAPI JSON and one for the Swagger UI:

[
  // Returns the OpenAPI JSON spec — used by Swagger UI and API clients
  FinchRoute(
    key: 'root.api.docs',
    path: 'api/docs',
    index: apiController.indexPublic,
  ),

  // Swagger UI page — pass the URL to the JSON spec
  FinchRoute(
    key: 'root.swagger',
    path: 'swagger',
    index: () => apiController.swagger(rq.url('api/docs')),
  ),
];

Open /swagger in your browser to see the interactive documentation.

By default the Swagger UI is only accessible in local debug mode (isLocalDebug: true). To make it public, pass showPublic: true to apiController.swagger(..., showPublic: true).

Step 3 — Define ApiDoc for Routes

Each route can have an ApiDoc that describes what the route does, what parameters it accepts, and what responses it returns. Attach it to a FinchRoute via the apiDoc property.

FinchRoute(
  key: 'api.books.list',
  path: 'api/books',
  methods: Methods.ONLY_GET,
  index: booksController.list,
  apiDoc: ApiDoc(
    get: ApiDoc(
      description: 'Returns a paginated list of books.',
      parameters: [
        ApiParameter<int>(
          'page',
          isRequired: false,
          paramIn: ParamIn.query,
          def: 1,
        ),
        ApiParameter<int>(
          'limit',
          isRequired: false,
          paramIn: ParamIn.query,
          def: 20,
        ),
      ],
      response: {
        '200': [
          ApiResponse<int>('count', def: 0),
          ApiResponse<List>('rows', def: []),
        ],
        '401': r_401,
        '404': r_404,
      },
    ),
  ),
),

ApiDoc Properties

ApiDoc can be used at the route level or per HTTP method. Nest an inner ApiDoc for each method:

ApiDoc(
  get:    ApiDoc(description: '...', parameters: [...], response: {...}),
  post:   ApiDoc(description: '...', parameters: [...], response: {...}),
  put:    ApiDoc(description: '...', parameters: [...], response: {...}),
  delete: ApiDoc(description: '...', parameters: [...], response: {...}),
)

ApiParameter

ApiParameter<T> describes a single input parameter:

Property Type Description
First arg String Parameter name
isRequired bool Whether the field is required
paramIn ParamIn Where the value comes from
def T? Default value example

ParamIn values:

Value Description
ParamIn.path URL path segment (e.g., /books/{id})
ParamIn.query Query string (e.g., ?page=1)
ParamIn.header HTTP request header
ParamIn.body Request body (POST/PUT)

ApiResponse

ApiResponse<T> describes a field in the response body:

ApiResponse<String>('title', def: 'Book Title'),
ApiResponse<int>('id', def: 0),
ApiResponse<bool>('success', def: true),
ApiResponse<Map<String, dynamic>>('data', def: {}),

Predefined Response Shortcuts

Finch provides ready-made response lists for common HTTP error codes:

// Use in your response map:
response: {
  '200': [...],
  '401': r_401,  // Unauthorized
  '404': r_404,  // Not found
  '500': r_500,  // Server error
}

Complete Route Example

FinchRoute(
  key: 'api.books.one',
  path: 'api/books/{id}',
  methods: Methods.GET_POST,
  index: booksController.one,
  apiDoc: ApiDoc(
    get: ApiDoc(
      description: 'Get a single book by ID.',
      parameters: [
        ApiParameter<String>('id', isRequired: true, paramIn: ParamIn.path),
      ],
      response: {
        '200': [
          ApiResponse<int>('id', def: 0),
          ApiResponse<String>('title', def: ''),
          ApiResponse<String>('author', def: ''),
        ],
        '404': r_404,
      },
    ),
    post: ApiDoc(
      description: 'Update a book by ID.',
      parameters: [
        ApiParameter<String>('id', isRequired: true, paramIn: ParamIn.path),
        ApiParameter<String>('title', isRequired: false, paramIn: ParamIn.body),
        ApiParameter<String>('author', isRequired: false, paramIn: ParamIn.body),
      ],
      response: {
        '200': [ApiResponse<bool>('success', def: true)],
        '404': r_404,
      },
    ),
  ),
),

Api documentation controller

final apiController = ApiController(
   title: "API Documentation",
   app: app,
);
// Routes that should be added to your FinchApp routing
// OpenApi json output
FinchRoute(
    key: 'root.api.docs',
    path: 'api/docs',
    index: apiController.indexPublic,
),
// Swagger UI
FinchRoute(
    key: 'root.swagger',
    path: 'swagger',
    index: () => apiController.swagger(rq.url('api/docs')),
),

Deffining ApiDoc for each route

class ApiDocuments {
    static Future<ApiDoc> onePerson() async {
    return ApiDoc(
      post: ApiDoc(
        response: {
          '200': [
            ApiResponse<int>('timestamp_start', def: 0),
            ApiResponse<bool>('success', def: true),
            ApiResponse<Map<String, String>>(
              'data',
              def: PersonCollectionFree.formPerson.fields.map((k, v) {
                return MapEntry(k, v.defaultValue?.call());
              }),
            ),
          ],
          '404': r_404,
        },
        description: "Update one person by id.",
        parameters: [
          ApiParameter<String>(
            'id',
            isRequired: true,
            paramIn: ParamIn.path,
          ),
          ApiParameter<String>(
            'name',
            isRequired: false,
            paramIn: ParamIn.header,
          ),
          ApiParameter<int>(
            'age',
            isRequired: false,
            paramIn: ParamIn.header,
          ),
          ApiParameter<double>(
            'height',
            isRequired: false,
            paramIn: ParamIn.header,
          ),
          ApiParameter<String>(
            'email',
            isRequired: true,
            paramIn: ParamIn.header,
          ),
          ApiParameter<String>(
            'married',
            isRequired: false,
            paramIn: ParamIn.header,
            def: false,
          ),
        ],
      ),
      get: ApiDoc(
        response: {
          '200': [
            ApiResponse<int>('timestamp_start', def: 0),
            ApiResponse<Map<String, String>>(
              'data',
              def: PersonCollectionFree.formPerson.fields.map((k, v) {
                return MapEntry(k, v.defaultValue?.call());
              }),
            ),
          ],
          '404': r_404,
        },
        description: "Get one person by id.",
        parameters: [
          ApiParameter<String>('id', isRequired: true, paramIn: ParamIn.path),
        ],
      ),
      delete: ApiDoc(
        response: {
          '200': [
            ApiResponse<int>('timestamp_start', def: 0),
            ApiResponse<bool>('success', def: true),
          ],
          '404': r_404,
        },
        description: "Delete one person by id.",
        parameters: [
          ApiParameter<String>('id', isRequired: true, paramIn: ParamIn.path),
        ],
      ),
    );
  }
}

/// Adding ApiDoc to Routes for example
FinchRoute(
  key: 'root.person.show',
  path: 'api/person/{id}',
  extraPath: ['example/person/{id}'],
  index: homeController.onePerson,
  methods: Methods.GET_POST,
  apiDoc: ApiDocuments.onePerson,
),