Erpnext Rest API for Sales Invoice creation from Sales Order using Postman

Hi All,

I am having a problem creating and saving the invoice using the sale order name . I am trying to do this in postman. Here is my screenshot

It outputs a json file but once I check the erpnext website I could not find what was the invoice generated.

Here is the sample json file:

  {
    "message": {
        "owner": "Albert@gmail.com",
        "idx": 0,
        "docstatus": 0,
        "naming_series": "ACC-SINV-.YYYY.-",
        "customer": "Albert",
        "customer_name": "Albert",
        "is_pos": 0,
        "is_consolidated": 0,
        "is_return": 0,
        "company": "Store",
        "posting_date": "2020-10-14",
        "posting_time": "16:47:34.608501",
        "set_posting_time": 0,
        "due_date": "2020-10-14",
        "update_billed_amount_in_sales_order": 0,
        "territory": "All Territories",
        "shipping_address_name": "",
        "currency": "PHP",
        "conversion_rate": 1.0,
        "selling_price_list": "Standard Selling",
        "price_list_currency": "PHP",
        "plc_conversion_rate": 1.0,
        "ignore_pricing_rule": 1,
        "set_warehouse": "Albert CS",
        "update_stock": 0,
        "total_billing_amount": 0.0,
        "total_qty": 1.0,
        "base_total": 2500.0,
        "base_net_total": 2500.0,
        "total_net_weight": 0.0,
        "total": 2500.0,
        "net_total": 2500.0,
        "tax_category": "",
        "base_total_taxes_and_charges": 0.0,
        "total_taxes_and_charges": 0.0,
        "loyalty_points": 0,
        "loyalty_amount": 0.0,
        "redeem_loyalty_points": 0,
        "apply_discount_on": "Grand Total",
        "base_discount_amount": 0.0,
        "additional_discount_percentage": 0.0,
        "discount_amount": 0.0,
        "base_grand_total": 2500.0,
        "base_rounding_adjustment": 0.0,
        "base_rounded_total": 2500.0,
        "base_in_words": "PHP Two Thousand, Five Hundred only.",
        "grand_total": 2500.0,
        "rounding_adjustment": 0.0,
        "rounded_total": 2500.0,
        "in_words": "PHP Two Thousand, Five Hundred only.",
        "total_advance": 0.0,
        "outstanding_amount": 2500.0,
        "allocate_advances_automatically": 0,
        "base_paid_amount": 0.0,
        "paid_amount": 0.0,
        "base_change_amount": 0.0,
        "change_amount": 0.0,
        "write_off_amount": 0.0,
        "base_write_off_amount": 0.0,
        "write_off_outstanding_amount_automatically": 0,
        "group_same_items": 0,
        "language": "en",
        "is_internal_customer": 0,
        "customer_group": "All Customer Groups",
        "is_discounted": 0,
        "status": "Draft",
        "debit_to": "Debtors - CS",
        "party_account_currency": "PHP",
        "is_opening": "No",
        "c_form_applicable": "No",
        "commission_rate": 0.0,
        "total_commission": 0.0,
        "pos_total_qty": 0.0,
        "doctype": "Sales Invoice",
        "items": [
            {
                "owner": "Albert@gmail.com",
                "parentfield": "items",
                "parenttype": "Sales Invoice",
                "idx": 1,
                "docstatus": 0,
                "item_code": "Camera",
                "item_name": "Camera",
                "description": "Camera",
                "item_group": "Products",
                "image": "",
                "qty": 1.0,
                "stock_uom": "Nos",
                "uom": "Nos",
                "conversion_factor": 1.0,
                "stock_qty": 1.0,
                "price_list_rate": 2500.0,
                "base_price_list_rate": 2500.0,
                "margin_type": "",
                "margin_rate_or_amount": 0.0,
                "rate_with_margin": 0.0,
                "discount_percentage": 0.0,
                "discount_amount": 0.0,
                "base_rate_with_margin": 0.0,
                "rate": 2500.0,
                "amount": 2500.0,
                "base_rate": 2500.0,
                "base_amount": 2500.0,
                "is_free_item": 0,
                "net_rate": 2500.0,
                "net_amount": 2500.0,
                "base_net_rate": 2500.0,
                "base_net_amount": 2500.0,
                "delivered_by_supplier": 0,
                "income_account": "Sales - CS",
                "is_fixed_asset": 0,
                "expense_account": "Cost of Goods Sold - CS",
                "enable_deferred_revenue": 0,
                "weight_per_unit": 0.0,
                "total_weight": 0.0,
                "warehouse": "Albert- CS",
                "allow_zero_valuation_rate": 0,
                "item_tax_rate": "{}",
                "actual_batch_qty": 0.0,
                "actual_qty": 599.0,
                "sales_order": "SAL-ORD-2020-00068",
                "so_detail": "8dd69dbc8b",
                "delivered_qty": 0.0,
                "cost_center": "Main - CS",
                "page_break": 0,
                "doctype": "Sales Invoice Item",
                "__islocal": 1,
                "__unsaved": 1
            }
        ],
        "pricing_rules": [],
        "packed_items": [],
        "timesheets": [],
        "taxes": [],
        "advances": [],
        "payment_schedule": [],
        "payments": [],
        "sales_team": [],
        "__islocal": 1,
        "__onload": {
            "load_after_mapping": true
        },
        "__unsaved": 1
    }
}

Any thoughts on how will I make this create an invoice?

Please notice that the JSON response contains no data section. Instead it contains a message section.

Itā€™s obvious once you see it. In long hand the message is trying to say, ā€œOh, so ya wanna invoice, eh? Buster? Well hereā€™re the pieces. Go make yer own, ā€˜n quit botherinā€™ me.ā€

So all you have to do is cut out the message body, and paste it into a POST.

curl -sLX POST 'https://dev.yourpublic.work/api/resource/Sales%20Invoice' \
-H 'Authorization: token ec226d15b52879e:ca6deade2ff3d0e' \
-H 'Content-Type: application/json' \
--data-raw '{
        "owner": "Albert@gmail.com",
        "idx": 0,
        "docstatus": 0,
        "naming_series": "ACC-SINV-.YYYY.-",
        "customer": "Albert",
        "customer_name": "Albert",

     "     "     "     "     "     "     "
    }'
1 Like

Hey @Jest,

Iā€™m working with the API right now as well and finding that weā€™re having many issues using Postman in particular. I saw some other folks suggest to try using ARC (also free) instead and weā€™ve had luck with that. I believe Postman sometimes tacks on a header that is not recognized by ERPNext and it errors out and says ā€œ200 OKā€, which normally would mean success, but 200 is the ā€œresource not recognizedā€ code for ERPNext. Try it with ARC and see if that works. :slight_smile:

1 Like

I have used Postman with complete success for this exact task.

In the top-right corner of the bottom-left window there is a Cookies link and a Code link. You can press the Code link to generate an executable command in any one of a couple of dozen languages, including curl.

If you are unsuccessful creating a Sales Invoice from a Sales Order, please post the error message and a curl format example of what you did and I will help you try to track down the Postman setting that is incorrect.

1 Like

Thank you Iā€™ll try that

thanks Iā€™ll try it if Postman still wonā€™t work for me

Oh , I see I have not noticed it yesterday I was focused on the response header .

Is it possible to create invoice with just the Sales Order Name and not writing every details of that sales_order?

To be honest I do not see anything on an Invoice that links back to the Sales Order it came from.

1 Like

Have you found a solution? I have the same problem!

So this bit isnā€™t documented or advertised but most of the buttons you see like ā€œCreate > Sales Invoiceā€ are one-to-one mapped with backend whitelisted functions that map order to invoice or order to delivery etc. You can easily find the function name if you observe the network tab :grimacing:

In this case, to create an invoice from order, the easiest and most reliable way would be this:

  1. Call erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice with source_name as sales order code to receive Sales Invoice in JSON format.
  2. POST the received invoice on sales invoice REST API - POST /api/resource/Sales Invoice
  3. Optionally you can submit it later by doing PUT {"docstatus": 1}

You can also do custom python API using these functions to do everything in one call.

2 Likes

Thank you so much Ankush,

It works with Sales Invoice and Delivery note, but I canā€™t find how to do it with Payment Entry.

Thank you for your answer!

I found it for Payment Entry :

/api/method/erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry

{
ā€œdtā€: ā€œSales Invoiceā€,
ā€œdnā€: ā€œACC-SINV-2022-01083ā€
}

Sorry to revive an old post. This was a life saver. I was wondering why make_sales_invoice didnā€™t create the sales invoice. Turns out it just returns what is needed to create a Sales Invoice resource.

Now I just need to figure out how to match the sales order number and sales invoice numberā€¦ The doc is pretty vagueā€¦
https://docs.erpnext.com/docs/v14/user/manual/en/customize-erpnext/client-scripts/sales-invoice-id-based-on-sales-order-id