External API

callAPI

Description

Call any URL directly in javascript.

Usage

callAPI(<HTTP METHOD>, <URL>, <OPTIONS>)

Options

NameTypeDescription
dataHashBody of the request
usernameStringBasic auth username
passwordStringBasic auth password
headersHashHTTP headers

data is automatically converted to JSON, username and password are automatically base64 encoded.

Example

{% action "Create Stripe subscription" %}
  var email   = params.stripeEmail, token = params.stripeToken;
  var payment = false;

  if (token) {
    payment = callAPI('POST', 'https://api.stripe.com', {      
      username: site.metafields.stripe.api_key,
      data: {
        email:  email,
        source: token,
        plan:   'weekly_box'
      }
    });
  }

  setProp('payment', !!payment);

{% endaction %}

{% if payment %}
  <h1>SUCCESS!</h1>
{% else %}
  <h1>FAILURE!</h1>
{% endif %}