Navigation Menu API Basics
Liferay’s REST APIs provide services for Liferay’s navigation menus. You can create and edit navigation menus with the API. Start by seeing an example of adding a new navigation menu.
Liferay DXP 2025.Q2+/Portal GA135+ The Navigation Menus API now uses External Reference Codes (ERCs) to reference these elements, enabling consistent identification across instances and supporting batch export/import for improved content management and portability.
Adding a Navigation Menu
Start a new Liferay DXP instance by running
Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps:
-
Download and unzip Categories and Vocabulary API Basics.
-
Find your site ID. Use it in different service calls below.
-
Use the cURL script to add a new navigation menu to your Site. On the command line, navigate to the
curl
folder. Execute theNavigationMenus_POST_ToSites.sh
script with yoursite ID
as a parameter.The JSON response shows a new navigation menu has been added:
-
Go to the Navigation Menus application by navigating to Administration Menu → Site Builder → Navigation Menus. See that a new navigation menu has been added.
-
The REST service can also be called using the Java client. Navigate out of the
curl
folder and into thejava
folder. Compile the source files with the following command: -
Run the
NavigationMenus_POST_ToSites
class with the following command. Replace thesiteId
value with your site ID:
Examine the cURL Command
The NavigationMenus_POST_ToSites.sh
script calls the REST service with a cURL command.
Here are the command’s arguments:
Arguments | Description |
---|---|
-H "Content-Type: application/json" | Indicates that the request body format is JSON. |
-X POST | The HTTP method to invoke at the specified endpoint |
"http://localhost:8080/o/headless-delivery/v1.0/sites/${1}/navigation-menus" | The REST service endpoint |
-d "{\"name\": \"Foo\"}" | The data you are requesting to post |
-u "test@liferay.com:learn" | Basic authentication credentials |
Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application that uses OAuth2.
The other cURL commands use similar JSON arguments.
Examine the Java Class
The NavigationMenus_POST_ToSites.java
class adds a navigation menu by calling the navigation menu related service.
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
NavigationMenuResource.Builder builder = ... | Gets a Builder for generating a NavigationMenuResource service instance. |
NavigationMenuResource navigationMenuResource = builder.authentication(...).build(); | Specifies basic authentication and generates a NavigationMenuResource service instance. |
NavigationMenu navigationMenu = navigationMenuResource.postSiteNavigationMenu(...); | Calls the navigationMenuResource.postSiteNavigationMenu method and passes the data to post. |
Note that the project includes the com.liferay.headless.delivery.client.jar
file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api
.
The main
method’s comment demonstrates running the class.
The other example Java classes are similar to this one, but call different NavigationMenuResource
methods.
See NavigationMenuResource for service details.
Below are examples of calling other NavigationMenu
REST services using cURL and Java.
Get Navigation Menus from Site
You can list a site’s navigation menus by executing the following cURL or Java command. As above, replace 1234
with your site ID.
NavigationMenus_GET_FromSites.sh
Command:
Code:
NavigationMenus_GET_FromSites.java
Command:
Code:
The site’s NavigationMenu
objects appear in JSON.
Get a Navigation Menu
Get a specific navigation menu with the following cURL or Java command. Replace 1234
with the navigation menu’s ID.
Use NavigationMenus_GET_FromSites.[java|sh]
to get NavigationMenu
IDs.
NavigationMenus_GET_ById.sh
Command:
Code:
NavigationMenus_GET_ById.java
Command:
Code:
The NavigationMenu
fields appear in JSON.
Put a Navigation Menu
Do a complete overwrite of an existing navigation menu with the following cURL and Java commands. Note, replace 1234
with your navigation menu’s ID.
NavigationMenus_PUT_ById.sh
Command:
Code:
NavigationMenus_PUT_ById.java
Command:
Code:
Delete a Navigation Menu
Delete an existing navigation menu with the following cURL and Java commands. Note, replace 1234
with your navigation menu’s ID.
NavigationMenus_DELETE_ById.sh
Command:
Code:
NavigationMenus_DELETE_ById.java
Command
Code:
The API Explorer lists all NavigationMenu
services and schemas and has an interface to try out each service.