Adding custom user fields to a user recordΒΆ

Below is a sample function written in python showing how to add a custom user field to a user record. You can add a custom field that has a single value, or if your field accepts multiple values, you can pass in multiple values.

import requests

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

def add_user_field(user_id, field_name, field_values):

    """ Updates the user at user_id with a custom field

        To add a custom user field with a single value:
            Example: add_user_field(123456, "field_name", "field value")

        To add a custom user field with multiple values:
            Example: add_user_field(123456, "field_name", ["a", "b", "c"])

    """

    endpoint = 'user'

    custom_field = {
        field_name: field_values
    }

    response = requests.patch(
        'https://{0}:{1}@{2}/rest/v1/{3}/{4}/'.format(
            AK_USERNAME,
            AK_PASSWORD,
            AK_DOMAIN,
            endpoint,
            user_id
        ),
        json={
            "fields": custom_field
        }
    )

    if response.status_code == 202:
        return "Successfully added custom field for user #{0}".format(
            user_id
        )
    else:
        return "Did not add custom field for user #{0}: {1}".format(
            user_id,
            response.text
        )

With that function defined, you'll be able to use add_user_field() with a given user_id, field_name, and field_value(s), like: user_add_field(123456, "important_issues", "Climate Change") which will add a custom user field with the name of "important_issues" and the value "Climate Change" for user ID 123456.

If your custom user field supports multiple answers, you could pass in multiple field_values, like: user_add_field(123456, "important_issues", ["Climate Change", "Medicare for All", "Free College"]) to add a custom user field with multiple values for that user.