<p>Since Global Sets are elements just like entries, users, etc., you can create API endpoints that are all about them:</p>



<pre><code>'globals/&lt;handle:\w+&gt;' =&gt; function($handle) {
    return [
        'elementType' =&gt; 'GlobalSet',
        'criteria' =&gt; ['handle' =&gt; $handle],
        'first' =&gt; true,
        'transformer' =&gt; function(\Craft\GlobalSetModel $globalSet) {
            return [
                'foo' =&gt; $globalSet-&gt;foo,
                'bar' =&gt; $globalSet-&gt;bar,
            ];
        }
    ];
}
</code></pre>

<p>You can also manually query for Global Sets, to include their data in an endpoint that’s tied to another element type:</p>

<pre><code>'my/other/endpoint' =&gt; [
    'elementType' =&gt; 'Entry',
    // ...
    'transformer' =&gt; function(\Craft\EntryModel $globalSet) {
        // ...

        // Fetch a global set
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::GlobalSet);
        $criteria-&gt;handle = 'myHandle';
        $myGlobalSet = $criteria-&gt;first();

        // Include the global set content in the response data
        return [
            // ...
            'foo' =&gt; $myGlobalSet-&gt;foo,
            'bar' =&gt; $myGlobalSet-&gt;bar,
        ]
    }
]
</code></pre>

<p>Craft also provides a couple methods for fetching Global Sets by ID or handle, so if that happens to be exactly what you need, feel free to use those:</p>

<ul>
<li><a href="https://craftcms.com/classreference/services/GlobalsService#getSetById-detail" rel="nofollow">craft()->globals->getSetById()</a></li>
<li><a href="https://craftcms.com/classreference/services/GlobalsService#getSetByHandle-detail" rel="nofollow">craft()->globals->getSetByHandle()</a></li>
</ul>

CC BY-SA 3.0
2016-04-18T11:50:39.843Z
2016-04-18T11:50:39.843Z
Brandon Kelly
<p>Is there an easy way to access global sets from an <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow">Element API</a> endpoint, so its content can be included in the JSON response?</p>

<plugin-elementapi><globals>
Accessing global sets from Element API
# Accessing global sets from Element API

(Asked by: username_0 on 2016-04-18)

Is there an easy way to access global sets from an [Element API](https://github.com/pixelandtonic/ElementAPI) endpoint, so its content can be included in the JSON response?

**Tags:** plugin-elementapi, globals

---

## 1 answers

### Answer by username_0 on 2016-04-18. Score: 4

Since Global Sets are elements just like entries, users, etc., you can create API endpoints that are all about them:
'globals/' => function($handle) {
    return [
        'elementType' => 'GlobalSet',
        'criteria' => ['handle' => $handle],
        'first' => true,
        'transformer' => function(\Craft\GlobalSetModel $globalSet) {
            return [
                'foo' => $globalSet->foo,
                'bar' => $globalSet->bar,
            ];
        }
    ];
}

You can also manually query for Global Sets, to include their data in an endpoint that’s tied to another element type:
'my/other/endpoint' => [
    'elementType' => 'Entry',
    // ...
    'transformer' => function(\Craft\EntryModel $globalSet) {
        // ...

        // Fetch a global set
        $criteria = craft()->elements->getCriteria(ElementType::GlobalSet);
        $criteria->handle = 'myHandle';
        $myGlobalSet = $criteria->first();

        // Include the global set content in the response data
        return [
            // ...
            'foo' => $myGlobalSet->foo,
            'bar' => $myGlobalSet->bar,
        ]
    }
]

Craft also provides a couple methods for fetching Global Sets by ID or handle, so if that happens to be exactly what you need, feel free to use those:

craft()->globals->getSetById()
craft()->globals->getSetByHandle()


---

