Docs / Strand / templating/examples

Templating Examples

Real-world examples of using Jinja2 and JSONPath in workflows.

Example 1: User Lookup and Email

Workflow:

code

[HTTP: user_lookup] -> [HTTP: send_email]

user_lookup URL:

jinja

https://api.example.com/users/{{ payload.user_id }}

send_email Body (Direct Connection):

json

{
  "to": "{{ payload.email }}",
  "subject": "Welcome {{ payload.first_name }}",
  "body": "Hello {{ payload.full_name }}"
}
Direct Connection

Since send_email is directly connected to user_lookup, use payload instead of steps.user_lookup.output_payload.

Example 2: Filter Active Users

Workflow:

code

[Filter: active_only] -> [Process]

Filter Condition:

jinja

payload.active == true and payload.verified == true

Example 3: Transform User Data

Transform Mapping:

json

{
  "full_name": "{{ payload.first_name }} {{ payload.last_name }}",
  "email_lower": "{{ payload.email | lower }}",
  "created_year": "{{ payload.created_at | truncate(4, True, '', 0) }}"
}

Example 4: JSONPath Array Processing

Extract all IDs:

jinja

{{ payload | jsonpath('$.items[*].id') }}

Filter active items:

jinja

{{ payload | jsonpath('$.users[?(@.active == true)].email') }}

Count items:

jinja

{{ payload | jsonpath('$.items.`len`') }}

Example 5: Conditional API Call

URL with condition:

jinja

{{ 'https://api.prod.com' if variables.env == 'production' else 'https://api.dev.com' }}/endpoint

Headers with auth:

json

{
  "Authorization": "Bearer {{ vault.api_token }}",
  "X-User-ID": "{{ payload.user_id }}"
}
Using Vault for Secrets

API tokens and other sensitive values should be stored in the Global Vault, not in Workflow Variables. The vault encrypts values at rest.

Example 6: Combining Multiple Steps

Transform combining data:

json

{
  "user": {{ steps.user_lookup.output_payload | tojson }},
  "preferences": {{ steps.preferences_lookup.output_payload | tojson }},
  "metadata": {
    "source": "{{ initial.meta.trigger_source }}",
    "workflow": "{{ initial.meta.workflow_name }}"
  }
}

Example 7: Nested Workflow Call

Flow Call data:

json

{
  "user_id": "{{ steps.user_lookup.output_payload.id }}",
  "action": "process",
  "context": {
    "source_workflow": "main_workflow",
    "timestamp": "{{ meta.received_at }}"
  }
}

Example 8: Error Handling

Safe access with defaults:

jinja

{{ steps.user_lookup.output_payload.email | default('[email protected]') }}
{{ steps.user_lookup.output_payload.role | default('user') }}

Example 9: String Manipulation

Build full name:

jinja

{{ payload.first_name }} {{ payload.last_name }}

Format date:

jinja

{{ payload.created_at | truncate(10) }}

Example 10: Complex JSONPath

Filter and extract:

jinja

{{ payload | jsonpath('$.users[?(@.active == true & @.role == "admin")].email') }}

Arithmetic:

jinja

{{ payload | jsonpath('$.price * 1.1') }}