Getting error when trying to make custom product collection through Shopify REST API

I need help with creating a product collection using the Shopify API

I’m working on building a custom collection through Shopify’s REST API but keep running into issues. I can successfully do other API operations like fetching products and managing shopping carts, but creating collections with specific product IDs just won’t work.

Here’s what I’m trying to do:

$collection_data = array(
    "custom_collection" => array(
        "title" => "MyNewCollection",
        "collects" => array(
            "product_id" => 12345678,
            "product_id" => 87654321
        )
    )
);

try {
    $result = $shopify_client('POST', '/admin/custom_collections.json', $collection_data, $headers);
    
    echo shopify_calls_made($headers);
    echo shopify_calls_left($headers);
    echo shopify_call_limit($headers);
    
} catch (ShopifyApiException $error) {
    echo "API call failed";
    print_r($error);
}

The response I get back shows a 500 Internal Server Error with this exception:

ShopifyApiException Object
(
    [info:protected] => Array
    (
        [method] => POST
        [path] => /admin/custom_collections.json
        [response_headers] => Array
        (
            [http_status_code] => 500
            [status] => 500 Internal Server Error
            [content-type] => application/json; charset=utf-8
        )
        [response] => Array
        (
            [errors] => Error
        )
    )
)

I’m following the official Shopify documentation but still getting stuck. Has anyone encountered this before or know what might be causing the internal server error? Any suggestions would be really helpful.

I encountered a similar issue recently. The Shopify REST API requires that you first create the collection without including products in the same call. After creating the collection, you should obtain the collection ID and then send separate requests to the /admin/collects.json endpoint for each product. This two-step process avoids the 500 Internal Server Error you’re facing. Following this approach worked for me.

yeah, that 500 error’s happening because you’re trying to do everything in one api call. shopify doesn’t work that way - you’ve gotta separate the collection creation from adding products to it. create the collection first, grab the id it returns, then hit the collects endpoint separately.

your collects array structure is off - you can’t just add product_ids like that when creating the collection. First, create the collection, then make separate POST calls to /admin/collects.json for each product. that’s why you’re getting that 500 error.

Your PHP array structure is the problem. You can’t have multiple product_id keys in the same array - PHP will only keep the last one since keys must be unique. So you’re ending up with just one product ID instead of two.

But there’s a bigger issue: Shopify’s API doesn’t let you add products when creating collections anyway. You’ve got to create the empty collection first, grab the collection ID from the response, then loop through your product IDs and make separate POST requests to the collects endpoint for each one. That’s just how Shopify built their REST API to handle the relationship between collections and products.