How to add query parameters to a CacheableJsonResponse's cache? I have an API which returns various json, like terms of a vocab, nodes of a type, with specific fields and so on.

Now, in my API I am getting the requested language like so:

$request = \Drupal::request();
$lang = $request-&gt;query-&gt;get('lang', 'en');


And load the respective translations of the entities.

Now, how can I add that $lang parameter as a cache indicator? So that:

/myapi/nodes?lang=en


creates another cache than:

/myapi/nodes?lang=klingon


Also, is that the right way to deliver language specific content in an API?
Add a cache context for the query parameter to the response:

$cache_metadata = (new CacheableMetadata())-&gt;addCacheContexts(['url.query_args:lang']);
$response-&gt;addCacheableDependency($cache_metadata);




About the second question, when this issue is resolved you can use core REST to deliver translated entities: https://www.drupal.org/node/2135829