Unable to Create Product with API Call

Solved
billrobertson42
Tourist
5 1 1

Hello,

I am unable to create a product via api call. Like, I can't create one at all. Even when I copy the JSON examples to post straight from the fine documentation.

I've tried many different things. Here's one example.

url: https://gttest11.myshopify.com/admin/api/2020-10/products.json

method: post

header X-Shopify-Access-Token: set to a working access token

body:

 

{
  "product":{
    "title":"Warbike Mob (Orks)",
    "vendor":"GW Games Workshop",
    "body_html":"CHANGE ME",
    "status":"draft",
    "handle":"gw-50-07-warbike-mob-orks",
    "published_scope":"web",
    "variants":[
      {
        "title":"Default Title",
        "price":"45.00",
        "sku":"GW-50-07",
        "position":1,
        "inventory_policy":"deny",
        "compare_at_price":"36.00",
        "fulfillment_service":"manual",
        "inventory_management":"shopify",
        "option1":"Default Title",
        "taxable":false,
        "requires_shipping":true
      }
    ],
    "options":[
      {
        "name":"Title"
      }
    ]
  }
}

 

 

Request ID's from the response header:

x-request-id: 1aab39ba-f7b4-4fb4-9d0b-daf687566983
cf-request-id: 06db653395000003b01b188000000001

Response body says nothing of any use. Hence why I'm here.

{
  "errors": {
    "product": "Required parameter missing or invalid"
  }
}

 

Please help me. As I said, this is just one of dozens that I've tried (and is close to what I think I need), but I can't even get examples from the documentation to work.

 

Thanks!

0 Likes
billrobertson42
Tourist
5 1 1

OK, I've managed to get one of the examples from the docs through now. I'm changing fields in it one by one to see if I can get it to be like I want, or to figure out where I went wrong.

0 Likes
Jack_Lee3
Shopify Partner
62 0 1

Here is PHP

Are you calling, say Curl correctly?

 

$custom = ""; // Not used here

$response = call_curl($link,true,false,$custom,$headersadd,$post_data);

function call_curl($init,$get,$post,$custom,$httpheader,$postfields)
{
$ch = curl_init($init);  // This is the LINK at Shopify
curl_setopt($ch,CURLOPT_SSLVERSION,'CURL_SSLVERSION_TLSv1_3'); // TLS 1.3
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPGET,$get);  // Get a Product, Must be FALSE
curl_setopt($ch,CURLOPT_POST,$post);  // Create a Product, MUST BE TRUE
curl_setopt($ch,CURLOPT_DNS_USE_GLOBAL_CACHE,true);
curl_setopt($ch,CURLOPT_DNS_CACHE_TIMEOUT,180);
curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);

curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$custom);

curl_setopt($ch,CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postfields);

$response = curl_exec($ch);

curl_close($ch);
return $response;
}

0 Likes
billrobertson42
Tourist
5 1 1

This is an accepted solution.

 

Well, I'm embarrassed to say this, but the difference was adding Content-Type: application/json as a header. Hopefully, this post will help someone in the future.

Oh, the example also has the price/compare-at prices backwards, so please note that too if you're using this as an example in the future for some reason.

0 Likes
billrobertson42
Tourist
5 1 1

I'm not sure if this really adds up or not. The trick in the postman-like interface was to add the content-type, but the code that I've written already does that.

 

0 Likes