How I can add an optional parameter in REST API URL How I can add an optional parameter in drupal 8 REST API URL

"canonical" = "/content/get/{id}"


But I have to add value for id here, How I can make this id optional
I experienced the same issue and solved it by adding the current request from the request stack to my resource:

RestBase.php

&lt;?php

namespace Drupal\api\Plugin\rest;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

class RestBase extends ResourceBase {

  /**
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;
  /**
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * 
   * @param array $configuration
   * @param type $pluginId
   * @param type $pluginDefinition
   * @param array $serializerFormats
   * @param \Drupal\api\Plugin\rest\resource\LoggerInterface $logger
   * @param \Drupal\api\Plugin\rest\resource\AccountProxyInterface $currentUser
   */
  public function __construct(array $configuration, $pluginId, $pluginDefinition, array $serializerFormats, LoggerInterface $logger, AccountProxyInterface $currentUser, Request $currentRequest) {
    parent::__construct($configuration, $pluginId, $pluginDefinition, $serializerFormats, $logger);

    $this-&gt;currentUser = $currentUser;
    $this-&gt;currentRequest = $currentRequest;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static(
        $configuration, 
        $pluginId, 
        $pluginDefinition, 
        $container-&gt;getParameter('serializer.formats'), 
        $container-&gt;get('logger.factory')-&gt;get('rest'), 
        $container-&gt;get('current_user'),
        $container-&gt;get('request_stack')-&gt;getCurrentRequest()
    );
  }

  /**
   * 
   * @param array $message
   * @param integer $cache
   * @return ResourceResponse
   */
  protected function getResponse($message, $cache = 0) {
    $build = [
      '#cache' =&gt; [
        'max-age' =&gt; $cache,
      ]
    ];

    $response = new ResourceResponse($message);
    $response-&gt;addCacheableDependency($build);

    return $response;
  }

}


TestResource.php (the actual REST resouce)

&lt;?php

namespace Drupal\api\Plugin\rest\resource;

use Drupal\rest\ResourceResponse;
use Drupal\api\Plugin\rest\RestBase;

/**
 * Test resource.
 * 
 * @RestResource(
 *   id = "test_resource",
 *   label = @Translation("Test API Resource"),
 *   uri_paths = {
 *     "canonical" = "/api/test-resource"
 *   }
 * )
 */
class TestResource extends RestBase {

  /**
   * 
   * @return ResourceResponse
   */
  public function get() {
    if (!$this-&gt;currentUser-&gt;hasPermission('use ' . $this-&gt;pluginId . ' service')) {
      throw new AccessDeniedHttpException('Access denied.');
    }

    $id = $this-&gt;currentRequest-&gt;get('id');

    $message = [
      'message' =&gt; 'Hello, you called service: ' . $this-&gt;pluginId,
      'id' =&gt; $id,
    ];

    return parent::getResponse($message);
  }

}


In my own base class RestBase I added the current request from the request stack (besides the AccountProxy for permission purposes) by overriding the create method of the ResourceBase class. You can then access query parameters in your REST resource by calling the get($key) method on the request object. 

You will need to pass your parameters in the API URI like so: http://localhost/api/test-resource?_format=&lt;your-format&gt;&amp;&lt;parameter1&gt;=&lt;value1&gt;. Considering this example API URI you would access parameter1 via $value1 = $this-&gt;currentRequest-&gt;get('parameter1').

I guess that it is not possible to have slugs in your resources at the moment. If someone knows how please do tell :).