Mastering Producing Liferay Headless APIs

Principles of Producing APIs

API development involves designing, implementing, and managing interfaces for seamless communication between applications. Effective APIs prioritize intuitive design and simplicity while ensuring all necessary data is readily accessible. Liferay provides tools to create tailored endpoints for your integration needs.

This lesson explores Liferay's methods for creating and customizing API endpoints.

Fundamental Principles of Liferay API Design

When designing headless APIs for Liferay, consider these core principles to ensure effective, maintainable, and user-friendly implementations:

  • Consumer-Centric Design: Headless APIs should be consumer-centric. This means they should adhere to established patterns and strive for predictability and expressiveness. The most effective APIs anticipate consumer needs, providing relevant resources and capabilities.
  • Consistency: API consumers often interact with multiple Liferay headless APIs. Maintaining consistency with Liferay's existing patterns and conventions is therefore highly beneficial.
  • Functional Parity: When your application offers parallel functionality in the UI and through headless APIs, ensure consistent behavior. While exceptions for feature enhancements are permissible, API consumers generally expect parity.
  • Performance and Security: Prioritize performance and security in your API design. Your design choices significantly affect application performance and path-level security. Analyze your use case to determine necessary data requests and joins, as well as appropriate security measures.
  • Fine-Grained, Path-Level Authorization: Liferay OAuth2 does not support fine-grained, path-level authorization. If your endpoints require varying authorizations, adjust your design accordingly. For REST Builder, this requires separate projects; for Liferay Objects, this requires separate object definitions.
  • Grouping Related Entities: Group entities that frequently interact. For example, Liferay's Headless Delivery API groups all CMS-related entities under a single API. If you are working with object-based APIs, consider flattening your design into a single object definition.
  • Avoiding Monolithic APIs: Balance grouping entities with modularity. Creating overly large, monolithic APIs or objects can complicate maintenance. While grouping related entities enhances efficiency, break up your application into multiple projects or object definitions when necessary.

By keeping these principles in mind, you can create effective APIs that facilitate seamless integration and empower diverse applications.

Understanding API Production Methods

Producing effective APIs requires a strategic blend of consistency, security, performance, and scalability. Liferay provides multiple methods for creating robust APIs, each suited to different use cases:

  • Liferay Objects
  • REST Builder
  • Microservices

Each method serves different use cases, providing flexibility in how you produce and leverage custom APIs.

Liferay Objects

Liferay Objects automatically generates REST and GraphQL endpoints when you publish an object definition, providing a straightforward, no-code approach to API creation. This enables immediate headless API access for standard CRUD operations. Liferay Objects also supports complex queries with aggregation, nested fields, and custom actions.

Liferay Objects automatically generates REST and GraphQL endpoints when you publish an object definition

Additionally, Liferay generates API endpoints for object relationships and manual actions. You can use these endpoints to manage related entries and trigger business logic.

NOTE
Each object definition determines the structure of its APIs, so careful data modeling is crucial. See the Mastering Data Modeling with Liferay Objects course for best practices.

REST Builder

REST Builder is a code generation tool that creates Liferay-standard headless endpoints compliant with OpenAPI specifications. It automates much of the API's code generation, including interfaces and resource classes, enabling developers to focus on business logic. REST Builder projects include built-in JSON/XML serialization, search, filtering, sorting, and batch operations, and support both REST and GraphQL APIs.

NOTE
REST Builder's dependency on OSGi modules limits its use to Liferay PaaS and Self-Hosted deployments. It is not available for Liferay SaaS implementations.

Microservices

Microservice client extensions enable you to develop bridging APIs that connect Liferay with external systems. These APIs act as intermediaries between Liferay and external systems, delivering tailored, aggregated responses beyond Liferay's built-in headless capabilities. This approach is ideal when dealing with multi-system integrations, data privacy concerns, and custom business logic requirements.

NOTE
Microservices client extensions function independently of Liferay's core, eliminating the need for module deployment. This means they're compatible with all Liferay deployment models, providing maximum flexibility in technology and protocol choices. However, developers must assume full responsibility for security and performance optimization.

Choosing Between API Production Methods

Each of Liferay's API production methods offers distinct advantages:

  • Liferay Objects is ideal for rapid CRUD endpoint creation, offering a no-code approach suitable for scenarios where standard generated endpoints are sufficient.
  • REST Builder is best suited for fine-grained control and data aggregation from multiple sources. However, because REST Builder APIs are deployed within Liferay, they require ongoing maintenance overhead when upgrading between Liferay versions.
  • Microservice client extensions provide maximum flexibility for implementing complex processes, such as when crafting custom response structures, masking sensitive data, or performing business logic across multiple systems. However, these require the most effort to implement and developers need to intentionally adhere to best practices to ensure reliability, performance, and maintainability.
NOTE
Liferay's API Builder (Beta) provides an additional, user-friendly method for creating API endpoints and custom applications. It enables you to filter and sort endpoints, define input parameters (types, scopes, paths, descriptions, schemas), and retrieve entity collections or individual elements. However, API Builder is currently in active beta development and should not be used in production environments. See API Builder for more information.

In many cases, Liferay Objects or microservice client extensions provide sufficient flexibility to meet requirements without the long-term burden of maintaining custom deployments.

Conclusion

Effective headless APIs are vital for integrated solutions. Liferay offers a spectrum of API generation tools, from the rapid, no-code capabilities of Liferay Objects to the precise control of REST Builder and the extensive flexibility of microservices. Strategic use of these tools empowers you to create interconnected ecosystems, fostering innovation and agility.

Next, you'll learn about using Liferay Objects to generate Liferay APIs.

  • Fundamental Principles of Liferay API Design

  • Understanding API Production Methods

  • Choosing Between API Production Methods

  • Conclusion

Capabilities

Product

Education

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy