0
votes

I want to create a product on my website and have it be created on square (which is working). However I also want to set the initial inventory which is seems there is no way to do it from the documentation. https://docs.connect.squareup.com/api/connect/v1/#post-inventory-variationid

If I go into my square account I can manually set up an initial amount, then query that entry and get the id and update it, but who wants to do anything manually. It defeats the purpose. Is there a way to create an inventory entry?

My second struggle is with uploading an image using unirest.

function uploadItemImage($itemId, $image_file) 
{
    global $accessToken, $locationId, $connectHost; 
    $requestHeaders = array 
    (
      'Authorization' => 'Bearer ' . $accessToken,
      'Accept' => 'application/json',
      'Content-Type' => 'multipart/form-data;'
    );  
    $request_body = array
    (   
'image_data'=>Unirest\Request\Body::file($image_file, 'text/plain', myproduct.jpg')
    );
    $response = Unirest\Request::post($connectHost . '/v1/' . $locationId . '/items/'.$itemId.'/image', $requestHeaders, $request_body);
    print(json_encode($response->type, JSON_PRETTY_PRINT)); 
}

where $itemId is taken from the product created earlier and $image_file is the direct link to the file on my server

I keep getting this error...

> PHP Fatal error:  Uncaught exception 'Unirest\Exception' with message
> 'couldn't open file "https://somewebsite/myPicture.jpg"  ' in
> rootFolder/Unirest/Request.php:479  Stack trace: 
> #0 rootFolder/Unirest/Request.php(292): Unirest\Request::send('POST', 'https://connect...', Array, Array, NULL, NULL) 
> #1 rootFolder/

Any help is much appreciated!

1

1 Answers

0
votes

Way to maximise the use of your question!

  1. There is not currently a way to set initial inventory via API, but new item and inventory management APIs are in the works, read more on the Square Blog
  2. I'm assuming that you are not literally using "https://somewebsite/myPicture.jpg" but it seems like unirest thinks you are trying to use a web url instead of getting a file from your filesystem. Try the following curl command and see if you can match up all the parts to unirest:

:)

curl --request POST \
--url https://connect.squareup.com/v1/XXXXXX/items/XXXXX/image \ 
--header 'authorization: Bearer sq0atp-XXXXX' \
--header 'cache-control: no-cache' \
--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
--form image_data=@/Users/ManuEng13/Desktop/test.png