<p>Indeed as <a href="https://craftcms.stackexchange.com/users/5557/robin-schambach">@robin</a> pointed out, I couldn't figure out a nice way to making custom data outputs with Element API (Not related to a ElementType, like available Sites/Languages or available sections). What I did was:</p>

<p>I created a <a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module" rel="nofollow noreferrer">custom module</a>, in this module I was able to register routes like:</p>

<pre><code>        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
            function (RegisterUrlRulesEvent $event) {

                $event-&gt;rules['api/languages'] = '/endpoints/endpoints/languages';
            }
        );
</code></pre>

<p>And in the action of the module. (Still is a rough version, also needs to handle possible errors and that kind of stuff)</p>

<pre><code>public function actionLanguages()
{
    $sites = Craft::$app-&gt;getSites()-&gt;getAllGroups();

    $languages = Craft::$app-&gt;getSites()-&gt;getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

    return $this-&gt;asJson($this-&gt;formatApiResponse($languages));
}
</code></pre>

<p>I don't know if it's the nicest way (Still open to suggestions) but this gave me the freedom I needed to enhance my Element API end-points.</p>

CC BY-SA 4.0
2018-06-12T15:32:06.090Z
2018-06-12T15:32:06.090Z
Jorge Anzola
<p>For example, an endpoint to retrieve all the languages available on the website</p>

<pre><code>'endpoints' =&gt; [   
   'languages' =&gt; function () {

            $sites = Craft::$app-&gt;getSites()-&gt;getAllGroups();

            $languages = Craft::$app-&gt;getSites()-&gt;getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

            Craft::dd($languages); // output this in a nice json

            return $languages;
        },
    ]
</code></pre>

<p>Or an endpoint to retrieve all the available sections</p>

<pre><code>    'sections' =&gt; function () {

            $sections = Craft::$app-&gt;getSections()-&gt;getAllSections();

            Craft::dd($sections); // output this in a nice json
    },
</code></pre>

CC BY-SA 4.0
2018-06-11T09:58:18.270Z
I can't think of a clean way to do this, it would be easier to create a custom controller for it
Robin Schambach
CC BY-SA 4.0
2018-06-11T10:53:24.160Z
You're right, I've made a custom module for it and it was super simple. I think I'll use this in combination with Element API.
Jorge Anzola
CC BY-SA 4.0
2018-06-11T18:29:34.910Z
Would you mind adding your solution as an official answer in case it helps someone in the future, @JorgeAnzola?
Brad Bell
CC BY-SA 4.0
2018-06-12T15:23:45.390Z
Yes! I will do that.
Jorge Anzola
<plugin-elementapi>
Can I output custom or specific data (Not related to an elementType) on Element API?
# Can I output custom or specific data (Not related to an elementType) on Element API?

(Asked by: username_0 on 2018-06-11)

For example, an endpoint to retrieve all the languages available on the website
'endpoints' => [   
   'languages' => function () {

            $sites = Craft::$app->getSites()->getAllGroups();

            $languages = Craft::$app->getSites()->getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

            Craft::dd($languages); // output this in a nice json

            return $languages;
        },
    ]

Or an endpoint to retrieve all the available sections
    'sections' => function () {

            $sections = Craft::$app->getSections()->getAllSections();

            Craft::dd($sections); // output this in a nice json
    },

**Tags:** plugin-elementapi

> **Comments:**
> **username_1**: I can't think of a clean way to do this, it would be easier to create a custom controller for it  
> **username_0**: You're right, I've made a custom module for it and it was super simple. I think I'll use this in combination with Element API.  
> **username_2**: Would you mind adding your solution as an official answer in case it helps someone in the future, @JorgeAnzola?  
> **username_0**: Yes! I will do that.  

---

## 1 answers

### Answer by username_0 on 2018-06-12. Score: 2 | Accepted answer

Indeed as @robin pointed out, I couldn't figure out a nice way to making custom data outputs with Element API (Not related to a ElementType, like available Sites/Languages or available sections). What I did was:
I created a custom module, in this module I was able to register routes like:
        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
            function (RegisterUrlRulesEvent $event) {

                $event->rules['api/languages'] = '/endpoints/endpoints/languages';
            }
        );

And in the action of the module. (Still is a rough version, also needs to handle possible errors and that kind of stuff)
public function actionLanguages()
{
    $sites = Craft::$app->getSites()->getAllGroups();

    $languages = Craft::$app->getSites()->getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

    return $this->asJson($this->formatApiResponse($languages));
}

I don't know if it's the nicest way (Still open to suggestions) but this gave me the freedom I needed to enhance my Element API end-points.


---

