Calling external APIs Last updated: 22. Oct 2026

Examples for API functions calling external WEB APIs (JSON) with POST and GET.

IMPORTANT: These functions are only available from the #/API folder.

httpGet(url, [headers])

Below is an example API endpoint #/API/test.js calling an external API. The credentials are read from the #/Config document — never write passwords or API keys in your code.

export default () => {
    // Credentials live in #/Config (password fields), never in code.
    let config = docly.getJson("#/Config") || {};

    /* Basic authentication */
    let headers = {
        "Authorization": "Basic " + docly.base64(config.ApiUser + ":" + config.ApiPassword)
    };

    /* Call server */
    let result = docly.httpGet("https://api.example.com/v1/items?id=1", headers);
    if (result && result.DoclyError) throw new Error("The external API did not answer");

    /* Return the result — Docly serialises the object to JSON */
    return result;
}

httpPost(url, data, [headers])

Posts data serialized as JSON to the url.

export default () => {
    let config = docly.getJson("#/Config") || {};

    let data = {
        id: 1
    };

    /* Basic authentication */
    let headers = {
        "Authorization": "Basic " + docly.base64(config.ApiUser + ":" + config.ApiPassword)
    };

    /* Call server */
    let result = docly.httpPost("https://api.example.com/v1/items", data, headers);
    if (result && result.DoclyError) throw new Error("The external API did not answer");
    return result;
}

httpFormPost(url, data, [headers])

Same as httpPost, but posts the data as a form (application/x-www-form-urlencoded).

Handle exceptions

If the request itself fails (network error, or a response that is not JSON), the result has a DoclyError field instead of throwing. An HTTP error status with a JSON body is returned as a normal result, so check the content as well. Check DoclyError before you use the result:

/* Call server */
let result = docly.httpPost("https://api.example.com/v1/items", data, headers);

/* Check for error */
if (result && result.DoclyError) throw new Error("The external API did not answer");

/* ... */