What's the right way to get a catalog_object_id?

I am selling a single item from an email driven workflow; once the user commits to buying, I generate a payment link, and send it to them. Initially I was using QuickPay, but I couldn’t find a way to create coupons with it, so I (after some trial and error) plugged in the item’s (variation) id to the link builder. What I have done though seems pretty brittle, and there must be a better way. Right?

I initially thought I’d be able to use the item id from the item’s url in the seller dashboard, but that didn’t work. So, after some experimentation, I managed to find the item via the api and dump it out:

        val items = client.catalogApi.searchCatalogObjects(
                   SearchCatalogObjectsRequest.Builder()
                         .objectTypes(listOf("ITEM"))
                   .build())
        val item = items.objects.first()
        logger.info("$item")

Examining the item in the log, I saw that there was a single variation listed, which was also a CatalogObject, and which had its own id; I plugged that id into the link builder, and it worked.

        val lineItem = OrderLineItem.Builder("1")
                .catalogObjectId("S_hardcoded_6")
                .build()
        val link = checkoutApi.createPaymentLink(
            CreatePaymentLinkRequest.Builder()
                .order(Order.Builder(configuration.square.location)
                    .lineItems(listOf(lineItem))
                    .build())
                .prePopulatedData(PrePopulatedData(email, null, null))
                .build()
            )

So here’s the question: is hardcoding the variation id safe? Will it change if I edit it? (I do not plan to add different types).

I can also get at the id with item.itemData.variations.first().id. Will there always be one variation?

Or is there generally a better approach where I can search for the item more directly and plug it directly into the CreatePaymentLinkRequest?

If your not going to change the item then yes you can hard code it if you’d like. Editing the item won’t change the Square generated catalog_object_variation_id. We don’t typically recommend hard coding the value. We recommend calling the Catalog API using either ListCatalog, SearchCatalogObjects, or SearchCatalogItems to bring in objects to pass to the request.

How are you identifying the item that’s being sold in your database? Do you have a specific ID for it. If so you can add a reference_id to the catalog object that you can search for when calling our APIs. :slightly_smiling_face: