NodeJS script to POST records to a Database


This script simply creates a new record in the database table. Each time a GET call is made on the scripted service API endpoint, this script creates a new record in the table, 'departments'. You can create/write this data into any popular database whether it be MySQL, Microsoft SQL Server, or Oracle, or any other DreamFactory Service.

To create a scripted service, navigate to the 'Services' tab in your instance, hit the create button on the left, and select service type 'Scripts', and then select the desired scripting language. Don't forget that performing CRUD operations is just scratching the surface, you can also track things such as timestamps, status codes, and more!

// In the scripts tab, add this script to user -> user.session -> post ->
// Make sure you've correctly configured Logstash as a service in the Services tab of the DreamFactory Admin Console.

// To enable Node.js scripting, set the path to node in your DreamFactory .env file.
// This setting is commented out by default.
// DF_NODEJS_PATH=/usr/local/bin/node
// Use npm to install any dependencies. This script requires 'lodash.'
// Your scripts can call console.log to dump info to the log file in storage/logs.

var url, result, options;
options = {
    'headers': {
        'X-DreamFactory-Api-Key': platform.session.api_key,
        'X-DreamFactory-Session-Token': platform.session.session_token,
        'Content-Type': 'application/json'
var payload =
         "resource" : [{'dept_no': 'd325', 'dept_name': 'Accounting'}]
function createInternal() {
    // create a record using internal URL
    url = 'mysql/_table/departments';, payload, null, function(body, response) {
        result = JSON.parse(body);

Need API Advice?

Our team has advised thousands of companies around the world on API projects. Go to market faster by talking to the API experts.


Ready to get started?