Demonstration: Generating a file and uploading it to Shopify, with Mechanic.

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

Demonstration: Generating a file and uploading it to Shopify

This task demonstrates how one might use the Files action to generate a file, using the fileCreate GraphQL mutation to transfer the resulting file to Shopify for storage.

Runs Occurs when a user manually triggers the task and Occurs when a Mechanic action is performed.

15-day free trial – unlimited tasks

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
Code
{% if event.topic == "mechanic/user/trigger" %}
  {% action "files" %}
    {
      "invoice.pdf": {
        "pdf": {
          "html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/actions/perform" %}
  {% if event.preview %}
    {% capture action_json %}
      {
        "type": "files",
        "run": {
          "result": {
            "invoice.pdf": {
              "url": "https://usemechanic.com/file/0000/invoice.pdf"
            }
          }
        }
      }
    {% endcapture %}

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

  {% if action.type == "files" %}
    {% action "shopify" %}
      mutation {
        fileCreate(
          files: [
            {
              originalSource: {{ action.run.result["invoice.pdf"].url | json }}
            }
          ]
        ) {
          files {
            fileStatus
            fileErrors {
              code
              details
              message
            }
          }
          userErrors {
            code
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more