Try ERPNext Buy Support Partners Foundation

How to create invoice from sales order using API

I am new to ERPNext, I’m in the process of transitioning my company from an old ERP system to ERPNext and we have some existing processes I’m trying to emulate. We take orders from various systems and store them in XML. I will be using that data to create sales orders with the ERPNext REST API, and that looks fairly straight forward, by passing in data using a POST to api/resource/Sales Order.
My next step will be generating an invoice for the order. There is a whitelisted function in the selling.doctype.sales_order.sales_order file called make_sales_invoice. It seemed logical to me that this function would return JSON which could then be fed into api/resource/Sales Invoice to create the invoice and submit it. But the call api/resource/Sales Invoice returns errors.
My question is if there is way to easily generate the JSON to generate a Sales Invoice from a Sales Order using the API, or if I just need to GET the sales order and construct the JSON to make the Sales Invoice myself?

Try the following (in Linux).

But first, jq is crazy good.

sudo apt install jq;

Then correct the first three envvars and the rest oughtta work straight out of box:

declare A_SALES_ORDER="SAL-ORD-2020-00001";
declare ERPNToken="ec226d15b52879e:934bad7c3fadda0";
declare YOUR_HOST="";

curl -slX POST "https://${YOUR_HOST}/api/method/erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice" \
    -H "Authorization: token ${ERPNToken}" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    --data-urlencode "source_name=${A_SALES_ORDER}" \
> freshFromTheOven.json;

jq .message ./freshFromTheOven.json > hotButtered.json;

curl -slX POST "https://${YOUR_HOST}/api/resource/Sales%20Invoice" \
    -H "Authorization: token ${ERPNToken}" \
    -H "Content-Type: application/json" \
    -d @./hotButtered.json \
> jellySandwich.json;

jq .data ./jellySandwich.json;

We just got a bread making machine :crazy_face:

1 Like

Thank you so much @MartinHBramwell. Worked like a charm! Now I need to figure out what I was doing differently with Postman. I really appreciate the quick, accurate help! Also the humor. I hope you’re enjoying some fresh bread :smile:

Glad to hear it helped.

I wonder, were you trying to call this …


… or this …



Also, tell me you if you used jq, please.

Bread’s all et up … and I put on a pound from a half pound loaf :frowning: Now I can perch my phone on my pandemic paunch.

I used:


I used jq as you did in the script when I tested on bash. Can’t do that with postman. But I think it should still work without pretty printed json. I am guessing I just had some header set wrong in my Postman requests.

How did you make it using Postman?