Getting 500 Error When Creating Custom Collection via Shopify PHP API

I’m trying to create a custom collection using a PHP library for Shopify API but keep getting a 500 Internal Server Error. Here’s my code:

define('STORE_DOMAIN', 'mystore.myshopify.com');
define('API_KEY', 'your_api_key_here');
define('API_PASSWORD', 'your_password');
define('SHARED_SECRET', 'your_secret');

require 'shopify_lib/client.php';

$client = shopify_api_client(STORE_DOMAIN, NULL, API_KEY, API_PASSWORD, true);

try {
    $collection_data = array(
        "custom_collection" => array(
            "title" => "Electronics",
            "collects" => array(
                "product_id" => 12345678
            )
        )
    );

    $result = $client('POST', '/admin/custom_collections.json', $collection_data, $headers);
    
    echo "API calls made: " . shopify_calls_made($headers);
    echo "Calls remaining: " . shopify_calls_left($headers);
    
} catch (ShopifyApiException $error) {
    print_r($error);
}

The error shows HTTP status 500 with the message “Internal Server Error”. The response just says “errors: Error” without more details. I’m not sure if the problem is with how I’m structuring the collection data or if there’s something wrong with my API credentials. Has anyone else run into this issue when creating collections through the Shopify API? What could be causing this server error?

Your data structure’s the problem. You can’t nest collects inside custom_collection - that’s not how Shopify’s API works. Collections and product links are separate endpoints. I ran into the same thing when I started with their API. Create the collection first without any products, then use /admin/api/2023-10/collects.json to link products after. Also check your API version in the URL - that legacy format might not work with newer Shopify stores. Make sure your PHP library supports the current REST Admin API.

had the same problem last week. that 500 error usually means you’re hitting a validation rule on shopify’s side. first, double-check that product_id 12345678 actually exists in your store - invalid product IDs trigger this error. also make sure your api permissions include write access for collections.

try making the collection without adding products at first, then you can use the collects endpoint to add them in later. that should get rid of the 500 error.