Demonstration: Fetch an external configuration file, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Demonstration: Fetch an external configuration file

This task script demonstrates using the "http" action to retrieve external data, using a GET request. It also demonstrates saving the retrieved data to the Mechanic cache, where it can be used for future task runs.

Runs Occurs when a user manually triggers the task and Occurs when a Mechanic action is performed. Configuration includes save data to cache.

15-day free trial – unlimited tasks

Documentation

This task script demonstrates using the "http" action to retrieve external data, using a GET request. It also demonstrates saving the retrieved data to the Mechanic cache, where it can be used for future task runs.

Learn more about this technique: Can I load data from external sources?

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
mechanic/actions/perform
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
save data to cache (boolean)
Code
{% if event.topic == "mechanic/user/trigger" %}
  {% action "http" %}
    {
      "method": "get",
      "url": "http://example.com/config.txt"
    }
  {% endaction %}

  {% if options.save_data_to_cache__boolean %}
    {% comment %}
      For details on reading from the cache, see
      https://learn.mechanic.dev/platform/liquid/objects/cache
    {% endcomment %}

    {% log cached_config: cache.demonstration_config %}
  {% endif %}
{% elsif event.topic == "mechanic/actions/perform" %}
  {% if event.preview %}
    {% comment %}
      For details on this technique, see
      https://learn.mechanic.dev/core/tasks/previews
    {% endcomment %}

    {% capture action_json %}
      {
        "type": "http",
        "run": {
          "result": {
            "body": "sample config"
          }
        }
      }
    {% endcapture %}

    {% assign action = action_json | parse_json %}
  {% endif %}

  {% if action.type == "http" %}
    {% assign config = action.run.result.body %}

    {% log retrieved_config: config %}

    {% if options.save_data_to_cache__boolean %}
      {% action "cache", "set", "demonstration_config", config %}
    {% endif %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more