Adding tags to a pageΒΆ
Below is a sample function written in python showing how to add tags to a page.
import requests
AK_USERNAME = '' # Enter your username
AK_PASSWORD = '' # Enter your password
AK_DOMAIN = 'docs.actionkit.com'
def tag_page(page_id, tag_ids, overwrite_tags=True):
""" Applies tags to a page with the given page_id.
tag_ids: a list of tag IDs. Use a list.
"""
endpoint = 'page'
# Step 1: Get the page by its ID
response = requests.get(
'https://{0}:{1}@{2}/rest/v1/{3}/{4}/'.format(
AK_USERNAME,
AK_PASSWORD,
AK_DOMAIN,
endpoint,
page_id
)
)
# Step 2: Get the resource_uri from the response
resource_uri = response.json()['resource_uri']
# Step 3: Determine which tags to apply
# It's probably easiest to look in the ActionKit admin at /admin/core/tag/ to find the tag IDs
tags = ['/rest/v1/tag/{0}/'.format(tag_id) for tag_id in tag_ids]
if not overwrite_tags:
existing_tags = response.json()['tags']
tags.extend(
[tag['resource_uri'] for tag in existing_tags]
)
# Step 4: Apply tags to the page
response = requests.patch(
'https://{0}:{1}@{2}{3}'.format(
AK_USERNAME,
AK_PASSWORD,
AK_DOMAIN,
resource_uri
),
json={
'tags': tags
}
)
if response.status_code == 202:
return "Successfully applied tags {0} to page #{1}".format(
', '.join([str(tag_id) for tag_id in tag_ids]),
page_id
)
With that function defined, you'll be able to use tag_page()
with a given page_id and tag_ids, like: tag_page(1111, [123, 234, 345])
which will add the tags that have the IDs 123, 234, and 345 to the page with the ID 1111.
Note that this will overwrite any existing tags previously applied to this page.
If you wish to add new tags while keeping existing tags, set overwrite_tags
to False
, like: tag_page(1111, [123, 234, 345], overwrite_tags=False)
.
Looking at /rest/v1/page/schema/, you'll notice that you can only use the GET HTTP method. In other words, /page/ is read-only, used to retrieve results of existing pages. So if we want to edit an page, we'll need to retrieve the resource_uri
using /page/ and then edit that resource by its specific page type.
For example, if you're updating a petitionpage, look at /rest/v1/petitionpage/schema/. You'll notice that you have other http methods available: allowed_detail_http_methods includes get, patch, and put. We'll use PATCH to update the page.