0
votes

I am trying to post a new item creation to a test store via C#, but I'm not sure how the syntax should read. Square Connect API requires at least one variation for new item creation, but I'm not sure how to add that to the JSON body. Here is what I have, but I'm not sure how to complete it.

var client = new RestSharp.RestClient();
var post = new RestRequest("https://connect.squareup.com/v1/me/items", Method.POST);
post.RequestFormat = DataFormat.Json;
post.AddHeader("Authorization", String.Format("Bearer {0}", testtoken));
post.AddBody(new { name = testname, variations = ???? });

This code works, but returns a response of an item must include at least one variation. I realize that, but do not know how to write it, or if it is even possible.

I am not opposed to going a different route.

Edited to add a sample request body from the Square documentation:

   {
       "name": "Milkshake",
       "description": "It's better than yours",
       "visibility": "PRIVATE",
       "category_id": "36ac7016-3a4e-4934-81f1-9057ac613f2y",
       "variations": [
       {
         "name": "Small",
         "pricing_type": "FIXED_PRICING",
         "price_money": {
           "currency_code": "USD",
           "amount": 400
       },
        "sku": "123"
      }
   ]
}
1
I'm going to be releasing a c# library to interact with their api in the near future. Are you interested? - Ronnie Overby

1 Answers

0
votes

Something like this should serialize to JSON in the correct format:

post.AddBody(new {
    name = testname,
    variations = new object[] {
        new {
            name = "Small",
            pricing_type = "FIXED_PRICING",
            price_money = new {
                currency_code = "USD",
                amount = 400
            }
        }
    },
    sku = "123"
});