How to create DocType via REST API JSON response?

Hello Team,
I need to create dynamic DocType based on the REST API JSON response.
Could you please help me here, how should I achieve this?
Thanks,
Jitendra Tilekar.

Hi @jitendra.tilekar,

If you haven’t checked then check it, please.

REST API:
https://frappeframework.com/docs/v13/user/en/api/rest

Responses:
https://frappeframework.com/docs/v13/user/en/python-api/response

Thanks.

Hi @NCP,

Here I am explaining my case:

URL : http:///api/getcustomerdata
JSON Response:
{
“customerID”:1234,
“firstName”:“Jitendra”,
“lastName”:“Tilekar”,
“type”:“Individual”
}

Based on the above JSON response I need to create a new DocType via python code, so how should I create a dynamic DocType with the above 4 fields?

If my API’s JSON response changed to:

JSON Response:
{
“customerID”:1234,
“Name”:“Jitendra Tilekar”,
“type”:“Individual”
}

So based on the above JSON response, the existing DocType should be changed with the above 3 fields.

https://frappeframework.com/docs/v13/user/en/api/rest#create

for the reference

client = frappe.get_doc(dict(
	doctype = 'Customer',
	first_name = "Test",
	last_name = "Name"
))
client.save()

Set you according to value and field name.

Thanks.

Hi @NCP,

My code:

class BankAPITest(Document):
	
	@frappe.whitelist()
	def createDoc(bean):
		client = frappe.get_doc(dict(
			doctype = 'Bank-API-Test',
			bankcode = bean
		))
		client.save()

I have tried the above example but I have got the below error:

File "apps/frappe/frappe/modules/utils.py", line 235, in load_doctype_module
  doctype_python_modules[key] = frappe.get_module(module_name)
File "apps/frappe/frappe/__init__.py", line 1236, in get_module
  return importlib.import_module(modulename)
File "/usr/local/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "apps/demo_app/demo_app/demo_app/doctype/bank_api_test/bank_api_test.py", line 11, in <module>
  class BankAPITest(Document):
File "apps/demo_app/demo_app/demo_app/doctype/bank_api_test/bank_api_test.py", line 84, in BankAPITest
  createDoc(bean['name'])
File "apps/demo_app/demo_app/demo_app/doctype/bank_api_test/bank_api_test.py", line 16, in createDoc
  client = frappe.get_doc(dict(
File "apps/frappe/frappe/__init__.py", line 1115, in get_doc
  doc = frappe.model.document.get_doc(*args, **kwargs)
File "apps/frappe/frappe/model/document.py", line 72, in get_doc
  controller = get_controller(doctype)
File "apps/frappe/frappe/model/base_document.py", line 62, in get_controller
  return _get_controller()
File "apps/frappe/frappe/model/base_document.py", line 58, in _get_controller
  raise ImportError(doctype)
ImportError: Bank-API-Test

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "apps/frappe/frappe/app.py", line 67, in application
  response = frappe.api.handle()
File "apps/frappe/frappe/api.py", line 54, in handle
  return frappe.handler.handle()
File "apps/frappe/frappe/handler.py", line 38, in handle
  data = execute_cmd(cmd)
File "apps/frappe/frappe/handler.py", line 76, in execute_cmd
  return frappe.call(method, **frappe.form_dict)
File "apps/frappe/frappe/__init__.py", line 1493, in call
  return fn(*args, **newargs)
File "apps/frappe/frappe/desk/form/load.py", line 79, in getdoctype
  docs = get_meta_bundle(doctype)
File "apps/frappe/frappe/desk/form/load.py", line 90, in get_meta_bundle
  bundle = [frappe.desk.form.meta.get_meta(doctype)]
File "apps/frappe/frappe/desk/form/meta.py", line 45, in get_meta
  meta = FormMeta(doctype)
File "apps/frappe/frappe/desk/form/meta.py", line 56, in __init__
  self.load_assets()
File "apps/frappe/frappe/desk/form/meta.py", line 76, in load_assets
  self.load_templates()
File "apps/frappe/frappe/desk/form/meta.py", line 225, in load_templates
  module = load_doctype_module(self.name)
File "apps/frappe/frappe/modules/utils.py", line 237, in load_doctype_module
  raise ImportError(
ImportError: Module import failed for Bank-API-Test (demo_app.demo_app.doctype.bank_api_test.bank_api_test Error: Bank-API-Test)

The sample response shows the response to the sample request. The response schema defines all possible elements in the response. The sample response is not exhaustive for all parameter configurations or operations but must match the parameters passed in the sample request. The answer allows developers to find out if the resource contains the information they want, the format and structure of the information, etc. The description of the response is sometimes also called the response scheme. The response schema documents the response in a more complete, generic way, listing each property that can be returned, what each property contains, the format of the value data, the structure, and other details. You can also refer to the website to learn more.