Frappe Rest API | Custom Rest API

REST API Documentation

Step 1:

Create a Custom doctype in your Custom app

assume Whatsapp your custom app and Status is your custom doctype

Step 2: Task

Create a 2 API create_satatus and delete_status

Step 3: Create API in Status Doctype

status.py

import json
import frappe
from frappe.model.document import Document
from werkzeug.wrappers import Response
from frappe.utils import response as r


class ShoppingCart(Document):
	pass


@frappe.whitelist(method='POST')
def create_status():
    # Your Code here
    data = json.loads(frappe.request.data)
    doc = frappe.new_doc('Status')
    doc.title = 'New Task 1'
    doc.ordered_by = frappe.session.user
    doc.text_status = data.get('text_status')
    doc.insert()
    return {'status_code':200,  'message': 'Status Uploaded succesfully'}

@frappe.whitelist(method='DELETE')
def delete_status():
    # Your Code here
   data = json.loads(frappe.request.data)
    frappe.db.delete("Route History", {
        "name": data.get("name")
    })

Step 4: After creating the API hit API from the postman

add Header in both API CLICK

Get API Key and Secret Key

Go to User doctype and scroll down you find a API Access generate there

POST method

http://mysite.localhost:8000/api/method/whatsapp.whatsapp.doctype.status.status.create_status

JSON BODY

{
"text_status": "Hi I'm in Traveling"
}

DELETE method

http://mysite.localhost:8000/api/method/whatsapp.whatsapp.doctype.status.status.delete_status

JSON BODY

{
"name": "status-0001"
}

2 Likes

Hello @Antony_Praveenkumar I tried the same steps in Postman but I am still getting "Insufficient Permission for doctype, Can you share what you did in the Doctype

First use the log in API after that you hit this api in postman

login

curl --cookie-jar snowcookie --request POST "http://<base-url>/api/method/login" -H 'Content-Type: application/json' -H 'Accept: application/json' --data-raw "{ \"usr\" : \"<username>\", \"pwd\": \"<password>\" }"
{"message":"Logged In","home_page":"/app","full_name":"<user:full_name>","dashboard_route":"/sites"}

get_logged_user

curl --cookie snowcookie --request POST "http://<base-url>/api/method/frappe.auth.get_logged_user" -H 'Accept: application/json'
{"message":"<username>"}

After that you will get the permission

I tried to log in from Postman and I got this error

I also add the authorization in the login

Thank you , it works using the get_logged_user

1 Like