Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-17T13:29:12.653Z
Answers.Text: 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.
Answers.UserDisplayName: nfq
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-17T17:37:55.397Z
Answers.Text: @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...
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-17T02:11:27.647Z
Answers.LastActivityDate: 2020-07-17T02:11:27.647Z
Answers.UserDisplayName: Jalen Davenport
Body: <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>
  
Tags: <php><plugin-elementapi>
Title: Get currentSite.handle in Element API
ThreadText: # 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:
  
  ```
  <?php
  
  use craft\elements\Entry;
  use craft\helpers\UrlHelper;
  use craft\services\Sites;
  
  return [
      'defaults' => [
          '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](https://docs.craftcms.com/api/v3/craft-services-sites.html#currentsite) 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...  
  
  ---
  
  