Jump to content

creating a new ledger account via API giving error 422 response (Unprocessable Entity).


Recommended Posts

Posted

Hi there,

I am trying to create a new ledger account by sending the following json object in the body of a POST request to https://api.accounting.sage.com/v3.1/ledger_accounts:

{
   "ledger_account":{
      "ledger_account_type_id":"SALES",
      "included_in_chart":true,
      "name":"Sea Freight (sales)",
      "display_name":"Sea Freight (sales)",
      "nominal_code":4030,
      "visible_in_banking":false,
      "visible_in_expenses":false,
      "visible_in_journals":true,
      "visible_in_other_payments":true,
      "visible_in_other_receipts":false,
      "visible_in_reporting":true,
      "visible_in_sales":true
   }
}

the API is returning a 422 error (Unprocessable Entity), with the following content:

[
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account is missing",
      "source":"ledger_account"
   },
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account[ledger_account_type_id] is missing",
      "source":"ledger_account[ledger_account_type_id]"
   },
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account[included_in_chart] is missing",
      "source":"ledger_account[included_in_chart]"
   },
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account[name] is missing",
      "source":"ledger_account[name]"
   },
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account[display_name] is missing",
      "source":"ledger_account[display_name]"
   },
   {
      "severity":"error",
      "dataCode":"Validation",
      "message":"ledger_account[nominal_code] is missing",
      "source":"ledger_account[nominal_code]"
   }
]

I cannot see the problem, please can anyone advise where I might be going wrong?

Many thanks,

Dan.

Posted

Hi Mark,

I am using a trial Sage account for development purposes (Accounting Plus). The business is set as a UK business.

For the ledger account types, the response from your suggested endpoint is as follows:

{
    "$total": 13,
    "$page": 1,
    "$next": null,
    "$back": null,
    "$itemsPerPage": 20,
    "$items": [
        {
            "id": "SALES",
            "displayed_as": "Sales",
            "$path": "/ledger_account_types/SALES"
        },
        {
            "id": "OTHER_INCOME",
            "displayed_as": "Other Income",
            "$path": "/ledger_account_types/OTHER_INCOME"
        },
        {
            "id": "DIRECT_EXPENSES",
            "displayed_as": "Direct Expenses",
            "$path": "/ledger_account_types/DIRECT_EXPENSES"
        },
        {
            "id": "OVERHEADS",
            "displayed_as": "Overheads",
            "$path": "/ledger_account_types/OVERHEADS"
        },
        {
            "id": "DEPRECIATION",
            "displayed_as": "Depreciation",
            "$path": "/ledger_account_types/DEPRECIATION"
        },
        {
            "id": "CURRENT_ASSETS",
            "displayed_as": "Current Assets",
            "$path": "/ledger_account_types/CURRENT_ASSETS"
        },
        {
            "id": "FIXED_ASSETS",
            "displayed_as": "Fixed Assets",
            "$path": "/ledger_account_types/FIXED_ASSETS"
        },
        {
            "id": "FUTURE_ASSETS",
            "displayed_as": "Future Assets",
            "$path": "/ledger_account_types/FUTURE_ASSETS"
        },
        {
            "id": "BANK",
            "displayed_as": "Bank",
            "$path": "/ledger_account_types/BANK"
        },
        {
            "id": "CURRENT_LIABILITY",
            "displayed_as": "Current Liability",
            "$path": "/ledger_account_types/CURRENT_LIABILITY"
        },
        {
            "id": "FUTURE_LIABILITY",
            "displayed_as": "Future Liability",
            "$path": "/ledger_account_types/FUTURE_LIABILITY"
        },
        {
            "id": "EQUITY",
            "displayed_as": "Equity",
            "$path": "/ledger_account_types/EQUITY"
        },
        {
            "id": "LINE_OF_CREDIT",
            "displayed_as": "Credit Card / Loan",
            "$path": "/ledger_account_types/LINE_OF_CREDIT"
        }
    ]
}

Can you see any reason why the API is rejecting my POST request?

Many thanks,

Dan.

Posted

I copied your body into my request and the request created the ledger_account without issue.

What headers are you passing in the request?

I'll PM you with an email address you can invite to the business to allow us to investigate.

Thanks

Mark

Posted

Hi Mark,

Many thanks for your assistance. In the end the issue turned out to be that my .Net code was adding the Content-Type header as "application/json; charset=utf-8" rather than simply "application/json", and this was then causing the request to be rejected by the server at your end.

I can see that the code at your end was simply not receiving any json from the http middleware, so that caused the cascade of error messages as seen. Just as a thought, it would be better to catch the error with the header rather than letting the subsequent errors get returned. There is an error message returned if the header doesn't contain "application/json", but it could also check for any unwanted additional parts to the string.

Thanks for your quick and comprehensive support in this matter.

Dan.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...