Using in your Templates

To capture API data on-demand without saving it as an entry, you can utilize Twig templates with the following code. APIs are cached for performance (default to 60 seconds), and you can adjust this duration through a tag parameter or in the plugin settings.

{% set apiId = '#apiId' %}

{% set apiUrl = craft.easyApi.getApiUrl(apiId) %}

    {# Your template code to modify the target URL goes here #}

{% set apiResponse = craft.easyApi.runApi(apiId, apiUrl) %}

{% for node in api %}
    {# Your template code goes here #}
{% endfor %}

Parameters

  • apiId (string, required) - Id of the API.
  • apiUrl (string, optional) - The URL of the API being called, only needs to be sent when the URL needs to be modified at run time.

Example template code

<?xml version="1.0" encoding="UTF-8" ?>
<entries>
    <entry>
        <title>Monday</title>
        <item>
            <title format="html">Event 1</title>
            <type>All-day</type>
        </item>
    </entry>
    <entry>
        <title>Tuesday</title>
        <item>
            <title format="html">Event 2</title>
            <type>Half-day</type>
        </item>
    </entry>
</entries>

With the above example XML, we would use the following Twig code to loop through each entry to extract its data.

{% set apiId = '#apiId' %}
{% set api = craft.easyApi.runApi(apiId) %}

{% for node in api %}
    Title: {{ node.title }}
    Item: {{ node.item.title['@'] }}
    Item Format: {{ node.item.title['@format'] }}
    Type: {{ node.item.type }}
{% endfor %}

{# Producing the following output #}
Title: Monday
Item: Event 1
Item Format: html
Type: All-day

Title: Tuesday
Item: Event 2
Item Format: html
Type: Half-day
There's a special case for XML-based apis, which is illustrated above when attributes are present on a node. To retrieve the node value, use ['@'], and to fetch the attribute value, use ['@attribute_name'].
← Creating Your API Feed