<p>The <a href="https://docs.craftcms.com/api/v3/craft-services-sites.html#currentsite" rel="nofollow noreferrer"><code>currentSite</code> property</a> from the Sites service is what you're looking for. Here's how you would use it:</p>
<pre><code>'api/search.json' =&gt; function() {

    $siteHandle = Craft::$app-&gt;getSites()-&gt;currentSite-&gt;handle;

    ...

    $criteria = [
        'site' =&gt; $siteHandle,
        ...
    ];

    return [
        ...
    ];
},
</code></pre>

CC BY-SA 4.0
2020-07-17T13:29:12.653Z
Thanks for the speedy reply. So, in essence this should work. One question remains: the JS for the autocomplete somehow needs to know what the current site is. How would one pass `siteHandle` to Javascript? Right now I'm doing it using the window object, but it feels a bit hackish.
nfq
CC BY-SA 4.0
2020-07-17T17:37:55.397Z
@nfq honestly not sure why you'd need the `siteHandle` for the autocomplete functionality, but I typically pass that and any other global info I need via the window object like you're describing...
Jalen Davenport
CC BY-SA 4.0
2020-07-17T02:11:27.647Z
2020-07-17T02:11:27.647Z
Jalen Davenport
<p>I need to get the currentSite in Element API. How can this be achieved? Is it as a slug in the endpoint URL? or as a Query param? Here is the current code:</p>
<pre><code>&lt;?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;
use craft\services\Sites;

return [
    'defaults' =&gt; [
        'resourceKey' =&gt; 'suggestions',
        'cache' =&gt; false,
        'paginate' =&gt; false,
        'pretty' =&gt; true,
    ],
    'endpoints' =&gt; [
        'api/search.json' =&gt; function() {

        $searchQuery = Craft::$app-&gt;request-&gt;getQueryParam('query');
        $site = Craft::$app-&gt;request-&gt;getQueryParam('site');

        $sectionHandles = [
            'pages',
            'news',
        ];

        // Search criterias
        $criteria = [
            'section' =&gt; $sectionHandles,
            'search' =&gt; $searchQuery,
            'site' =&gt; $site,
            'orderBy' =&gt; 'score',
            'limit' =&gt; 10,
        ];

            return [
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; $criteria,
                'paginate' =&gt; false,

            'transformer' =&gt; function(craft\elements\Entry $entry) {

                return [
                    'title' =&gt; $entry-&gt;title,
                    'url' =&gt; $entry-&gt;url,
                ];
            },

            ];
        },
    ]
];
</code></pre>

<php><plugin-elementapi>
Get currentSite.handle in Element API
# Get currentSite.handle in Element API

(Asked by: username_0 on 2020-07-17)

I need to get the currentSite in Element API. How can this be achieved? Is it as a slug in the endpoint URL? or as a Query param? Here is the current code:
 [
        'resourceKey' => 'suggestions',
        'cache' => false,
        'paginate' => false,
        'pretty' => true,
    ],
    'endpoints' => [
        'api/search.json' => function() {

        $searchQuery = Craft::$app->request->getQueryParam('query');
        $site = Craft::$app->request->getQueryParam('site');

        $sectionHandles = [
            'pages',
            'news',
        ];

        // Search criterias
        $criteria = [
            'section' => $sectionHandles,
            'search' => $searchQuery,
            'site' => $site,
            'orderBy' => 'score',
            'limit' => 10,
        ];

            return [
                'elementType' => Entry::class,
                'criteria' => $criteria,
                'paginate' => false,

            'transformer' => function(craft\elements\Entry $entry) {

                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                ];
            },

            ];
        },
    ]
];

**Tags:** php, plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2020-07-17. Score: 2 | Accepted answer

The currentSite property from the Sites service is what you're looking for. Here's how you would use it:
'api/search.json' => function() {

    $siteHandle = Craft::$app->getSites()->currentSite->handle;

    ...

    $criteria = [
        'site' => $siteHandle,
        ...
    ];

    return [
        ...
    ];
},

> **Comments:**
> **username_0**: Thanks for the speedy reply. So, in essence this should work. One question remains: the JS for the autocomplete somehow needs to know what the current site is. How would one pass `siteHandle` to Javascript? Right now I'm doing it using the window object, but it feels a bit hackish.  
> **username_1**: @username_0 honestly not sure why you'd need the `siteHandle` for the autocomplete functionality, but I typically pass that and any other global info I need via the window object like you're describing...  

---

