Documentation

Tags API Basics

Liferay’s REST APIs provide services for Liferay DXP/Portal’s tags. You can create and edit tags with the API. Start by seeing an example of adding a new tag. Note that in Liferay’s codebase, tags are called keywords.

Adding a Tag

Start a new Liferay DXP instance by running

docker run -it -m 8g -p 8080:8080 liferay/dxp:7.4.13-u22

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:

  1. Download and unzip Tags API Basics.

    curl https://learn.liferay.com/dxp/latest/en/content-authoring-and-management/tags-and-categories/developer-guide/liferay-r7u9.zip -O
    
    unzip liferay-r7u9.zip
    
  2. Find your Site’s ID. You’ll use this in different service calls below.

  3. Use the cURL script to add a new tag to your Site. On the command line, navigate to the curl folder. Execute the Keyword_POST_ToSite.sh script with your site ID as a parameter.

    ./Keyword_POST_ToSite.sh 1234
    

    The JSON response shows a new tag has been added:

    "creator" : {
    "additionalName" : "",
    "contentType" : "UserAccount",
    "familyName" : "Test",
    "givenName" : "Test",
    "id" : 20129,
    "name" : "Test Test",
    "profileURL" : "/web/test"
    },
    "dateCreated" : "2021-09-09T21:15:46Z",
    "dateModified" : "2021-09-09T21:15:46Z",
    "id" : 40130,
    "keywordUsageCount" : 0,
    "name" : "foo",
    "siteId" : 20125
    
  4. Go to the Tags application by navigating to Administration MenuCategorizationTags. See that a new tag has been added.

    See that a new tag has been added.

  5. The REST service can also be called using the Java client. Navigate out of the curl folder and into the java folder. Compile the source files with the following command:

    javac -classpath .:* *.java
    
  6. Run the Keyword_POST_ToSite class with the following command. Replace the siteId value with your Site’s ID:

    java -classpath .:* -DsiteId=1234 Keyword_POST_ToSite
    

Examine the cURL Command

The Keyword_POST_ToSite.sh script calls the REST service with a cURL command.

curl \
	-H "Content-Type: application/json" \
	-X POST \
	"http://localhost:8080/o/headless-admin-taxonomy/v1.0/sites/${1}/keywords" \
	-d "{\"name\": \"Foo\"}" \
	-u "[email protected]:learn"

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

Note

Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2.

The other cURL commands use similar JSON arguments.

Examine the Java Class

The Keyword_POST_ToSite.java class adds a tag by calling the keyword related service.

public static void main(String[] args) throws Exception {
	KeywordResource.Builder builder = KeywordResource.builder();

	KeywordResource keywordResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Keyword keyword = keywordResource.postSiteKeyword(
		Long.valueOf(System.getProperty("siteId")),
		new Keyword() {
			{
				name = "Foo";
			}
		});

	System.out.println(keyword);
}

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.

Note

The main method’s comment demonstrates running the class.

The other example Java classes are similar to this one, but call different KeywordResource methods.

Important

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’s ID.

Keywords_GET_FromSite.sh

Command:

./Keywords_GET_FromSite.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-taxonomy/v1.0/sites/${1}/keywords" \
	-u "[email protected]:learn"

Keywords_GET_FromSite.java

Command:

java -classpath .:* -DsiteId=1234 Keywords_GET_FromSite

Code:

public static void main(String[] args) throws Exception {
	KeywordResource.Builder builder = KeywordResource.builder();

	KeywordResource keywordResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Page<Keyword> page = keywordResource.getSiteKeywordsPage(
		Long.valueOf(System.getProperty("siteId")), null, null,
		Pagination.of(1, 2), null);

	System.out.println(page);
}

The Site’s Keyword objects are listed in JSON.

Get a Keyword

Get a specific tag with the following cURL or Java command. Replace 1234 with the tag’s ID.

Tip

Use Keywords_GET_FromSite.[java|sh] to get site Keyword IDs.

Keyword_GET_ById.sh

Command:

./Keyword_GET_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-taxonomy/v1.0/keywords/${1}" \
	-u "[email protected]:learn"

Keyword_GET_ById.java

Command:

java -classpath .:* -DkeywordId=1234 Keyword_GET_ById

Code:

public static void main(String[] args) throws Exception {
	KeywordResource.Builder builder = KeywordResource.builder();

	KeywordResource keywordResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Keyword keyword = keywordResource.getKeyword(
		Long.valueOf(System.getProperty("keywordId")));

	System.out.println(keyword);
}

The Keyword fields are listed 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.

Keyword_PUT_ById.sh

Command:

./Keyword_PUT_ById.sh 1234

Code:

curl \
	-H "Content-Type: application/json" \
	-X PUT \
	"http://localhost:8080/o/headless-admin-taxonomy/v1.0/keywords/${1}" \
	-d "{\"name\": \"Bar\"}" \
	-u "[email protected]:learn"

Keyword_PUT_ById.java

Command:

java -classpath .:* -DkeywordId=1234 Keyword_PUT_ById

Code:

public static void main(String[] args) throws Exception {
	KeywordResource.Builder builder = KeywordResource.builder();

	KeywordResource keywordResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Keyword keyword = keywordResource.putKeyword(
		Long.valueOf(System.getProperty("keywordId")),
		new Keyword() {
			{
				name = "Bar";
			}
		});

	System.out.println(keyword);
}

Delete a Keyword

Delete an existing tag with the following cURL and Java commands. Note, replace 1234 with your tag’s ID.

Keyword_DELETE_ById.sh

Command:

./Keyword_DELETE_ById.sh 1234

Code:

curl \
	-X DELETE \
	"http://localhost:8080/o/headless-admin-taxonomy/v1.0/keywords/${1}" \
	-u "[email protected]:learn"

Keyword_DELETE_ById.java

Command

java -classpath .:* -DkeywordId=1234 Keyword_DELETE_ById

Code:

public static void main(String[] args) throws Exception {
	KeywordResource.Builder builder = KeywordResource.builder();

	KeywordResource keywordResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	keywordResource.deleteKeyword(
		Long.valueOf(System.getProperty("keywordId")));
}

The API Explorer lists all of the Keyword services and schemas and has an interface to try out each service.