Forum Discussion

jeffshurtliff's avatar
5 years ago

Add multiple tags to a message with a single API call?

Can anyone tell me if it is possible to add multiple tags to a message with a single API call, or if they have to be added one-by-one with a separate API call for each?

Looking at the v2 subcollection documentation it shows the payload for tags like this:

{
    "data": {
        "type": "tag",
        "text": "something"
    }
}

This works fine for me and I'm able to add a single tag to a message via the API without issue.  However, what about when I want to add multiple tags?

In my experimenting I tried performing the POST request using the JSON syntax below (wrapping multiple entries in a list/array) but, while it returned a "success" response, the tags did not get added to the message.

 

This is the syntax I used:

{
    "data": [
        {
            "type": "tag",
            "text": "tag1"
        },
        {
            "type": "tag",
            "text": "tag1"
        }
    ]}
}

 

This is the response I received:

{
    "status": "success", 
    "message": "",
    "http_code": 200,
    "data": {}, 
    "metadata": {}
}

 

Any idea on what I could be doing wrong?

Thanks in advance!

  • Hi jeffshurtliff ,

    It looks like when trying to update via the subcollection, you can only add one tag at a time. However, through the message collection itself, you can set multiple tags at a time.

    For example:

    curl --location --request PUT '.../api/2.0/messages/882?restapi.session_key=...' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "data":{
            "type":"message",
            "tags": [
            	{
                	"type": "tag",
                	"text": "tag3"
            	},
            	{
                	"type": "tag",
                	"text": "tag4"
            	}
            ]
        }
    }'

    Please be aware that this will set the tags exactly as you've specified, and any existing tags on the post that are not specified will be removed. So if the message in my example started with tags "tag1", "tag2", and "tag3", after the call it would have only tags "tag3" and "tag4". Tags "tag1" and "tag2" would be deleted.

    I hope this helps!

2 Replies

  • AdamN's avatar
    AdamN
    Khoros Oracle
    5 years ago

    Hi jeffshurtliff ,

    It looks like when trying to update via the subcollection, you can only add one tag at a time. However, through the message collection itself, you can set multiple tags at a time.

    For example:

    curl --location --request PUT '.../api/2.0/messages/882?restapi.session_key=...' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "data":{
            "type":"message",
            "tags": [
            	{
                	"type": "tag",
                	"text": "tag3"
            	},
            	{
                	"type": "tag",
                	"text": "tag4"
            	}
            ]
        }
    }'

    Please be aware that this will set the tags exactly as you've specified, and any existing tags on the post that are not specified will be removed. So if the message in my example started with tags "tag1", "tag2", and "tag3", after the call it would have only tags "tag3" and "tag4". Tags "tag1" and "tag2" would be deleted.

    I hope this helps!

  • Awesome, this is EXACTLY what I need, thanks AdamN

    And thanks for the heads up about the tags getting overwritten.  It sounds like I'll want to build in a function to first grab the existing tags with a GET request and then aggregate them into a single array of dictionaries and then pass them in the PUT request.

    Thanks again!