vb.netapisage-one

Unable to Post an invoice to Sage One via API 422 Error


I am able to authenticate against the SAGE ONE API and also able to retrieve data from the API however I am now trying to post a simple invoice however keep getting a 422 error. Is there something obvious I am missing?

I have double checked the JSON structure and the values i am providing and are all valid. Not sure what i am doing wrong here!

my JSON package looks like this

{"contact_id":"66b76fa19edc11e797950a57719b2edb",
"date":"2019-04-13",
"invoice_lines":[{"description":"Test Description",
"ledger_account_id":"0367afd89ece11e797950a57719b2edb","quantity":1,"unit_price":100}]}

code 

Dim http As HttpWebRequest = WebRequest.Create(New Uri("https://api.columbus.sage.com/uki/sageone/accounts/v3/sales_invoices"))
            http.Method = "POST"
            http.ContentType = "application/json"
            http.Accept = "application/json"
            http.Headers.Add("X-Site", "mysite")
            http.Headers.Add("ocp-apim-subscription-key", "mykey")
            http.Headers.Add("Authorization", "Bearer " mytoken)

            Dim data = Encoding.UTF8.GetBytes(json)
            http.ContentLength = data.Length

            Dim stream = http.GetRequestStream()
            stream.Write(data, 0, data.Length)
            stream.Close()

            Dim response = http.GetResponse().GetResponseStream()

Solution

  • This POST body worked for me (with different ids):

    {
      "sales_invoice": {
        "contact_id": "66b76fa19edc11e797950a57719b2edb",
        "date": "2019-04-13",
        "main_address": { "address_line_1": "42 Test Street" },
        "invoice_lines": [
          {
            "description": "Test Description",
            "ledger_account_id": "0367afd89ece11e797950a57719b2edb",
            "quantity": 1,
            "unit_price": 100,
            "tax_rate_id": "GB_NO_TAX"
          }
        ]
      }
    }
    

    You need to wrap your JSON structure with a key, which is the name of your resource ("sales_invoice" in this case).

    You also need to specify a main address, as well as a tax rate id for each line item.

    To be sure to have a valid ledger account for your sales invoice, get a list of possible accounts with:

    GET /ledger_accounts?visible_in=sales