Adding custom page fields to a pageΒΆ

Below is a sample function written in python showing how to add custom page fields to a page.

import requests

AK_USERNAME = '' # Enter your username
AK_PASSWORD = '' # Enter your password
AK_DOMAIN = 'docs.actionkit.com'

def change_page_fields(page_id, custom_page_fields):

    """ Updates the page at page_id with new custom_page_fields

        Example usage: back-dating old image URLs to use Cloudfront instead of S3

        custom_page_fields should be a dictionary,
        with the keys as the field names and the values as the field values.

        Example: custom_page_fields = {
            "featured_image": "https://example.com/image.jpg"
        }
    """

    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 4: Apply custom fields to the page
    response = requests.patch(
        'https://{0}:{1}@{2}{3}'.format(
                AK_USERNAME,
                AK_PASSWORD,
                AK_DOMAIN,
                resource_uri
            ),
        json={'fields': custom_page_fields }
    )

    if response.status_code == 202:
        return "Successfully applied fields {0} to page #{1}".format(
            custom_page_fields,
            page_id
        )

With that function defined, you'll be able to use change_page_fields() with a given page_id and custom_page_fields, like: change_page_fields(1111, {"featured_image": "https://example.com/image.jpg"}) which will add a custom page field (featured image) for page ID 1111.

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.