question

toddinator2 avatar image
toddinator2 asked toddinator2 commented

How to associate item/category using API category_items with axios request

I am using axios.request for the APIs, and I'm uploading the categories and items from our database to Clover. I can get the categories and items into Clover just fine, but I cannot seem to get the items associated with the categories they need to be associated with. I'm sending the correct data for category_items:

  1. el: { category: {…}, item: {…}}
    1. category: { id: 'Z840SB2NKRD2W'}
    2. item: { id: '85JERFNGZWF72'}

and using the correct options:

const options = {

method: 'POST',

url: `${cloverURL}/merchants/${props.mId}/category_items?expand=${el}`,

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

};

But nothing is happening, the items are not getting associated with the category. I need some help getting this figured out.

REST API
10 |2000

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

parquet76 avatar image
parquet76 answered toddinator2 commented

It looks like you didn't see my nested comment so I'm reposting it.

Good luck with blancarivera, I think that user is an ai bot, there seems to be a ton of those around here lately handing out very poor information.

As for your problem, look at the docs, there is an example request:

https://docs.clover.com/docs/managing-categories#associating-items-with-a-category

You are missing the "elements" wrapper.


1 comment
10 |2000

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

toddinator2 avatar image toddinator2 commented ·

Thank you so much @ that helped a lot!

Just so others are clear on the answer, and because Clover's documentation for axios on this is so unclear, especially with the elements wrapper, here is how to do this in axios:


const options = {

method: 'POST',

url: `${cloverURL}/merchants/${mId}/category_items`,

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

data: {

elements: categoryItems,

},

};

await axios.request(options);


the categoryItems above is your array of category IDs and item IDs:

[{"category":{"id":"{categoryId}"},"item":{"id":"{item1Id}"}}]


Hope this helps others!

0 Likes 0 ·
blancarivera avatar image
blancarivera answered

Hello,

It seems that you're trying to associate items with categories using the category_items endpoint in the Clover API. From your code snippet, it looks like you're using the axios library to make the API request. My Sutter Online

To properly associate items with categories using the category_items endpoint, you need to make sure you're sending the correct data and handling the response correctly. Here's an example of how you can modify your code to achieve that:

const axios = require('axios');


// Assuming `el` is an array of category-item associations

const categoryItems = [

{ category: { id: 'Z840SB2NKRD2W' }, item: { id: '85JERFNGZWF72' } },

// Add more category-item associations as needed

];


const authToken = 'YOUR_AUTH_TOKEN';

const cloverURL = 'https://api.clover.com/v3.0';


// Make the API request to associate items with categories

axios

.post(

`${cloverURL}/merchants/${props.mId}/category_items`,

categoryItems,

{

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

}

)

.then((response) => {

console.log('Items associated with categories successfully:', response.data);

// Handle the successful response

})

.catch((error) => {

console.error('Error associating items with categories:', error);

// Handle the error

});

In this code, categoryItems is an array containing the category-item associations you want to create. Each association consists of category and item objects, each containing an id property with the respective category and item IDs.

Make sure to replace 'YOUR_AUTH_TOKEN' with your actual authentication token, and 'https://api.clover.com/v3.0' with the appropriate base URL for the Clover API.

This code uses the axios.post method to send a POST request to the category_items endpoint. The categoryItems array is sent as the request body. The API response is then logged to the console in the .then block for successful requests or caught in the .catch block for any errors that occur.


10 |2000

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

toddinator2 avatar image
toddinator2 answered toddinator2 published

Thank you for getting back so quickly Blanca, unfortunately I did not have time to get back to this until today. Does the API call for category_items no longer work? I have tried this every single way I can think of and it just does not work. This is in the Sandbox environment, so not sure if that makes a difference, but that's how desperate in my thinking I am now LOL. I have even gone as far as putting a setTimeout() at 1200ms on each iteration of the category_items call and that didn't help. All the data being sent to the axios request or post is correct as you can see:

INDIVIDUALLY IN THE SETTIMEOUT FUNCTION

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}category: {id: 'D5W7BSF1HGBDJ'}item: {id: '3Q3BHQMSZGH8P'}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}category: {id: 'D5W7BSF1HGBDJ'}item: {id: '7G1FPP5DB58WM'}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ci: {category: {…}, item: {…}}

ALL AT ONCE IN THE ARRAY

catItems: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

0: {category: {…}, item: {…}}

1: {category: {…}, item: {…}}

2: {category: {…}, item: {…}}

category: {id: "W5J1HQ2MGDXGW"}

item: {id: 'S467NY2CP57K2'}

3: {category: {…}, item: {…}}

4: {category: {…}, item: {…}}

5: {category: {…}, item: {…}}

6: {category: {…}, item: {…}}

7: {category: {…}, item: {…}}

8: {category: {…}, item: {…}}

9: {category: {…}, item: {…}}

10: {category: {…}, item: {…}}

category: {id: 'W5J1HQ2MGDXGW'}

item: {id: 'R3M8A311G1AET'}

11: {category: {…}, item: {…}}

12: {category: {…}, item: {…}}

13: {category: {…}, item: {…}}

14: {category: {…}, item: {…}}

Here are a couple ways I have tried:

The same way I am doing all other API calls:

const options = {

method: 'POST',

url: `${cloverURL}/merchants/${props.mId}/category_items`,

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

data: {

items: props.categoryItems,

},

};

const { data } = await axios.request(options);


The way you suggested:

axios

.post(`${cloverURL}/merchants/${props.mId}/category_items`, props.categoryItems, {

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

})

.then((response) => {

console.log('Items associated with categories successfully:', response.data);

if (data.length !== 0) {

return data;

}

})

.catch((error) => {

console.error('Error associating items with categories:', error);

});


With a setTimeout function (your suggestion and my way):

categoryItems.forEach((ci, index) => {

const ciTimer = setTimeout(async () => {

const options = {

method: 'POST',

url: `${cloverURL}/merchants/${mId}/category_items`,

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

data: {

items: ci,

},

};

const { data } = await axios.request(options);

ciCount++;

}, 1200 * (index + 1));

});


Maybe I'm just not seeing something obvious that's wrong, but I have spent hours on this trying every different way I can think of, but the sad part is that I am not getting any errors at all, except for the BAD_REQUEST error on your suggestion way, and I might have that code wrong as well, but it looks like everything is working fine until I go into the Sandbox and see that items and categories are still not linked. Any more help would be greatly appreciated!! Thanks!

10 |2000

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

toddinator2 avatar image
toddinator2 answered parquet76 edited

Thanks for responding back so quickly Blanca. With your suggestion I am getting a BAD_REQUEST error, which obviously means I have some code wrong somewhere.

axios

.post(`${cloverURL}/merchants/${props.mId}/category_items`, props.categoryItems, {

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

})

.then((response) => {

console.log('Items associated with categories successfully:', response.data);

})

.catch((error) => {

console.error('Error associating items with categories:', error);

});


My way that I am using for all other API calls, seems like it is working, I get no errors at all, but the items and categories are still not linked when finished running.

const options = {

method: 'POST',

url: `${cloverURL}/merchants/${props.mId}/category_items`,

headers: {

Accept: 'application/json',

Authorization: `Bearer ${authToken}`,

},

data: {

items: props.categoryItems,

},

};

const { data } = await axios.request(options);

if (data.length !== 0) {

return data;

}


All of the data being sent is correct:

catItems: (5) [{…}, {…}, {…}, {…}, {…}]

0: {category: {…}, item: {…}}

1: {category: {…}, item: {…}}

category: {id: '4Z3PES9Q8ZE7T'}

item: {id: 'DJ0GKGSS56Q04'}

2: {category: {…}, item: {…}}

3: {category: {…}, item: {…}}

category: {id: '4Z3PES9Q8ZE7T'}

item: {id: 'MP3HA7WY6Z104'}

4: {category: {…}, item: {…}}


I have spent hours on this and just cannot figure out what the issue is. This is all in Sandbox, so maybe that makes a difference? That's how far my thinking has gone LOL. No errors whatsoever, data is correct, and still not linking items to categories. I have even gone as far as putting a setTimeout function for 1200ms on each categoryItem iteration and still not working. Are we sure the category_items API call still works? LOL Please help with any other suggestions or find what's wrong in my code. Thanks!

1 comment
10 |2000

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

parquet76 avatar image parquet76 commented ·

Good luck with blancarivera, I think that user is an ai bot, there seems to be a ton of those around here lately handing out very poor information.

As for your problem, look at the docs, there is an example request:

https://docs.clover.com/docs/managing-categories#associating-items-with-a-category

You are missing the "elements" wrapper.



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