Create custom get endpoint

Hello everyone,
I want to create an endpoint that takes two parameters and return customer detail ( it will return the customer details that i entered in parameters) in server script i write this code
“”"
import request
url = “example.com/api/resource/Customer/
payload = {
“fields”: ‘[“name”,“customer_name”]’,
}
headers = {
‘Authorization’: 'token xxxxxxxx:x,xxxxxxxxxx,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
}
response = request.request(url, headers=headers, data=payload)
“””
but i have this error
pylint: disable=exec-used\n File \"\", line 12, in \nNameError: name ‘request’ is not defined\n"]"
i tried this frappe.make_get_request but it doesn’t work
any sollution please
Thank you

Hi @Lili13,

Please check the syntax.

import frappe

def get_customer_details(customer_name):
    base_url = "example.com"
    endpoint = "/api/resource/Customer"
    fields = ["name", "customer_name"]
    query_params = f"?fields={fields}"

    url = f"{base_url}{endpoint}{query_params}"
    headers = {
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN',  # Replace with your actual access token
        'Content-Type': 'application/json',
    }
    
    response = frappe.get_request(url, headers=headers)
    
    # Process the response as needed
    if response.status_code == 200:
        customer_data = response.json()  # Convert JSON response to Python dictionary
        return customer_data
    else:
        frappe.throw(f"Failed to fetch customer details: {response.status_code} - {response.text}")

# Call the function with the desired customer name
customer_name = "desired_customer_name"
customer_details = get_customer_details(customer_name)

Reference:

Thank You!

Thanks for answering,
but i have this error now line 12, in \nTypeError: ‘NoneType’ object is not callable\n"]"
} and the line contain this frappe.get_request(url, headers=headers)
(i am wrinting the code in SERVER SCRIPT of erpnext

Please check it.

Some import package and method does not work on the server side.

So please create a custom app and then check it.

Thank You!

1 Like

Okk , thanks for your answer

Ok, I do it now what i want to know is how and where to declare the route to my endpoint, example i want that to access to this endpoint, url/customerdet?name=…&customer_name=…
(i dont’t want to use /api in the endpoint i create .
i try to do it in frappe-bench/apps/frappe/frappe/app.py like this
elif request.path.startswith(‘/customerdet/’):
response = frappe.api.get_customer_details() (I declare the function get_customer_details in frappe-bench/apps/frappe/frappe/api.py)
but it doesn’t work
any help please