Doc Type - APi _ Login

can we access the doctype api without login? is it possible please tell how to do?
Thank you

Hi @Ramki_Marichamy
If you refer resource/CRUD API, i think you cannot do that.

https://frappeframework.com/docs/v14/user/en/api/rest
Solution:

  1. You can create a custom API, you can refer to this document. And don’t forget to set
    @frappe.whitelist(allow_guest = True)

https://frappeframework.com/docs/v14/user/en/guides/integration/rest_api

  1. Alternative - You can create User, limit the role and access to doctype. Then always use it to login before making call to the API.
1 Like

I created a file in my module called turiya_api.py inside the file i had

import frappe
from frappe.utils.response import json_response

@frappe.whitelist(allow_guest=True)
def get_turiya_data():
data = frappe.db.get_all(‘Turiya’)
return json_response(data)

in the same module hooks. py i used this code
def setup_guest_api_routes(app):
api_version = “v1”
api_name = “turiya”
api_url = f"/api/{api_version}/{api_name}"
api_methods = {
“GET”: “turiyform.turiyform.turiya_api.get_turiya_data”
}
api.config.add_route(api_url, methods=api_methods)

def after_install(app, *args, **kwargs):
setup_guest_api_routes(app)

After calling that api? i got 404 error Whether is that right approach or any error in this code please expain thankyou

you have to pass a user’s api key and secret as part of your request header. make sure the user has the right permission to the resource you’re trying to query.

I think before you create ur custom url API path.
Can you confirm that :

http://[blabla.com]/api/method/turiyform.turiyform.turiya_api.get_turiya_data
don’t forget the /method
(replace the blabla.com) Is it working?
You should get the data… if not

  1. recheck the path again (especially in turiyform.turiyform.turiya_api.get_turiya_data)

  2. Or try restart the bench, to apply the code
    use this command on working bench

bench restart

For me the path is working for this sql query code
def get_turiya_data():
items = frappe.db.sql(“”“SELECT * FROM tabTuriyaData WHERE parent=‘2’; “””)
return items

For this code its not working
@frappe.whitelist(allow_guest=True)
def get_turiya_data():
data = frappe.db.get_all(‘Turiya’)
return json_response(data)