# Creating Standalone Scripted Services

To create a standalone scripted service, you'll navigate to Services > Create and then click the Select Service Type dropdown. There you'll find a scripted service type called Script, and under it you'll find links to the supported scripting engine languages (PHP, Python, and NodeJS):

After choosing your desired language you'll be prompted to supply the usual namespace, label, and description for your API. Click the Next button and you'll be presented with a simple text editor. You're free to experiment by writing your script inside this editor, or could use the Link to a service option to reference a script stored in a file system, or within a repository. Keep in mind you'll first need to configure the source control or file API in order for it to be included in the Link to a service dropdown.

In addition to taking full advantage of the scripting language syntax, you can also use special data structures and functionality DreamFactory injects into the scripting environment. For instance, you can listen for request methods using the $event['request']['method'] array value. For instance try adding the following code to a scripted service:

if ($event['request']['method'] == "POST") {
  dd("POST request!);
} elseif ($event['request']['method'] == "GET") {
  dd("GET request!);

Save the changes, and then try contacting the scripted service endpoint with GET and POST methods. The dd() function will fire for each respective conditional block.

For more sophisticated routing requirements, we recommend taking advantage of one of the many OSS routing libraries. For instance bramus/router offers a lightweight PHP routing package that can easily be added to DreamFactory (see the next section, "Using Third-Party Libraries"). Once added, you'll be able to create sophisticated scripted service routing solutions such as this:



$router = new \Bramus\Router\Router();
$response = new \DreamFactory\CustomResponse();

$router->before('GET', '/.*', function () {
  header('X-Powered-By: bramus/router');

$router->get('/.*', function() use($response) {
  $response->setContent('Hello Router World!');

$router->set404(function() {
  header('HTTP/1.1 404 Not Found');
  $response->setContent('404 not found');


return $response->getContent();

# Example Standalone Scripted Services

# Obfuscate Table Endpoints (PHP)

This script allows you to obfuscate table endpoints to a more concise endpoint. For example, you might want to change service_name/_table/employees to just exployees.

Paste the script into the PHP scripted service and change api_path variable to be whatever service/_table/tablename you want to obfuscate. Save the service. It is now available using the standard DreamFactory table record API procedures, except the endpoint is shortened.


// Set up the platform object with shortcuts to each verb
$api = $platform['api'];
$get = $api->get;
$post = $api->post;
$put = $api->put;
$patch = $api->patch;
$delete = $api->delete;

$api_path = 'db/_table/todo'; // the service/_table/tablename you wish to obfuscate
$method = $event['request']['method']; // get the HTTP Method
$options['parameters'] = $event['request']['parameters']; // copy params from the request to the options object

// if there are additional resources in the request path add them to our request path
if ( $event['resource'] && $event['resource'] != '' ) { 
  $api_path = $api_path . '/' . $event['resource'];

if ( $event['request']['payload'] ) { // if the payload is not empty assign it to the payload var
  $payload = $event['request']['payload'];
} else { //else make the payload null
  $payload = null;

switch ( $method ) { // Determine which verb to use when making our api call
case 'GET':
  $result = $get ( $api_path, $payload, $options );
case 'POST':
  $result = $post ( $api_path, $payload, $options );
case 'PUT':
  $result = $put ( $api_path, $payload, $options );
case 'PATCH':
  $result = $patch ( $api_path, $payload, $options );
case 'DELETE':
  $result = $delete ( $api_path, $payload, $options );
  $result['message'] = 'Invalid verb.';

return $result; // return the data response to the client