Templates

Create Template

Note

The template creation process via the python library deviates from template creation through cURL requests. For details on constructing a template using the python library, please see Template Operations.

To create a template, use the Template class’ create method:

import uareach as ua


client = ua.Reach('email', 'api_key')

# 1. Initialize an Apple template; add metadata like name, description, etc
apple_loyalty = ua.AppleTemplate()
apple_loyalty.add_metadata(
    name='An Apple template',
    description='Rewards points',
    type_=ua.Type.LOYALTY
)

# 2. Add template headers
apple_loyalty.add_headers(
    barcode_value='123456789',
    icon_image='https://s3.amazonaws.com/passtools_prod/1/images/default-pass-icon.png',
    barcode_type=ua.BarcodeType.PDF_417,
    logo_image='https://s3.amazonaws.com/passtools_prod/1/images/default-pass-logo.png',
    background_color=ua.rgb(49,159,196),
    barcode_encoding='iso-8859-1',
    logo_color=ua.rgb(24,86,148),
    barcodeAltText='123456789',
    foreground_color=ua.rgb(255,255,255),
    logo_text='Logo Text'
)

# 3. Create and add some fields
program_name = ua.Field(
    name='Program Name',
    label='Bleep',
    value='Program Name',
    fieldType=ua.AppleFieldType.PRIMARY
)

points = ua.Field(
    name='Points',
    label='Points',
    value=1234.0,
    fieldType=ua.AppleFieldType.HEADER
)

apple_loyalty.add_fields(
   program_name,
   points
)

# 4. Call the create method
response = my_template.create(client, project_id=12345)

Update Template

To update a template, use the Template class’ update method:

import uareach as ua

client = ua.Reach('email', 'api_key')

# 1. Get a template to update
my_template = ua.get_template(client, template_id=12345)

# 2. Example: Update the template's 'Member Name' field
member_name = my_template.fields['Member Name']
member_name['value'] = 'The Biebz'

# 3. Call the update method on the reach instance
response = my_template.update(client)

Note

As the example above shows, when updating a key-value pair within a field, you can just treat the field as a dictionary. To remove or create new fields/headers/metadata, you can use the methods described in the Template Operations doc.

Get Template

To get a template, use the get_template function:

import uareach as ua

client = ua.Reach('email', 'api_key')
my_template = ua.get_template(client, template_id=12345)

List Templates

To get a list of templates, use the TemplateList class:

import uareach as ua

client = ua.Reach('email', 'api_key')

template_list = ua.TemplateList(client)

for template_header in template_list:
   print template_header

Delete a Template

To delete a template, use the delete_template function:

import uareach as ua


client = ua.Reach('email', 'api_key')
response = ua.delete_template(client, template_id=12345)

Duplicate a Template

Note

Currently, this API call only works with Apple templates

To duplicate a template, use the duplicate_template function. This will put the newly created template in the same project as the original:

import uareach as ua


client = ua.Reach('email', 'api_key')
response = ua.duplicate_template(client, template_id=12345)

Add Locations to Template

To add locations to a template, use the add_template_locations function:

import uareach as ua


client = ua.Reach('email', 'api_key')

# Minimal location object
location_1 = {
    "longitude": -122.374,
    "latitude": 37.618
}

# Full location object
location_2 = {
    "longitude": -80.1918,
    "latitude": 25.7617,
    "relevantText": "Hello loc 2",
    "streetAddress1": "address line #1",
    "streetAddress2": "address line #2",
    "city": "Miami",
    "region": "FL",
    "regionCode": 33101,
    "country": "US"
}

response = ua.add_template_locations(
    client, [location_1, location_2], template_id=12345
)

Remove Location from Template

To remove a location from a template, use the remove_template_location function:

import uareach as ua


client = ua.Reach('email', 'api_key')
response = ua.remove_template_location(
   client, 12345678, template_id=12345
)