question

brtinney avatar image
brtinney asked Jonathan Ryan Grice commented

How to associate categories/modifier groups with items

So, given the following category:

curl --request GET \
     --url 'https://sandbox.dev.clover.com/v3/merchants/{MID}/categories/01RK004VVF99T?expand=items' \
     --header 'Accept: application/json' \
     --header 'Authorization: Bearer {TOKEN}'
{"id": "01RK004VVF99T", "name": "Pizza", "sortOrder": 1, "items": {"elements": [ ]}}

And the following item:

curl --request GET \
     --url 'https://sandbox.dev.clover.com/v3/merchants/{MID}/items/ZT1J6B5S6R1SY?expand=categories' \
     --header 'Accept: application/json' \
     --header 'Authorization: Bearer {TOKEN}'
{"id": "ZT1J6B5S6R1SY", "hidden": true, "available": true, "autoManage": false, "name": "Personal Pizza", "price": 900, "priceType": "FIXED", "defaultTaxRates": true, "isRevenue": true, "categories": {"elements": [ ]}, "modifiedTime": 1659913743000}

Why does the following association between the two do nothing?

curl --request POST \
     --url https://sandbox.dev.clover.com/v3/merchants/{MID}/category_items \                         
     --header 'Accept: application/json' \
     --header 'Authorization: Bearer {TOKEN}' \
     --data-raw '{"elements":[{"category":{"id":"01RK004VVF99T"},"item":{"id":"ZT1J6B5S6R1SY"}}]}'
{}

I can't seem to make modifier group associations work either. The actual API documentation is not great, but I'm following what I found here: https://docs.clover.com/docs/managing-categories#associating-items-with-a-category (and other posts in this community).

Note that the response code for the call is 200, no matter what garbage I throw at it, including malformed JSON.

REST APIInventory
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Jonathan Ryan Grice avatar image
Jonathan Ryan Grice answered Jonathan Ryan Grice commented

To associate an item with a category, seems you are doing this using CURL at the command line, it would look like for example:


curl -v \
https://sandbox.dev.clover.com/v3/merchants/{mid}/category_items \
-H "Content-Type: application/json" \
-H "Authorization: Bearer {token}" \
-A "User-Agent" \
--data-raw "{\"elements\":[{\"category\":{\"id\":\"D0KPWD97FHKYG\"}, \"item\":{\"id\":\"GX1DNHYPBPM58\"}}]}"


POST is implied so it's not necessary

to see any errors make sure you put -v or --verbose somewhere in there.

Not necessary to specify a switch for the URL.

Set your Content-Type and Authorization header.

Set a User-Agent in case Clover's server blocks the default command line agent.

Use quotation marks instead of apostrophes and escape quotation marks inside the JSON.
Change the category and item ids accordingly.

3 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks, and for reference, the only difference that actually matters in your example is the Content-Type: application/json header. Apparently I had copied the Accept header from somewhere instead.

So now that I have cURL working (which was just illustrative), now can you explain how to get it working via Node?

The API docs are sparse on this, but I assume I'm to do something like the following:

const sdk = require('api')('@clover-platform/v3#cja48j514l5babyqi')

sdk.auth(token)
sdk['category.CreateOrDeleteCategoryItems'](
  {
    elements: [
      {
        item: { id: 'ZT1J6B5S6R1SY' },
        category: { id: '01RK004VVF99T' }
      }
    ]
  },
  { mId: mid }
)
  .then((res) => console.log(res))
  .catch((err) => console.error(err))

However, that doesn't work, which is why I started down the cURL path.

My guess is that because the OpenAPI spec is missing a requestBody. https://dash.readme.io/api/v1/api-registry/cja48j514l5babyqi

I'm guessing that's also why no body appears in the documentation, either. So the api package either ignores the request body, or, if nothing else, uses the wrong Content-Type (which would result in the behavior seen above).

Can this be fixed, or do I have to resort to a separate (probably axios-based) REST call amongst all the other calls that work fine using the documented api package?

P.S. The documentation package seems to have stripped the . out of the operationId, so all of the Node examples are wrong now (this was a recent change when going to cja48j514l5babyqi).

Edit: Updating api to 4.5.2 solved the operationId issue, but now I get a 401 every time (on all calls). Even with the same exact code (without a body) that returns a 200 from the API docs.

Edit: After a complete wipe of node_modules, a yarn cache clean, followed by the fresh yarn install 4.5.2 returns the same response as 4.5.1, but still doesn't work.

0 Likes 0 ·

To be clear, even using the "manual" post method on the api package results in the same issue.

const api = require('api')
const sdk = api('@clover-platform/v3#cja48j514l5babyqi')

sdk.auth(token)
sdk.server('https://sandbox.dev.clover.com')
sdk.config({ parseResponse: false })
sdk
  .post('/v3/merchants/{mid}/category_items', {
    elements: [
      {
        item: { id: 'ZT1J6B5S6R1SY' },
        category: { id: '01RK004VVF99T' }
      }
    ]
  })
  .then((res) => console.log(res))
  .catch((err) => console.error(err))

But axios has no issue (as obviously cURL did not either).

const axios = require('axios')

const instance = axios.create({
  baseURL: 'https://sandbox.dev.clover.com',
  headers: { Authorization: 'Bearer {token}' }
})
instance
  .post(
    '/v3/merchants/{mid}/category_items',
    {
      elements: [
        {
          item: { id: 'ZT1J6B5S6R1SY' },
          category: { id: '01RK004VVF99T' }
        }
      ]
    }
  )
  .then((res) => console.log(res))
  .catch((err) => console.error(err))

So this is most likely an issue with the OpenAPI spec as published by Clover. I believe, that fixing it will also fix the documentation.

0 Likes 0 ·
Node is beyond my knowledge. I hope someone who is familiar with Node will chime in.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community