The "http" action

Mechanic's "http" action allows you to send request to third-party APIs. This means Mechanic can be used to translate Shopify data and deliver it to other services.

Options

  • "method" – required, must be one of "get" , "post" , "put" , "patch" , "delete" 
  • "url" – required, must be a string starting with http:// or https:// 
  • "headers" – optional, may be a JSON object of header names and values
  • "body" – required for non-GET requests, may be a string to represent a plain request body, or may be a JSON object to be automatically serialized to JSON
  • "verify" – optional, may be set to false to disable SSL certificate verification
  • "error_on_5xx" – optional, may be set to true to have 5xx-class responses be considered action errors

Note: Mechanic will only automatically serialize to JSON if the Content-Type header is either blank or set to application/json.

Using the response

Action runs are performed in an entirely separate stage from task runs (see An introduction to runs), which means that you cannot perform third-party API requests within a task run. However, you can have your task subscribe to "mechanic/actions/perform" – a task configured this way will be reinvoked after its "http" action is performed, and the event data for that invocation will contain data from the HTTP response.

To learn more about this strategy, see Responding to action results.

Error handling

By default, Mechanic will only automatically retry errors that result from connection issues (e.g. network or DNS failures, etc). If a valid HTTP response is received, the run will be considered successful – even if a 5xx status code is received.

However, because 5xx responses should often be considered a retryable error, we support the "error_on_5xx" option. When set to true, this action will interpret any 5xx responses as a failure of the action itself, rather than considering the action successful just because an HTTP request was technically successfully performed.

To read more about how retries are handled for erroring actions, see An introduction to runs / Runs are retryable.

Basic authentication

To authenticate your request using the Authorization header, using the "Basic" authentication type, use something like this:

{% assign username = "guest" %}
{% assign password = "guest" %}
{% assign authorization_header = username | append: ":" | append: password | base64 | prepend: "Basic " %}

{% action "http" %}
  {
    "method": "get",
    "url": "https://jigsaw.w3.org/HTTP/Basic/",
    "headers": {
      "Authorization": {{ authorization_header | json }}
    }
  }
{% endaction %}

Example

This task sends the merchant's input to a third-party API that simply echoes it back. Great for testing. ;)

Subscriptions:

mechanic/user/text

Script:

{% action "http" %}
  {
    "method": "post",
    "url": "https://postman-echo.com/post",
    "body": {{ event.data | json }}
  }
{% endaction %}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.