Looking to make request using the jsonapi module to GET all urls that point to the images optimized version of the image.

If I use the Drupal front-end with Twig and view my page and inspect the images on the page I get something like this


If I make a request to my api endpoint and drill down to the image field it does not have the included image style url. ```

"data": {
    "type": "file--file",
    "id": "b74b73fb-19fd-48a2-ac91-8a7aacd7fe18",
    "attributes": {
        "fid": 2,
        "uuid": "b74b73fb-19fd-48a2-ac91-8a7aacd7fe18",
        "langcode": "en",
        "filename": "MYIMAGE.png",
        "uri": "public://promo-backgrounds/MYIMAGE.png",
        "filemime": "image/png",
        "filesize": 37624,
        "status": true,
        "created": 1508723420,
        "changed": 1508723610,
        "url": "/sites/default/files/promo-backgrounds/MYIMAGE.png"
    "relationships": {
        "uid": {
            "data": {
                "type": "user--user",
                "id": "b044b880-ab4e-495b-b2d1-6a4fe58dc609"
            "links": {
                "self": "http://local.mysite.com/api/file/file/b74b73fb-19fd-48a2-ac91-8a7aacd7fe18/relationships/uid?language=en",
                "related": "http://local.mysite.com/api/file/file/b74b73fb-19fd-48a2-ac91-8a7aacd7fe18/uid?language=en"
    "links": {
        "self": "http://local.mysite.com/api/file/file/b74b73fb-19fd-48a2-ac91-8a7aacd7fe18?language=en"
"links": {
    "self": "http://local.mysite.com/api/file/file/b74b73fb-19fd-48a2-ac91-8a7aacd7fe18?language=en"



How can one get access the image style uri using the jsonapi as my api server?

  • What is the exact answer? Please post
    – Ram_T
    Sep 12, 2018 at 7:54

1 Answer 1


I would do it

create file: test_api.info.yml

name: Test API
type: module
description: Custom RESTful API module with GET/POST/PUT/DELETE etc.. method examples.
author: Minnur Yunusov
core: 8.x

create file: test_api.routing.yml

  path: 'my-api/get/{nid}'
  defaults: { _controller: '\Drupal\test_api\Controller\TestAPIController::getpara_example' }
  methods:  [GET]
    _access: 'TRUE'   

create file: /src/Controller/TestAPIController.php


 * @file
 * Contains \Drupal\test_api\Controller\TestAPIController.

namespace Drupal\test_api\Controller;

use Drupal\Core\Controller\ControllerBase;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\user\Entity\User;

 * Controller routines for test_api routes.
class TestAPIController extends ControllerBase {

   * Callback for `my-api/get.json` API method.
  public function getpara_example($nid, Request $request ) {

    //get node;

    //get image uri
    $image =$node->field_image->entity->getFileUri();

    $style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');

    $url = $style->buildUrl($image);

    $response['nid'] = $nid;
    $response['uri'] = file_create_url($image);
    $response['image_original'] = file_create_url($image);
    $response['image_style_thumbnail'] =file_create_url($url);

    return new JsonResponse( $response );


test: url: localhost/drupal8/my-api/get/30 method: get

note: 30 is nid.


  "nid": "30",
  "uri": "public://2017-10/generateImage_1RwT72.png",
  "image_original": "http://jeremiselxi.260mb.net/drupal8/sites/default/files/2017-10/generateImage_1RwT72.png",
  "image_style_thumbnail": "http://jeremiselxi.260mb.net/drupal8/sites/default/files/styles/thumbnail/public/2017-10/generateImage_1RwT72.png?itok=Dzg02oew"
  • 1
    Hey, thanks for your response. Shortly after I posted this I found a module that addresses this issue. drupal.org/project/consumer_image_styles I hope this helps others looking for a solution.
    – TikaL13
    Nov 4, 2017 at 20:00
  • Where should I create those files? Please tell us clearly
    – Ram_T
    Sep 12, 2018 at 7:55
  • these files must be created inside the / modules / test_api folder. Sep 13, 2018 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.