Docly Developers
DocsUpdated 22 Oct 2026

Save data to Docly from API-function

Example of how to save data to Docly from an API function in #/API/.

saveFile and deleteFile are only available in API functions (files in #/API/); fileExists works in templates too. An endpoint that saves data must only accept POST.

saveFile(filePath, data, [options])

Saves data as a new Docly document, or overwrites an existing one. Returns { timestamp, id, guid, filename }.

Param

Type

Description

filepath

string

Filepath and name for where to save your document. Filepath is absolute according to site root.

data

string (json)

Document data formatted as JSON in string

schema

string (filepath or hex)

What schema to create your Docly file from. Passed inside the options object.

• Specify path to schema file, WITHOUT the .docly extension — "#/My schema", not "#/My schema.docly"
• You can also specify with the document hash (#Dxxxx)

// #/API/SaveNote.js
export default (name) => {
    // Saving changes data, so it must never run on a GET (a link could trigger it).
    if (request.method != "POST") throw new Error("Verb not supported");
    if (!docly.isString(name)) throw new Error("Name is required");

    let data = {
        "Note": name
    };

    // Paths are extensionless — saveFile appends .docly itself.
    // "#/My schema.docly" would fail; "#/data/test.docly" would give test.docly.docly.
    return docly.saveFile("#/data/test", data, {
        schema: "#/My schema",
        ensurePath: true
    });   // { timestamp, id, guid, filename }
}

The example creates a "Note" document and returns its timestamp, id, guid and filename.

Note: the example saves data under #/ so it is not available from the published site.

Validation of parameters

Filename: #/API/RegPayment.js

export default (name) => {
    if (!docly.isString(name)) throw new Error("Name is required");
    // ...
}

API files are plain JavaScript and do not start with #{ like .hash files. In API functions the Docly helpers are called with the docly. prefix (docly.isString, not IsString). Throwing an error returns HTTP 403 with the error as JSON.

docly.fileExists(filepath)

Returns true if a file exists with this filepath. Filepath is absolute according to site root.

if (docly.fileExists("folder1/myfilename")) {
    /* file exists */
}    
else {
    /* file does not exist */
}

deleteFile(filepath)

Deletes specified file, returns true if deleted Otherwise false.

docly.deleteFile("folder1/testfile.txt");