Issue with Collection Creation via API
I’m working with a PHP wrapper for Shopify’s API and running into problems when trying to build custom collections. Other API operations work fine like fetching items and managing cart data, but collection creation keeps throwing errors.
My current approach:
$collection_data = array(
"custom_collection" => array(
"title" => "Summer Sale Items",
"products" => array(
"item_id" => 78451923,
"item_id" => 78329847
)
)
);
try {
$result = $api_client('POST', '/admin/custom_collections.json', $collection_data, $headers);
// Check API usage
echo api_calls_used($headers);
echo api_calls_remaining($headers);
}
catch (ApiException $error) {
echo "Failed to create collection";
var_dump($error);
}
Error Response:
Getting a 500 server error with minimal details in the response. The error object shows the request data but doesn’t give clear information about what’s wrong with my payload structure.
I’ve been following the official documentation examples but something in my data format must be incorrect. Has anyone successfully created collections this way? What am I missing in the request structure?
You have two issues: the PHP array syntax and the way Shopify collections function. The problem with your products array is that you have duplicate keys, which PHP will overwrite without warning. More critically, when you POST to /admin/custom_collections.json, Shopify only allows metadata like title and handle. After creating the collection, you’ll receive a collection ID, and then you need to make separate POST requests to /admin/collects.json with both the collection_id and product_id for each product you want to add. This two-step process is essential; you can’t bulk-add products upon creation, which is likely causing your 500 error.
Had this same issue a few months ago. The problem’s in your products array structure. You can’t assign product IDs directly when creating collections - Shopify’s API doesn’t work that way for custom collections. Your products array format is wrong too - you’ve got duplicate “item_id” keys, which breaks things. Here’s what works: create the collection first without any products, then use a separate API call to add the products. Once you create the collection, you’ll get a collection ID back. Then hit the collects endpoint to link your products to that collection. Took me hours to figure out this workflow - kept getting the same 500 error. Start simple: just create the collection with title and handle first.
yeah, that products array is broken. u can’t have duplicate keys in PHP - the second one overwrites the first. plus Shopify won’t let you add products when creating collections anyway.
try this:
$collection_data = array(
"custom_collection" => array(
"title" => "Summer Sale Items",
"handle" => "summer-sale"
)
);
create the collection first, then add products through the collects api.