Tags API Basics
Liferay’s REST APIs provide services for managing tags (referred to as keywords in Liferay’s codebase). You can create, edit, and delete tags using the API. Start with an example of adding a new tag.
Add a Tag
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 Tags API Basics.
-
Find your site ID and use it in the following service calls.
-
Use the cURL script to add a new tag to your site. On the command line, navigate to the
curl
folder. Execute theKeywords_POST_ToSites.sh
script with your site ID as a parameter.The JSON response shows a new tag has been added:
-
Go to the Tags application by navigating to Administration Menu → Categorization → Tags. See that a new tag 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
Keywords_POST_ToSites
class with the following command. Replace thesiteId
value with your site ID:
Examine the cURL Command
The Keywords_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-admin-taxonomy/v1.0/sites/${1}/keywords" | 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 Keywords_POST_ToSites.java
class adds a tag by calling the keyword related service.
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
KeywordResource.Builder builder = ... | Gets a Builder for generating a KeywordResource service instance. |
KeywordResource keywordResource = builder.authentication(...).build(); | Specifies basic authentication and generates a KeywordResource service instance. |
Keyword keyword = keywordResource.postSiteKeyword(...); | Calls the keywordResource.postSiteKeyword method and passes the data to post. |
Note that the project includes the com.liferay.headless.admin.taxonomy.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 KeywordResource
methods.
See KeywordResource for service details.
Below are examples of calling other Keyword
REST services using cURL and Java.
Get Keyword Posts from Site
You can list a site’s tags by executing the following cURL or Java command. As above, replace 1234
with your site ID.
Keywords_GET_FromSites.sh
Command:
Code:
Keywords_GET_FromSites.java
Command:
Code:
The Site’s Keyword
objects appear in JSON.
Get a Keyword
Get a specific tag with the following cURL or Java command. Replace 1234
with the tag’s ID.
Use Keywords_GET_FromSites.[java|sh]
to get site Keyword
IDs.
Keywords_GET_ById.sh
Command:
Code:
Keywords_GET_ById.java
Command:
Code:
The Keyword
fields appear in JSON.
Put a Keyword
Completely overwrite an existing tag with the following cURL and Java commands. Note, replace 1234
with your tag’s ID.
Keywords_PUT_ById.sh
Command:
Code:
Keywords_PUT_ById.java
Command:
Code:
Delete a Keyword
Delete an existing tag with the following cURL and Java commands. Note, replace 1234
with your tag’s ID.
Keywords_DELETE_ById.sh
Command:
Code:
Keywords_DELETE_ById.java
Command
Code:
The API Explorer lists all Keyword
services and schemas and has an interface to try out each service.
Improved Tags Management via Batch Engine
Liferay DXP 2025.Q2+/Portal GA135+
The Tags API supports batch export and import for migrating tags across Liferay instances. The process includes handling user filters based on creation/modification dates, name, or other attributes, and maintaining creator data during imports.
For details on using the Batch Engine, see Exporting and Importing Data using the Batch Engine API.
Filter Data
Refine exported data using query parameters.
Here’s how the query parameters work in the example:
-
Use
http://localhost:8080/o/headless-batch-engine/v1.0/export-task/
to start an export task using the Batch Engine API. -
com.liferay.headless.admin.taxonomy.dto.v1_0.Keyword/JSON
specifies the type of data to export (in this case, tags in JSON format). TheKeyword
refers to the class in the API responsible for representing tags. -
taskItemDelegateName=Marketing
specifies which tag you want to export. In this example, the valueMarketing
corresponds to a tag named Marketing. Replace this value with the actual name of the tag you want to export. -
filter=name%20eq%20%27Marketing%27
refines the data returned by the export. The filter query parameter is URL-encoded here (name%20eq%20%27Marketing%27
). The equivalent human-readable version of the filter query isfilter=name eq 'Marketing'
. This filters the results to only include items where the name field is equal to “Marketing”.
Maintain Creator Data
When importing tags, use the importCreatorStrategy
query parameter to define how creator data should be handled. The creatorStrategy
parameter determines what happens if the creator does not exist in the target system.
You can use these parameters:
-
creatorStrategy=INSERT
Ensures that a new user is added as the creator if the specified creator is missing in the target system. This is required when using
importCreatorStrategy=KEEP_CREATOR
to ensure missing creators are added during import. -
importCreatorStrategy=KEEP_CREATOR
Retains the original creator specified in the exported data. If the creator does not exist in the target system, the import fails unless
creatorStrategy=INSERT
is also set. -
importCreatorStrategy=OVERWRITE_CREATOR
Replaces the original creator with the importing user. All imported tags are assigned to the user performing the import, regardless of the original creator’s information.
If importCreatorStrategy
is omitted, the system defaults to OVERWRITE_CREATOR
.
Here’s an example: