How to connect to Splunk Enterprise using the Splunk SDK for JavaScript

To start a Splunk session, the first thing your program must do is connect to Splunk by sending login credentials to the splunkd server. Splunk returns an authentication token, which is then automatically included in subsequent calls for the rest of your session. By default, the token is valid for one hour, but is refreshed every time you make a call to splunkd.

We'll go over how to do this for server and client.

Server-side JavaScript

The basic steps to connect to Splunk are as follows:

  1. The Splunk SDK for JavaScript requires Node.js for server-side scripting, so use the require('splunk-sdk') function to add the Splunk SDK for JavaScript to your own Node.js programs.
  2. Create an instance of the Service class, which is the primary entry point to the Splunk client library and provides access to most of Splunk's resources.

Here's the example code to start a Splunk session. For simplicity, the credentials for logging in to the splunkd server are hard-coded, so replace them with your own. This example also prints the locally-installed Splunk apps to the console to verify you connected successfully.

Before you run this example, be sure to start the Splunk server if it's not running already.

var splunkjs = require('splunk-sdk');

// Create a Service instance and log in 
var service = new splunkjs.Service({
  username:"admin",
  password:"changeme",
  scheme:"https",
  host:"localhost",
  port:"8089",
  version:"5.0"
});

// Print installed apps to the console to verify login
service.apps().fetch(function(err, apps) {
  if (err) {
    console.log("Error retrieving apps: ", err);
    return;
  }

  console.log("Applications:");

  var appsList = apps.list();
  for(var i = 0; i < appsList.length; i++) {
    var app = appsList[i];
    console.log("  App " + i + ": " + app.name);
  }
});

Client-side JavaScript

The basic steps to connect to Splunk in client-side script are as follows:

  1. Add <script> tags that include the required libraries: jQuery.min.js, and either splunk.js or splunk.min.js.
  2. Create an instance of the Service class, which is the primary entry point to the Splunk client library and provides access to most of Splunk's resources.

Here's a sample HTML page that displays a button that when clicked, connects to Splunk and lists the locally-installed Splunk apps to verify that you connected successfully. For simplicity, the credentials for logging in to the splunkd server are hard-coded, so replace them with your own.

Before you run this example, update the paths to the required libraries and start the Splunk server if it's not running already.

<html>
  <head>
    <meta charset="utf-8">
    <title>How to start a Splunk session</title>
    <script type="text/javascript" src="YOUR_PATH/jquery.min.js"></script>
    <script type="text/javascript" src="YOUR_PATH/client/splunk.js"></script>

    <script type="text/javascript" charset="utf-8">
    function displayApps() {
        var http = new splunkjs.ProxyHttp("/proxy");

        // Create a Service instance and log in 
        var service = new splunkjs.Service(http, {
            username: "admin",
            password: "changeme",
            scheme: "https",
            host: "localhost",
            port:"8089",
            version:"5.0"
        });


        // Verify we logged in

        // Print installed apps to the console to verify login
        service.apps().fetch(function(err, apps) {
            if (err) {
                alert("Error listing apps");
                return;
            }

            var myapps = "";
            var appsList = apps.list();
            for(var i = 0; i < appsList.length; i++) {
                myapps += "App " + i + ": " + appsList[i].name + "<br/>"
            } 

            document.getElementById("applist").innerHTML=myapps;
        });

    }
    </script> 
</head>
<body>
    <button type="button" onclick="displayApps()">Log in and list apps</button>
    <p id="applist"></p>
</body>
</html>

You can use the sample web server to test this code. First, start the web server—open a command prompt in the /splunk-sdk-javascript directory and enter:

node sdkdo runserver

Then, save this code as an HTML page and open it in a web browser. For example, navigate to http://localhost:6969/examples/browser/filename.html.