Skip to main content

API

This command is used to generate the API on a page. It creates a complete API integration layer following clean architecture principles.

morpheme api [api_name] -p [page_name] -f [feature_name]

Options

morpheme api [api_name] [options]

To see all available options and flags, run morpheme api --help.

Available Options

OptionAbbrDescriptionDefault
--feature-name-fName of the feature to be added api (Mandatory)
--page-name-pName of the page to be added api (Mandatory)
--method-mHTTP method for the API (get, post, put, patch, delete, multipart, etc.)post
--pathCustom path for the API endpoint
--return-data-rType of data to return (model, header, body_bytes, raw, etc.)model
--body-listSet if the request body is a listfalse
--response-listSet if the response body is a listfalse
--json2dartGenerate models handled by json2dartfalse
--headerPath to JSON file for additional headers
--cache-strategyStrategy for caching API response
--ttlCache duration in minutes
--keep-expired-cacheKeep cache even if expired
--apps-name-aGenerate for specific apps (Optional)

Method Options

Values for -m: get, post, put, patch, delete, multipart, postMultipart, patchMultipart, head, getSse, postSse, putSse, patchSse, deleteSse, download.

Return Data Options

Values for -r: model, header, body_bytes, body_string, status_code, raw.

Cache Strategy Options

Values for --cache-strategy: async_or_cache, cache_or_async, just_async, just_cache.

Examples

Basic Usage: Generate a get_users API for the users page in the master feature.

morpheme api get_users -p users -f master -m get

With Response List: Generate an API where the response is a list of objects.

morpheme api get_products -p products -f shop -m get --response-list

With Caching: Generate an API with caching strategy and TTL.

morpheme api get_profile -p profile -f user -m get --cache-strategy async_or_cache --ttl 60
info

Generated Artifacts:

  • Models: Request/Response models
  • Entities: Domain entities
  • Datasource: Remote data source definition and implementation
  • Repositories: Repository interface and implementation
  • UseCase: Domain use case
  • BLoC: Integration into the page's BLoC/Cubit
tip

Use the json2dart feature for more powerful model generation. Check the link: Json2Dart

caution

API generation will fail if:

  • Although the command creates files, the Feature or Page must exist beforehand logic-wise (created via morpheme feature or morpheme page).
  • The API name already exists in the target page.