Creating doctype through Frappe API?

I have a use case where I need to run a lot migrations (doctypes) and doing it manually takes time. Is there a way where I can just hit frappe apis (or desk apis) to create the doctype ?

I am currently playing around with this api - /api/method/ . But I run into some request validations error (417) and since there is no documentation on the api, it is becoming very difficult to debug.

If someone has tried this out, please let me know. Thanks in advance.

It’s Interesting. I have not tried this but Can you try to write python scripts to read from JSON or excel info based on this to create boilerplate doctypes and then further edit ?

import frappe

def create_doctype(doctype_name, fields):
        frappe.get_doc("DocType", doctype_name)
        print("Doctype already exists:", doctype_name)
    except frappe.DoesNotExistError:
        doctype = frappe.new_doc("DocType")
            "doctype_name": doctype_name,
            "fields": fields,
            "module": "Custom"
        print("Doctype created:", doctype_name)

create_doctype("My Doctype", [
    {"fieldname": "field_1", "fieldtype": "Data", "label": "Field 1"},
    {"fieldname": "field_2", "fieldtype": "Int", "label": "Field 2"},
    {"fieldname": "field_3", "fieldtype": "Date", "label": "Field 3"},

Thanks for replying back.

The code snippet you have mentioned is to add a document or update it for a doctype. But what I am looking for creating a doctype itself through the API or running some kind of script.

I was able to achieve this by using this api /api/method/ Had to debug through the network requests to see what does the body takes.

Now my script can create multiple doctypes in a single run by doing api calls.