Get a main image href from single Get A Product request

products
javascript

#1

So I’ve looked through the get Products request and understand how to cross-reference a product image href from the relationships using ids etc.

However I can’t see how to get the image href from the Get A Product request (https://api.moltin.com/v2/products/:id, or using the SDK, Moltin.Products.With(‘main_image’).Get(ID)).

Again, I do not need a list of products, just a single product. I’ve also created a main image relationship already.

I can’t see how to get an image href without also making a second api call to https://api.moltin.com
/v2/products, which I’d obviously like to avoid, or just to the /products endpoint, and pull out the product I want based on id, but that payload is way larger than i need to just display a single product.

Maybe I’m missing something…suggestions most welcome!


#2

Hey Chris,

I think you are looking to do the below. It will grab a product and its main image. https://docs.moltin.com/basics/includes

const MoltinGateway = require('@moltin/sdk').gateway
​
const Moltin = MoltinGateway({
  client_id: 'X'
})
​
const id = 'XXXX'
​
Moltin.Categories.With('products')
  .Get(id)
  .then(category => {
    // Do something
  })

Let me know if this does what you need in one request?

-George


#3

Hi George,

From the documentation the ‘id’ in that call refers to the product id - which would be perfect, however I can only get this endpoint to work by using a category id. And this of course returns the category info with a list of products, but the products don’t have a main image href.

Could you please confirm that this endpoint, when passed a product id (and not a category id), returns the product info like in the document you referenced? I believe I have correctly set up the relationships in the store via the dashboard…

Thanks

Chris


#4

@chris.eaton Using the With() method will return the requested resources in an included hash at the root of the API response body. It’ll look something like this:

Moltin.Products.With('main_image').Get(productID)

{  
  "data":{  
    "type":"product",
    "id":"25f60a7d-92f6-4308-8df8-5cc9afb03f88",
    "relationships":{  
      "main_image":{  
        "data":{  
          "type":"main_image",
          "id":"c6da9fce-8d23-48a0-9d9f-f72f87aaa206"
        }
      }
    }
  },
  "included":{  
    "main_images":[  
      {  
        "type":"main_image",
        "id":"c6da9fce-8d23-48a0-9d9f-f72f87aaa206",
        "link": {
          "href": "https://s3-eu-west-1.amazonaws.com/bkt-svc-files-cmty-api-moltin-com/6bc5ecbd-b9ea-4111-9856-5f453d1a4573/e508b3ea-9383-4e6f-b285-87e4e1d510e1.png"
         },
        "file_name": "Screen Shot 2018-01-03 at 12.54.16.png",
      }
    ]
  }
}

Check out this thread for some ways in which you can match up your product with the corresponding main_image record.

Hope this helps!


#5

Thanks Jonathan, that was the call I was originally making a few days ago but didn’t see the link.href.

Thanks for your help,

Chris


#6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.