API connections

i want to make a connections between sales order in frappe with my API. this is example of my api url : http://000.000.0.000:9000/abc/api-so. can you help me to connect sales order data with this api url? thanks a lot

Reference:

i’ve been trying it. but i got nothing no error or success message appear. is there anything wrong? below i give my code. thankss

@frappe.whitelist()
def send_sales_order_to_cimory(doc, method):
if method != “on_submit”:
return

sales_order = frappe.parse_json(doc)

url = "http://000.000.0.000:9000/abc/api-so"

sales_person = sales_order.sales_team[0].sales_person if sales_order.sales_team else ""

data = {
	"Orderno": sales_order.get("name"),  
    "Custno": sales_order.get("customer"), 
    "Slsno": sales_person,
    "Tanggal": str(sales_order.get("transaction_date")),
    "Items": [
        {
            "Pcode": item.get("item_code"),  
            "Qty": item.get("qty")  
        } for item in sales_order.get("items")
    ]
}

headers = {'Content-Type': 'application/json'}

try:
	response = requests.post(url, data=json.dumps(data), headers=headers) 
	response.raise_for_status()
	frappe.log("Sales order data successfully sent to external API.")
	return {"status": "success", "message": "Sales order data successfully sent to external API"}
except requests.exceptions.RequestException as e:
	frappe.log_error(f"Error sending data to API: {e}")
	return {"status": "failed", "message": "Error sending data to API {e} "}

try it.

sales_order = frappe.get_doc("Sales Order", doc.name)

i already try. but how i know if the sales order data already sent to my external API?

Do you want an external program to talk to the API of ERPNext/Frappeframework,

or do you want to create some part of script in or use some function of ERPNext/Frappeframework to talk to an API of another program/device/host?

I want to create some part of script in or use some function of ERPNext/Frappeframework to talk to an API of another program/device/host

Then you need to refer to these functionalities:

https://frappeframework.com/docs/user/en/desk/scripting/script-api#api

API
Make external API calls from Frappe.

i’m sorry but i’m not really understand. can you give me the example of the code?

There is not much documentation, but this is what is offered as per the docs:

frappe.make_get_request

Make a GET request.

Example: frappe.make_get_request('https://example.com')

frappe.make_post_request

Make a POST request.

Example: frappe.make_post_request('https://example.com', data={'username: 'test'})

frappe.make_put_request

Make a PUT request.

Example: frappe.make_put_request('https://example.com', headers={'Auth': 'Bearer xyz'})

It’s not hidden, but the doc could be clearer in distinguishing between incoming and outgoing calls.
If you want to dig into the details, try grep on the frappe/erpnext repositories or search for these functions in the forum, maybe there are code examples.

A code snippet library would be nice, but, after all, the software itself is some kind of code snippet library itself.
These functions are probably used in any module/functionality that sounds like “integration”, “using external auth providers” and such (just to give you an idea of how to quickly restrain the search - anyway, grep is blazingly fast on the repo).
Learning to read the code is very useful and doesn’t take that long.
Much is nicely mapped, although there are some surprises.

so do i dont need this code?

I just tried this:

test㉿kali)-[~/frappe/bench/apps/frappe]
└─$ time grep -r make_get_request *                                                                                                                               130 ⨯
grep: frappe/utils/__pycache__/safe_exec.cpython-311.pyc: binary file matches
frappe/utils/safe_exec.py:                      make_get_request=frappe.integrations.utils.make_get_request,
grep: frappe/integrations/__pycache__/utils.cpython-311.pyc: binary file matches
frappe/integrations/utils.py:def make_get_request(url, **kwargs):


real    73.76s
user    1.51s
sys     5.04s
cpu     8%

The search (in all of “frappe” app) took 73 seconds (on a spinning hard disk, which is very slow compared to more modern SSDs), but in 2 seconds I had the lines which tell me where to look.
The first just redefines a definitions’ name and calls the real one.
The second refers to the file “frappe/integrations/utils.py” on a relative path starting with “~/frappe/bench/apps/frappe”
So with a
$ cat frappe/integrations/utils.py
you get the file where the function is defined.
This is all a dev needs to know in order to proceed further and start exploring how to use it.
But I only search in the “frappe” app. You can also search in erpnext app.

$ cd ../erpnext                   
$ time grep -r make_get_request * 

real    14.31s
user    0.51s
sys     1.60s
cpu     14%

It yields nothing in this quick (test) case. So the framework does provide the functions, but my erpnext version doesn’t use them. I didn’t install any integrations, so I’d have to install more stuff in order to find some examples in these.
These searches took me 3 minutes, but writing all this down many more… that’s the reality of answering a quick question (unless you have some code snippets ready, but adaptation to a specific case also takes some time).

Or try in the forum with a search for these function’s names.

Maybe you can use some of it adapting it to the “make_XXX_request” functions…
But you need to learn how to adapt these things to work together as you wish.