Element API: Get Entries Related to Category with Query Parameter I'm using the Element API Plugin to create an API, with which I want to get entries related to one category, but dynamically with the category set in the Url.
What I have so far in the config/element-api.php:
&lt;?php

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

return [
  'endpoints' =&gt; [
    'api/projects.json' =&gt; function() {
      $projectCategory = \Craft::$app-&gt;request-&gt;getQueryParam('projectCategory');

      // dump for test purpose
      var_dump($projectCategory);

      return [
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
          'section' =&gt; 'projects',
          'relatedTo' =&gt; &quot;{$projectCategory}&quot;,
        ],
        'elementsPerPage' =&gt; 2,
        'transformer' =&gt; function(Entry $entry) {
          return [
            'title' =&gt; $entry-&gt;title,
            'url' =&gt; $entry-&gt;url,
            'jsonUrl' =&gt; UrlHelper::url(&quot;projects/{$entry-&gt;id}.json&quot;),
            'thumbnail' =&gt; $entry-&gt;projectThumbnail,
            'description' =&gt; $entry-&gt;projectDescription,
            'credits' =&gt; $entry-&gt;projectCredits,
          ];
        },
      ];
    },
  ],
];

When I go to http://domain.test/api/projects.json?projectCategory=category, I get the right value dumped, but the relatedTo doesn't seem to work. I get zero entries, although there are some related to a specific category.
Am I doing something wrong here?
Are you passing the projectCategory parameter as a string or ID? In any case, the relatedTo parameter will receive a string in your code, which might trip up the query. If the parameter is passed as an ID, passing it as an integer instead of a string might be enough:
'relatedTo' =&gt; (int) $projectCategory,

However, it might be cleaner to get the actual category element based on the ID and pass that to relatedTo. This will also allow you to check if the specified category actually exists (and optionally return an HTTP error for invalid parameters).
$criteria = ['section' =&gt; 'projects'];
$projectCategory = \Craft::$app-&gt;request-&gt;getQueryParam('projectCategory');
// this assumes the category is passed as an ID
$category = \craft\elements\Category::find()-&gt;id((int) $projectCategory)-&gt;one();
if ($category) {
    $criteria['relatedTo'] = $category;
}