square / square-java-sdk

Java client library for the Square API

Home Page:https://developer.squareup.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get Catalog Item Image URL?

zeppelinux opened this issue · comments

I uploaded the image for the item and can see it's id is returned when I query Catalog Items. How Do i get the image URL?
Screenshot 2023-05-04 at 6 59 07 PM

Hey @zeppelinux !

Very stoked to see you actively building with Square :)

There are few different ways for you to retrieve the url for image, and depending on your implementation, it might choose how you want to proceed.

If you decide to query CatalogItems and want to get their image data, you can use the SearchCatalogObjects endpoint and set the option to include_related_objects.

You will need to do some information stitching from your id's that are returned in the item, to the id's that are returned in the related objects for objects of type Image

LinkedList<String> objectTypes = new LinkedList<>();
objectTypes.add("ITEM");

SearchCatalogObjectsRequest body = new SearchCatalogObjectsRequest.Builder()
  .objectTypes(objectTypes)
  .includeRelatedObjects(true)
  .build();

catalogApi.searchCatalogObjectsAsync(body)
  .thenAccept(result -> {
    System.out.println("Success!");
  })
  .exceptionally(exception -> {
    System.out.println("Failed to make the request");
    System.out.println(String.format("Exception: %s", exception.getMessage()));
    return null;
  });
// response example
{
  "objects": [
    {
      "type": "ITEM",
      "id": "Y5SJ2MYDCGIOPT7WRDZ4ETBR",
      "updated_at": "2023-02-28T17:13:55.899Z",
      "created_at": "2022-07-29T19:05:22.196Z",
      "version": 1677604435899,
      "is_deleted": false,
      "present_at_all_locations": true,
      "item_data": {
        "name": "Watermelon Teapot",
        "description": "A delightful addition to your Sunday picnic. This teapot perfectly holds the correct amount of water for brewing your favorite leaves.",
        "is_taxable": true,
        "variations": [
          {
            "type": "ITEM_VARIATION",
            "id": "4EDJCEE26GNYLWDFLBXSEZNL",
            "updated_at": "2022-07-29T19:05:23.803Z",
            "created_at": "2022-07-29T19:05:22.196Z",
            "version": 1659121523803,
            "is_deleted": false,
            "present_at_all_locations": true,
            "item_variation_data": {
              "item_id": "Y5SJ2MYDCGIOPT7WRDZ4ETBR",
              "name": "Regular",
              "ordinal": 0,
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 7500,
                "currency": "USD"
              },
              "location_overrides": [
                {
                  "location_id": "LHJ1ZXJ8YSV8W",
                  "track_inventory": true
                }
              ],
              "track_inventory": true,
              "inventory_alert_type": "LOW_QUANTITY",
              "inventory_alert_threshold": 3,
              "sellable": true,
              "stockable": true
            }
          }
        ],
        "product_type": "REGULAR",
        "image_ids": [
          "HILIYDKSUNYEHUZJ2DU6N26V",
          "X2ES2R7TFWHAB4U3EAIBX7QU"
        ],
        "description_html": "<p>A delightful addition to your Sunday picnic. This teapot perfectly holds the correct amount of water for brewing your favorite leaves.</p>",
        "description_plaintext": "A delightful addition to your Sunday picnic. This teapot perfectly holds the correct amount of water for brewing your favorite leaves."
      }
    }
  ],
  "related_objects": [
    {
      "type": "IMAGE",
      "id": "HILIYDKSUNYEHUZJ2DU6N26V",
      "updated_at": "2022-07-29T19:05:25.041Z",
      "created_at": "2022-07-29T19:05:25.041Z",
      "version": 1659121525041,
      "is_deleted": false,
      "present_at_all_locations": true,
      "image_data": {
        "url": "<image.jpg>",
        "caption": "A picture of a watermelon teapot."
      }
    },
    {
      "type": "IMAGE",
      "id": "X2ES2R7TFWHAB4U3EAIBX7QU",
      "updated_at": "2022-07-29T19:05:26.311Z",
      "created_at": "2022-07-29T19:05:26.311Z",
      "version": 1659121526311,
      "is_deleted": false,
      "present_at_all_locations": true,
      "image_data": {
        "url": "<image.jpg",
        "caption": "A picture of a watermelon teapot."
      }
    }
  ],
  "latest_time": "2023-05-01T20:49:56.21Z"
}

Some more information about the searchCatalogObjects

Another way you could get image data is by using RetrieveCatalogObject

Images in the Catalog are an Object like everything else in the catalog, so if you want to make separate API calls once you have the image ID, you can look up Image from that API directly to get the url as well.

Lastly, if you have a list of Image Ids, and you want to make a request to get the image data for all of those objects, you can use Batch retrieve catalog objects

LinkedList<String> objectIds = new LinkedList<>();
objectIds.add("HILIYDKSUNYEHUZJ2DU6N26V");
objectIds.add("X2ES2R7TFWHAB4U3EAIBX7QU");

BatchRetrieveCatalogObjectsRequest body = new BatchRetrieveCatalogObjectsRequest.Builder(objectIds)
  .build();

catalogApi.batchRetrieveCatalogObjectsAsync(body)
  .thenAccept(result -> {
    System.out.println("Success!");
  })
  .exceptionally(exception -> {
    System.out.println("Failed to make the request");
    System.out.println(String.format("Exception: %s", exception.getMessage()));
    return null;
  });

Hope this helps!

I'm going to close this issue because it's not a bug or anything with the Java SDK.

If you continue to have questions about our API Platform while building with Square, we ask you post these types of questions on our Developer Forums - This forum is monitored by Square Employees, and actively responded to by our Developer Success Engineers. We also recommend joining our community Slack Channel where you can get also get more help from developer success or even other developers in the community!

Cheers :)

Thanks @zenmasterjobo, i also found that:

ListCatalogResponse resp = api.listCatalog(null, "ITEM,CATEGORY,IMAGE", null);

returns everything I need (so far :)